Skip to content

Ignore Accept-Charset when reading String request bodies - #13801

Merged
graemerocher merged 1 commit into
micronaut-projects:5.2.xfrom
sharanggupta:gh-13739-body-string-charset
Oct 8, 2026
Merged

graemerocher merged 1 commit into
micronaut-projects:5.2.xfrom
sharanggupta:gh-13739-body-string-charset

Conversation

@sharanggupta

Copy link
Copy Markdown
Contributor

StringBodyReader and TextPlainObjectBodyReader decoded the request body with MessageBodyWriter.findCharset(mediaType, headers). When the Content-Type has no charset, that falls back to the Accept-Charset header, and for a request body the headers are the request's own. So Content-Type: application/json plus Accept-Charset: Big5 made a UTF-8 body decode as Big5 and corrupted non-ASCII text.

Accept-Charset expresses preferences for the response (RFC 9110 section 12.5.2), not the encoding of the request body. The readers now use only the charset parameter of the Content-Type, falling back to the configured default charset (UTF-8 unless micronaut.application.default-charset is set).

Tests: RequestBodyCharsetTest covers buffer, InputStream and chunked reads with Accept-Charset: Big5 and ISO-8859-1, a missing media type, text/plain, and that an explicit Content-Type charset still wins. On 5.2.x nine of the eleven tests fail, with the garbled output from the report; the two guards for the explicit charset pass before and after.

Two notes for review:

  • This changes behaviour for clients that relied on Accept-Charset to choose the request body charset when the Content-Type has none. That was never correct per the RFC, and it is what the issue asks for.
  • HttpMessage#getCharacterEncoding() (via HttpUtil.getCharset and HttpUtil.resolveCharset) still falls back to the request's Accept-Charset, and NettyHttpRequestSpec asserts that on purpose. It is the same kind of problem for form decoding and client request encoding, but a wider API behaviour, so I left it out of this PR. Happy to look at it separately if you want.

Fixes #13739

StringBodyReader and TextPlainObjectBodyReader resolved the charset
through MessageBodyWriter.findCharset, which falls back to the Accept-Charset
header of the headers passed in. For a request body those are the request's
own headers, so a UTF-8 body without a charset in its Content-Type was
decoded with whatever the client listed in Accept-Charset, corrupting
non-ASCII text (for example Accept-Charset: Big5 with Korean JSON).

Accept-Charset states the charsets the client prefers for the response
(RFC 9110 section 12.5.2) and says nothing about how the request body is
encoded. The readers now use only the charset parameter of the Content-Type
and otherwise the configured default charset (UTF-8 unless
micronaut.application.default-charset is set).

Fixes micronaut-projects#13739
@cla-assistant

cla-assistant Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@graemerocher
graemerocher requested a balanced review from Copilot October 7, 2026 21:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@graemerocher
graemerocher merged commit 011ab8a into micronaut-projects:5.2.x Oct 8, 2026
12 checks passed
@graemerocher graemerocher added the type: improvement A small improvement to an existing feature label Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: improvement A small improvement to an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants