spring-cloud-contract icon indicating copy to clipboard operation
spring-cloud-contract copied to clipboard

Contract response body in json array format

Open Qingyuan-Liu opened this issue 2 years ago • 9 comments

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.

Qingyuan-Liu avatar Nov 10 '21 03:11 Qingyuan-Liu

And the response I got from the stub is: [ "["162383699343","162383699343"]" ]

Qingyuan-Liu avatar Nov 10 '21 03:11 Qingyuan-Liu

So [[a,b]] means an array of arrays. You should do [a,b]

marcingrzejszczak avatar Nov 10 '21 06:11 marcingrzejszczak

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!

Qingyuan-Liu avatar Nov 10 '21 06:11 Qingyuan-Liu

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?

marcingrzejszczak avatar Nov 10 '21 07:11 marcingrzejszczak

It will return the same result which is: [ "["1623836993432","1623836993432"]" ]

Qingyuan-Liu avatar Nov 10 '21 08:11 Qingyuan-Liu

Meh. It's a bug then...

marcingrzejszczak avatar Nov 10 '21 08:11 marcingrzejszczak

Hi @marcingrzejszczak , has this been fixed? This blocks me in a test case.

Zane-XY avatar Nov 18 '21 02:11 Zane-XY

The status of this issue hasn't changed so it's not been resolved. PRs are welcome!

marcingrzejszczak avatar Nov 19 '21 14:11 marcingrzejszczak

Probably related to #1656.

igarash1 avatar Mar 28 '22 07:03 igarash1

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

marcingrzejszczak avatar Apr 26 '23 13:04 marcingrzejszczak