Repository navigation
Ignore Accept-Charset when reading String request bodies - #13801
Merged
graemerocher merged 1 commit intoOct 8, 2026
Merged
graemerocher merged 1 commit into
graemerocher merged 1 commit into
Conversation
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
graemerocher
approved these changes
Oct 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
StringBodyReaderandTextPlainObjectBodyReaderdecoded the request body withMessageBodyWriter.findCharset(mediaType, headers). When theContent-Typehas nocharset, that falls back to theAccept-Charsetheader, and for a request body the headers are the request's own. SoContent-Type: application/jsonplusAccept-Charset: Big5made a UTF-8 body decode as Big5 and corrupted non-ASCII text.Accept-Charsetexpresses preferences for the response (RFC 9110 section 12.5.2), not the encoding of the request body. The readers now use only thecharsetparameter of theContent-Type, falling back to the configured default charset (UTF-8 unlessmicronaut.application.default-charsetis set).Tests:
RequestBodyCharsetTestcovers buffer, InputStream and chunked reads withAccept-Charset: Big5andISO-8859-1, a missing media type,text/plain, and that an explicitContent-Typecharset still wins. On5.2.xnine 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:
Accept-Charsetto choose the request body charset when theContent-Typehas none. That was never correct per the RFC, and it is what the issue asks for.HttpMessage#getCharacterEncoding()(viaHttpUtil.getCharsetandHttpUtil.resolveCharset) still falls back to the request'sAccept-Charset, andNettyHttpRequestSpecasserts 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