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

escaped json string get expanded to wrong asserts in verification test

Open Zane-XY opened this issue 2 years ago • 5 comments

Given the contract definition

  outputMessage {
        body(file('response.json'))
  }

response.json

"payload": "{\"propsA\":\"valueA\",\"propsB\":\"valueB\"}"

The above contract generates multiple asserts

assertThatJson(parsedJson).field("['payload']").field("['propsA']").isEqualTo("valueA");
assertThatJson(parsedJson).field("['payload']").field("['propsB']").isEqualTo("valueB");

The payload here is a json string, should be treated as String, but it wrongly treated as json object.

Zane-XY avatar Aug 24 '21 05:08 Zane-XY

Can you provide content type to be application/json ? Also can you provide which version of contract you are using?

marcingrzejszczak avatar Aug 30 '21 08:08 marcingrzejszczak

@marcingrzejszczak

this is for a message contract. Can I make content type to be application/json inside a outputMessage?

Zane-XY avatar Aug 30 '21 09:08 Zane-XY

Yes. You can set it via messageHeaders

marcingrzejszczak avatar Aug 30 '21 10:08 marcingrzejszczak

I just tried, makes no difference

outputMessage {
        ...
        headers {
            contentType(applicationJson())
        }
}

still generates

			assertThatJson(parsedJson).field("['payload']").field("['propsA']").isEqualTo("valueA");
			assertThatJson(parsedJson).field("['payload']").field("['propsB']").isEqualTo("valueB");

Zane-XY avatar Aug 31 '21 05:08 Zane-XY

Meh, I'll have to look into that.

marcingrzejszczak avatar Aug 31 '21 08:08 marcingrzejszczak

With this commit https://github.com/spring-cloud/spring-cloud-contract/commit/a2444550d546f9f5663074d280475e72ed8cdf1d we're suggesting that whenever you have payload that contains an embedded JSON as part of your json, you should go with the fileAsBytes approach and then you won't have any additional JSON assertions.

marcingrzejszczak avatar Apr 26 '23 12:04 marcingrzejszczak