简体   繁体   中英

How define request body object property as nullable in Spring Cloud Contract Groovy DSL Contract definition?

I'm working on Spring Cloud based microservices, with spring cloud contract validation of client/API between services. WireMock is used in client service side tests for services API stubbing. Spring Cloud is in version 2020.0.2 . I have following Groovy contract definition:

Contract.make {
description "Nullable sender & receiver API contract"

request {
    method POST()
    url value(consumer(regex('/message')))
    headers {
        contentType(applicationJson())
        header("x-channelId", anyNonBlankString())
    }
    body(
            sender: [
                    toEmail            : anyEmail(),
                    firstName          : anyNonBlankString(),
                    lastName           : anyNonBlankString()
            ],
            receiver: [
                    toEmail            : anyEmail(),
                    firstName          : anyNonBlankString(),
                    lastName           : anyNonBlankString()
            ]
    )
}
response {
    status CREATED()
}
}

The think I want to achieve is to have both 'sender', and 'receiver' nullable. I can define nullable (DSL optional() ) each sub-field of both, but request body can look like this:

{sender:null, receiver:null}

I've tried define multiple contracts for each possibility(one with both filled, second with sender: null , third with receiver: null , and both null). In this case, client service side WireMock, that uses above definitions for API stubbing, does not match wanted stubs to proper requests - eg when calling using request with empty sender , WireMock identifies as closest stub one with expected sender and returns 404, ignoring contract definition that expects empty sender .

Is there way to define field with internal structure as nullable? In documentation I've only saw possibility to make nullable field value, but not whole structure.

If a field is potentially null you should create 2 contracts. 1 with it being null and 1 with it not being null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM