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

Use UTF-8 for application/json in MockHttpServletResponse #27846

Closed
wants to merge 1 commit into from

Conversation

izeye
Copy link
Contributor

@izeye izeye commented Dec 23, 2021

This PR changes to use UTF-8 for application/json in MockHttpServletResponse.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 23, 2021
@sbrannen sbrannen added in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) labels Dec 23, 2021
@sbrannen
Copy link
Member

Hi @izeye,

Is there an existing issue related to this change? If not, can you please briefly explain the rationale for the change (even if it seems self explanatory to some)?

Also, please note that you only changed the code in the test fixture. Thus, you would also need to update org.springframework.mock.web.MockHttpServletResponse, MockHttpServletResponseTests, etc.

@sbrannen sbrannen added the status: waiting-for-feedback We need additional information before we can continue label Dec 23, 2021
@sbrannen sbrannen marked this pull request as draft December 23, 2021 16:06
@izeye
Copy link
Contributor Author

izeye commented Dec 24, 2021

@sbrannen Thanks for the feedback!

Is there an existing issue related to this change? If not, can you please briefly explain the rationale for the change (even if it seems self explanatory to some)?

I noticed that MockMvcResultHandlers.print() with UTF-8 doesn't work with the MediaType.APPLICATION_JSON (application/json) while it works with the deprecated MediaType.APPLICATION_JSON_UTF8 (application/json;charset=UTF-8). See https://github.com/izeye/spring-boot-throwaway-branches/blob/minimal-rest/src/test/java/com/izeye/throwaway/HomeControllerTests.java#L67-L70 for an example.

Also, please note that you only changed the code in the test fixture. Thus, you would also need to update org.springframework.mock.web.MockHttpServletResponse, MockHttpServletResponseTests, etc.

Sorry. I should have looked into it closely, but it seems that I just searched and picked a wrong one. Thanks for guiding me to the right direction. I updated accordingly. Please let me know if there's anything missing or worng.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Dec 24, 2021
@sbrannen sbrannen self-assigned this Dec 24, 2021
@sbrannen sbrannen removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 24, 2021
@sbrannen sbrannen added this to the 5.3.15 milestone Dec 24, 2021
@sbrannen sbrannen marked this pull request as ready for review January 7, 2022 15:25
@sbrannen sbrannen added the type: enhancement A general enhancement label Jan 7, 2022
@sbrannen
Copy link
Member

sbrannen commented Jan 7, 2022

Sorry. I should have looked into it closely, but it seems that I just searched and picked a wrong one.

No problem. Even core maintainers (like me) often forget to update both versions of those mocks.

Thanks for guiding me to the right direction.

You're welcome.

I updated accordingly. Please let me know if there's anything missing or worng.

I think that looks pretty good now; however, having looked at the issue in more detail I'm starting to wonder if that is the correct "fix".

As far as I know, we don't set the character encoding for the response to UTF-8 in such scenarios in production code (in spring-webmvc). Instead, we let the client correctly interpret the response as UTF-8.

For the particular test scenario you've described, I see the print() functionality as the "client" in Mock MVC.

In other words, I don't think we should make this change to MockHttpServletResponse.setContentType(String). Instead, I think we should make a change to org.springframework.test.web.servlet.result.PrintingResultHandler.printResponse(MockHttpServletResponse) to infer UTF-8 if the content type in the response is application/json.

@rstoyanchev and @sdeleuze, what are your thoughts on the matter?

@rstoyanchev
Copy link
Contributor

rstoyanchev commented Jan 10, 2022

There is some history. The corresponding MediaType was deprecated in #22788 and removed from use throughout because the charset is no longer defined for application/json. Subsequently, a similar change was considered for MockMvc in #23219 but resulted in a more limited change. I do not think we should revive the charset parameter on application/json anywhere but if there is a specific problem to solve somewhere we can try to be more lenient where it is present or needs to be implied.

@sbrannen
Copy link
Member

Hi @izeye,

Thanks again for the PR.

In the end, we decided to address this differently (see comments above).

I am therefore closing this as superseded by #27926.

@sbrannen sbrannen closed this Jan 12, 2022
@izeye izeye deleted the application-json branch January 12, 2022 16:14
@sbrannen sbrannen removed this from the 5.3.15 milestone Jan 13, 2022
@sbrannen sbrannen added the status: superseded An issue that has been superseded by another label Jan 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) status: feedback-provided Feedback has been provided status: superseded An issue that has been superseded by another type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants