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

Fix writing serialized JSON with WebClient and WebTestClient [SPR-15443] #20004

Closed
spring-projects-issues opened this issue Apr 12, 2017 · 1 comment
Assignees
Labels
in: web type: bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Apr 12, 2017

Ledoyen Loïc opened SPR-15443 and commented

When using plain text JSON as body, WebTestClient uses Jackson2JsonEncoder which escape given JSON String instead of leaving it untouched.

WebTestClient client = WebTestClient.bindToController(new MyController()).build();

client
                .post()
                .uri("/api/myservice/v1")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .body("{\"field1\": \"value1\"}") // does not work
//                .body("{\"field1\": \"value1\"}".getBytes()) // works
                .exchange()
                .expectStatus().isOk();

For some reason, the method Jackson2JsonEncoder#encodeValue is called when content is a String, but not called with a byte[].

However for the two types, the Flux is mapped by the Jackson2JsonEncoder#encode (l.120) method.


Affects: 5.0 M5

Referenced from: commits 3efb76c

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Apr 12, 2017

Rossen Stoyanchev commented

This is because in WebFlux the CharSequenceEncoder is ordered once ahead for "text/plain" only and then once again at the very end for \*/\* allowing others in between to get involved, e.g. Flux<String> + "text/event-stream".

On the decoding side Jackson2JsonDecoder already explicitly ignores String. I think we need the same on the encoding side. There is little value in writing String as JSON even if content type is "application/json" but rather let it pass and be handled by the CharSequenceEncoder.

This is also consistent with Spring MVC which will write a String with the StringHttpMessageConverter for any media type.

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

No branches or pull requests

2 participants