spring-cloud-contract
spring-cloud-contract copied to clipboard
Contract response body in json array format
Describe the bug Hi team, I would like to have my response be a json array, but I got a string. I've stated the content type as 'application/json'.
Sample I would like to have my response look like this:
[
[
1623836993432,
1623836993432
]
]
And here is how I defined my contract response:
response {
status OK()
body(
[
[
162383699343,
162383699343
]
]
)
headers {
contentType'application/json'
}
}
Could you help me fix this issue? Thanks.
And the response I got from the stub is: [ "["162383699343","162383699343"]" ]
So [[a,b]] means an array of arrays. You should do [a,b]
Sorry for the confusion, yes, I would like to have an array of arrays. My response should look like this: [ [ 1623836993432, 1623836993432 ] ] Instead of this: [ "["162383699343","162383699343"]" ]
So in this case, how should I define my response? Thanks!
That sounds like a bug. What if you provide the response as a string?
response {
status OK()
body("""
[
[
1623836993432,
1623836993432
]
]
""")
headers {
contentType'application/json'
}
}
Is it any better then?
It will return the same result which is: [ "["1623836993432","1623836993432"]" ]
Meh. It's a bug then...
Hi @marcingrzejszczak , has this been fixed? This blocks me in a test case.
The status of this issue hasn't changed so it's not been resolved. PRs are welcome!
Probably related to #1656.
I don't think this is a problem for Contract 3.1.x. I added the test case
@Issue("1729")
def "should not change types of array elements"() {
given:
org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.
make {
request {
method('GET')
url("/api/foo")
}
response {
status OK()
body([162383699343, 162383699343])
headers {
header 'Content-Type': 'text/plain'
}
}
}
when:
String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).
toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual('''
{
"request" : {
"url" : "/api/foo",
"method" : "GET"
},
"response" : {
"status" : 200,
"body" : "[162383699343,162383699343]",
"headers" : {
"Content-Type" : "text/plain"
},
"transformers" : [ "response-template", "foo-transformer" ]
}
}
''', wireMockStub)
and:
stubMappingIsValidWireMockStub(wireMockStub)
}
and the test passes. So the WireMock stub doesn't change the types. I will close this issue and if someone proves that it's still a bug I will reopen it.
Duplicate of https://github.com/spring-cloud/spring-cloud-contract/issues/1656