MockMvcRequestConverter uses US_ASCII for URL decoding while encoding uses UTF-8 #1034
Open
config25 wants to merge 1 commit intospring-projects:mainfrom
Open
MockMvcRequestConverter uses US_ASCII for URL decoding while encoding uses UTF-8 #1034config25 wants to merge 1 commit intospring-projects:mainfrom
config25 wants to merge 1 commit intospring-projects:mainfrom
Conversation
MockMvcRequestConverter.decode() was using StandardCharsets.US_ASCII for URL decoding while urlEncode() in the same class uses StandardCharsets.UTF_8. This mismatch causes non-ASCII characters in query parameters to be corrupted. Align with QueryParameters.decode() which correctly uses UTF_8. Fixes spring-projectsgh-1033 Signed-off-by: config25 <yhkim052556@naver.com>
06e99f3 to
aed94c2
Compare
wilkinsona
requested changes
Mar 31, 2026
Comment on lines
+153
to
+169
| @Test | ||
| void getRequestWithNonAsciiParametersProducesCorrectQueryString() { | ||
| OperationRequest request = createOperationRequest( | ||
| MockMvcRequestBuilders.get("/foo").param("name", "\uD64D\uAE38\uB3D9")); | ||
| assertThat(request.getUri()) | ||
| .isEqualTo(URI.create("http://localhost/foo?name=%ED%99%8D%EA%B8%B8%EB%8F%99")); | ||
| assertThat(request.getMethod()).isEqualTo(HttpMethod.GET); | ||
| } | ||
|
|
||
| @Test | ||
| void postRequestWithNonAsciiParametersCreatesCorrectFormUrlEncodedContent() { | ||
| OperationRequest request = createOperationRequest( | ||
| MockMvcRequestBuilders.post("/foo").param("name", "\uD64D\uAE38\uB3D9")); | ||
| assertThat(request.getContentAsString()).isEqualTo("name=%ED%99%8D%EA%B8%B8%EB%8F%99"); | ||
| assertThat(request.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_FORM_URLENCODED); | ||
| } | ||
|
|
Member
There was a problem hiding this comment.
These tests don't exercise the changed code. The decode method is only used when the request has both a form URL encoded body and a query string. See postRequestWithParametersAndQueryStringCreatesFormUrlEncodedContentWithoutDuplication below. Instead of adding these two tests, that test could be modified with some UTF-8 values or a new test added to cover that case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MockMvcRequestConverter.decode()usesStandardCharsets.US_ASCIIfor URL decoding, whileurlEncode()in the same class usesStandardCharsets.UTF_8. This causes non-ASCII characters (e.g., CJK, accented characters) inquery parameters to be corrupted.
This was likely introduced unintentionally in commit
f5a629af("Handle form and query parameters separately"), asQueryParameters.decode()created in the same commit correctly usesStandardCharsets.UTF_8.Changes
MockMvcRequestConverter.java: ChangedUS_ASCII→UTF_8indecode()methodMockMvcRequestConverterTests.java: Added two tests verifying non-ASCII parameter handling for both GET (query string) and POST (form URL encoded body)Fixes gh-1033