Skip to content

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

@config25

Description

@config25

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.javawhich was introduced in the same commit (f5a629af, "Handle form and query parameters separately") by the same authorcorrectly 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);
}

---

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions