Skip to content

Honor the declared charset in the Protobuf JSON codecs - #37557

Closed
sharanggupta wants to merge 1 commit into
spring-projects:mainfrom
sharanggupta:protobuf-json-charset
Closed

sharanggupta wants to merge 1 commit into
spring-projects:mainfrom
sharanggupta:protobuf-json-charset

Conversation

@sharanggupta

Copy link
Copy Markdown
Contributor

ProtobufJsonDecoder reads its input through an InputStreamReader created without a charset, so with the platform default, and ProtobufJsonEncoder always writes UTF-8. Both ignore the charset parameter of the media type, for example application/json;charset=ISO-8859-1, whereas ProtobufJsonFormatHttpMessageConverter and the messaging ProtobufMessageConverter use the declared charset and UTF-8 by default. On JDK 17, where the default charset is still platform-dependent, the decoder can also mis-read UTF-8 input that declares no charset.

This change makes both codecs use the charset of the MIME type, if any, and UTF-8 otherwise, as AbstractCharSequenceDecoder and CharSequenceEncoder do.

ProtobufJsonDecoderTests#decodeToMonoWithCharset and ProtobufJsonEncoderTests#encodeWithCharset cover a non-ASCII value declared as ISO-8859-1; both fail without the change. The protobuf codec tests of spring-web and checkstyle pass.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 22, 2026
ProtobufJsonDecoder read its input through an InputStreamReader created
without a charset, so with the platform default, and ProtobufJsonEncoder
always wrote UTF-8; both ignored the charset parameter of the media
type. The servlet and messaging Protobuf converters use the declared
charset, and UTF-8 by default.

The codecs now do the same.

Signed-off-by: Sharang Gupta <sharang@sharanggupta.dev>
@sharanggupta
sharanggupta force-pushed the protobuf-json-charset branch from 4e3a02d to 4ffc156 Compare November 8, 2026 18:30
@bclozel

bclozel commented Nov 14, 2026

Copy link
Copy Markdown
Member

Thanks for the PR, but we won't be merging it.

JSON must be UTF-8 (per the official RFC 8259), and "application/json" defines no charset parameter. That's why ProtobufJsonEncoder always writes UTF-8 and the Jackson encoders refuse non-Unicode charsets. If we honored "charset=ISO-8859-1", our encoder would then emit non-compliant JSON.

If anything ProtobufJsonFormatHttpMessageConverter and others are not compliant and we should tighten this. I have opened #37683 and #37684

@bclozel bclozel closed this Nov 14, 2026
@bclozel bclozel added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: declined A suggestion or change that we don't feel we should currently apply

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants