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

improper utf-8 handling in json-unit-spring #212

Closed
octopus-prime opened this issue Nov 2, 2019 · 6 comments
Closed

improper utf-8 handling in json-unit-spring #212

octopus-prime opened this issue Nov 2, 2019 · 6 comments

Comments

@octopus-prime
Copy link

Describe the bug

  • JsonUnit 2.11.0
  • BringBoot 2.2.0.RELEASE

When using json-unit with rest-assured all works fine:

        given(specification)
                .with()
                .accept(MediaType.APPLICATION_JSON_VALUE)
                .queryParam("produktIds", PreisApiData.PRODUKT_IDS)

                .when()
                .get("preise")

                .then()
                .statusCode(HttpStatus.OK.value())

                .assertThat()
                .contentType(MediaType.APPLICATION_JSON_VALUE)
                .body(jsonEquals(PreisApiData.PREIS_DTOS));

But writing the same test with json-unit-spring and mock-mvc causes encoding issues:

        mockMvc.perform(get("/preise")
                .accept(MediaType.APPLICATION_JSON)
                .param("produktIds", PreisApiData.PRODUKT1_ID.toString(), PreisApiData.PRODUKT2_ID.toString()))
                .andExpect(status().isOk())
                .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
                .andExpect(json().isEqualTo(PreisApiData.PREIS_DTOS));

->

Different value found in node "[0].currency", expected: <"€"> but was: <"â�¬">.
Comparison Failure: 
Expected :€
Actual   :�
@octopus-prime
Copy link
Author

Maybe in JsonUnitResultMatchers the code could be changed from

        @Override
        public void match(MvcResult result) throws Exception {
            String actual = result.getResponse().getContentAsString();
            doMatch(actual);
        }

to

        @Override
        public void match(MvcResult result) throws Exception {
            String actual = result.getResponse().getContentAsString(StandardCharsets.UTF_8);
            doMatch(actual);
        }

@lukas-krecan
Copy link
Owner

Good catch, thanks, will fix it.

@lukas-krecan
Copy link
Owner

lukas-krecan commented Nov 2, 2019

It seems that it's a problem on your side. Spring takes the encoding form the response content type. Can you check the 'Content-Type' header returned by the controller? I was able to reproduce it like this (you should do the opposit :)) https://github.com/lukas-krecan/JsonUnit/compare/unicode?expand=1

@octopus-prime
Copy link
Author

octopus-prime commented Nov 2, 2019

Note that
MediaType.APPLICATION_JSON_UTF8
and
MediaType.APPLICATION_JSON_UTF8_VALUE
are deprecated since Spring 5.2 (and so in Spring Boot 2.2) with reason

	/**
	 * Public constant media type for {@code application/json;charset=UTF-8}.
	 * @deprecated Deprecated as of Spring Framework 5.2 in favor of {@link #APPLICATION_JSON}
	 * since major browsers like Chrome
	 * <a href="https://bugs.chromium.org/p/chromium/issues/detail?id=438464">
	 * now comply with the specification</a> and interpret correctly UTF-8 special
	 * characters without requiring a {@code charset=UTF-8} parameter.
	 */

So the controller implements this API

@Tag(name = "Preis")
public interface PreisApi {

    @Operation(summary = "Get kunde")
    @GetMapping(path = "preise", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    Set<PreisDto> getPreise(@RequestParam("produktIds") Set<UUID> produktIds);
}

My proposed fix would try to
'interpret correctly UTF-8 special characters without requiring a {@code charset=UTF-8} parameter.'

@lukas-krecan
Copy link
Owner

Ok, it's quite a mess but this should help #213

@lukas-krecan
Copy link
Owner

Released as 2.11.1

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

No branches or pull requests

2 participants