Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spring-cloud-contract-maven-plugin does not escape double quotes in json strings #1850

Open
huberchrigu opened this issue Nov 23, 2022 · 3 comments
Labels

Comments

@huberchrigu
Copy link

Describe the bug
I generate contracts over Spring REST docs and convert them to stubs with the maven plugin. In a response I have a JSON String with escaped double quote. The stub should look like this:

{
  "id" : "1e23e0de-132c-4635-b2b9-2094783069ab",
  "request" : {
    "urlPath" : "",
    "method" : "GET"
  },
  "response" : {
    "status" : 200,
    "body" : "{\"text\":\"Text with \\\"double quotes\\\"\"}",
    "headers" : {
      "Content-Type" : "application/json"
    }
  },
  "uuid" : "1e23e0de-132c-4635-b2b9-2094783069ab"
}

But the maven plugin generates the body value as

"{\"text\":\"Text with \"double quotes\"\"}"

. This stub won't work because it will lead to deserialization errors.

Sample
Run mvn install in this repository: https://github.com/huberchrigu/spring-cloud-contract-issue. target/generated-snippets/stubs/test.json will look ok. But target/stubs/.../mappings/test.json will not. Latter is packaged into the stubs artifact, therefore the build does not create valid stubs right now.

@prashant-mns
Copy link

prashant-mns commented Jan 27, 2023

I am seeing similar issue. If i hardcode the graphql json request body string in yml contract file then generated test converts triple \ to single \ . but if i put that string in a separate request.json then generated test works fine on producer side. but then generated stub doesn't match on consumer side. Can following code be responsible for this? @marcingrzejszczak

https://github.com/spring-cloud/spring-cloud-contract/blob/main/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/util/DelegatingJsonVerifiable.java#L269

	private String escapedHackedJavaText(String value) {
		return StringEscapeUtils.escapeJava(value).replace("\\\"", "\"");
	}`

@marcingrzejszczak
Copy link
Contributor

It's possible @huberchrigu can you rewrite the sample to use Java? That way it will be easier for me to debug what's going on

@huberchrigu
Copy link
Author

@marcingrzejszczak I did the same in Java: https://github.com/huberchrigu/spring-cloud-contract-issue/tree/master/java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants