-
Notifications
You must be signed in to change notification settings - Fork 733
MockMvcRequestConverter uses US_ASCII for URL decoding while encoding uses UTF-8 #1033
Copy link
Copy link
Closed as not planned
Closed as not planned
Copy link
Labels
Description
Bug Description
In MockMvcRequestConverter, the decode() method uses StandardCharsets.US_ASCII for URL decoding, while the urlEncode() method in the same class uses StandardCharsets.UTF_8 for encoding. This mismatch causes non-ASCII
characters (e.g., CJK, accented characters) in query parameters to be corrupted.
Location
spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcRequestConverter.java
Line 251 (encode):
return URLEncoder.encode(s, StandardCharsets.UTF_8);
Line 287 (decode):
return URLDecoder.decode(encoded, StandardCharsets.US_ASCII);
Evidence
This appears to be unintentional, as QueryParameters.java — which was introduced in the same commit (f5a629af, "Handle form and query parameters separately") by the same author — correctly uses StandardCharsets.UTF_8 for
decoding:
// QueryParameters.java line 87
return URLDecoder.decode(encoded, StandardCharsets.UTF_8);
How to Reproduce
When a MockHttpServletRequest contains a query parameter with non-ASCII characters (e.g., name=홍길동), the parameter is:
1. URL-encoded as name=%ED%99%8D%EA%B8%B8%EB%8F%99 using UTF-8
2. URL-decoded using US_ASCII, which cannot correctly interpret the multi-byte UTF-8 sequences
This results in corrupted parameter values in the generated documentation.
Expected Behavior
MockMvcRequestConverter.decode() should use StandardCharsets.UTF_8, consistent with:
- MockMvcRequestConverter.urlEncode() in the same class
- QueryParameters.decode() in the same project
Suggested Fix
private static String decode(String encoded) {
return URLDecoder.decode(encoded, StandardCharsets.UTF_8);
}
---Reactions are currently unavailable