We are trying to set null
values into the producer client site code.
Example of a Groovy contract:
Contract.make {
description("Creating user")
name("Create user")
request {
method 'POST'
url '/api/user'
body(
name: $(consumer(anyNonEmptyString()), producer('John Doe')),
address: $(consumer(optional(regex(alphaNumeric()))), producer(null))
)
headers {
contentType(applicationJson())
}
}
response {
status 201
}
}
Following contract produces an assertion failed when trying to parse the Groovy contract:
assert testSide ==~ Pattern.compile(stubSide.optionalPattern())
| | | | |
null false | | ([a-zA-Z0-9]+)?
| ([a-zA-Z0-9]+)?
([a-zA-Z0-9]+)?
Any suggestion to set it to null in the generated class to get a result like this (or simply not adding the field address
:
@Test
public void create_user() throws Exception {
// given:
MockMvcRequestSpecification request = given()
.header("Content-Type", "application/json")
.body("{\"name\":\"John Doe\",\"address\": null,");
// when:
ResponseOptions response = given().spec(request)
.post("/api/user");
// then:
assertThat(response.statusCode()).isEqualTo(201);
}
There was an issue in Spring Cloud Contract that we treated an optional property as a value that is set to either empty or non-empty, but it had to be there. With fixing of https://github.com/spring-cloud/spring-cloud-contract/issues/1257 we've also added support for null
value.