MockMvcRequestConverter uses US_ASCII for URL decoding while encoding uses UTF-8 #1034
MockMvcRequestConverter uses US_ASCII for URL decoding while encoding uses UTF-8 #1034config25 wants to merge 2 commits 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
| @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); | ||
| } | ||
|
|
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.
There was a problem hiding this comment.
Thank you for the review. You're right — the two added tests don't exercise the decode() method since it's only invoked when the request has both a query string and form parameters. I'll remove those two tests and add a new
one that covers the non-ASCII case with both a query string and parameters, similar to postRequestWithParametersAndQueryStringCreatesFormUrlEncodedContentWithoutDuplication. I'll update the PR shortly.
Signed-off-by: config25 <yhkim052556@naver.com>
24924a2 to
fbf7cba
Compare
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