spring-cloud-contract
spring-cloud-contract copied to clipboard
Improper validation when objects contains array of objects with array inside
For a given contract:
package contracts.consumerApi
import org.springframework.cloud.contract.spec.Contract
Contract.make {
request {
method 'GET'
url '/test-get'
headers {
header('Content-Type', 'application/json')
}
}
response {
status OK()
body(
'''
{
"fields": [
{
"id": "1",
"options": []
},
{
"id": "2",
"options": [
{
"x": "x",
"y": "y"
}
]
}
]
}
'''
)
headers {
header('Content-Type', 'application/json')
}
}
}
The generated test looks like this:
DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
assertThatJson(parsedJson).array("['fields']").contains("['id']").isEqualTo("1");
assertThatJson(parsedJson).array("['fields']").array("['options']").isEmpty();
assertThatJson(parsedJson).array("['fields']").contains("['id']").isEqualTo("2");
assertThatJson(parsedJson).array("['fields']").array("['options']").contains("['x']").isEqualTo("x");
assertThatJson(parsedJson).array("['fields']").array("['options']").contains("['y']").isEqualTo("y");
which causes assertion to fail because the first field
object has empty options
array, and the second one has one element in options
array
@marcingrzejszczak @OlgaMaciaszek Could you take a look into that?
We will look into that asap, we're in the middle of various release processes atm.
Great, thank you! 👍
@marcingrzejszczak I am facing similar issues. Any news on when this is planned to be fixed? The workaround that seems to be working for now is to define that array element (fields[0].options) as a bodyMatcher by jsonpath with minOccurence 0
byType{
minOccurrence(0)
}
No deadlines just yet. We're currently preparing for 2022 release train