Skip to content

MockMvcRequestConverter uses US_ASCII for URL decoding while encoding uses UTF-8 #1034

Open
config25 wants to merge 1 commit intospring-projects:mainfrom
config25:fix/mockmvc-url-decoder-charset
Open

MockMvcRequestConverter uses US_ASCII for URL decoding while encoding uses UTF-8 #1034
config25 wants to merge 1 commit intospring-projects:mainfrom
config25:fix/mockmvc-url-decoder-charset

Conversation

@config25
Copy link
Copy Markdown

Summary

MockMvcRequestConverter.decode() uses StandardCharsets.US_ASCII for URL decoding, while urlEncode() in the same class uses StandardCharsets.UTF_8. This causes non-ASCII characters (e.g., CJK, accented characters) in
query parameters to be corrupted.

This was likely introduced unintentionally in commit f5a629af ("Handle form and query parameters separately"), as QueryParameters.decode() created in the same commit correctly uses StandardCharsets.UTF_8.

Changes

  • MockMvcRequestConverter.java: Changed US_ASCIIUTF_8 in decode() method
  • MockMvcRequestConverterTests.java: Added two tests verifying non-ASCII parameter handling for both GET (query string) and POST (form URL encoded body)

Fixes gh-1033

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>
@config25 config25 force-pushed the fix/mockmvc-url-decoder-charset branch from 06e99f3 to aed94c2 Compare March 31, 2026 01:30
@wilkinsona wilkinsona changed the title Fix URL decoding charset mismatch in MockMvcRequestConverter MockMvcRequestConverter uses US_ASCII for URL decoding while encoding uses UTF-8 Mar 31, 2026
@wilkinsona wilkinsona added type: bug A bug and removed status: waiting-for-triage Untriaged issue labels Mar 31, 2026
@wilkinsona wilkinsona added this to the 3.0.x milestone 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);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@wilkinsona wilkinsona added the status: waiting-for-feedback Feedback is required before progress can be made label Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-feedback Feedback is required before progress can be made type: bug A bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MockMvcRequestConverter uses US_ASCII for URL decoding while encoding uses UTF-8

3 participants