Spring-framework: StringHttpMessageConverter lost a fix that assumed charset UTF-8 for "application/json"

Created on 3 Dec 2019  路  6Comments  路  Source: spring-projects/spring-framework

With #22100 (release 5.1.4) the StringHttpMessageConverter used charset UTF-8 for application/json, which is correct. However, the change to StringHttpMessageConverter.java was undone with #22954 (release 5.2.0).

22954 aims to remove the charset parameter from Spring MVC JSON content type. But the lines dropped from StringHttpMessageConverter.java do not add a charset parameter to a content type. Instead they define the charset which will be used in interpreting messages. Without the fix from #22100 the charset for JSON messages defaults to ISO_8859_1, which is not correct and would require to set the charset explicitly.

web regression

Most helpful comment

Indeed, that should be the outcome. I'll add such tests and ensure they pass. Probably getDefaultCharset() could be overridden to return null for application/json if the default hasn't been set explicitly (i.e. is same as DEFAULT_CHARSET).

All 6 comments

That seems like a regression indeed. I can't see why https://github.com/spring-projects/spring-framework/commit/bc205e0dbf4b79e9dd2f3527ddfa9fb5a117cc67 dropped the charset defaulting to UTF-8 for JSON in StringHttpMessgageConverter. That isn't actually added to a header. @sdeleuze do you agree?

Tentatively scheduled for 5.2.3.

@rstoyanchev For the reading side, I agree and this is confirmed by an additional StringHttpMessageConverterTests#readJSON test I wrote. But while writing a StringHttpMessageConverterTests#writeJSON one, I figured out that we need potentially to take care about the writing side as well, since currently I think application/json is processed as application/json;charset=ISO-8859-1 due to the fact that StringHttpMessageConverter default charset is StandardCharsets.ISO_8859_1. And if we modify something here, we need to make sure the resulting content type header is application/json not application/json;charset=UTF-8 (implicit UTF-8 encoding).

Any thoughts?

Indeed, that should be the outcome. I'll add such tests and ensure they pass. Probably getDefaultCharset() could be overridden to return null for application/json if the default hasn't been set explicitly (i.e. is same as DEFAULT_CHARSET).

@sdeleuze note that the addition of charset=ISO-8859-1 when writing JSON was existing behavior that has always been there. I've changed it for 5.2.3 but for 5.1.x I will only backport the reading side change which is the regression reported here.

This was actually introduced in 5.2.0, so no need to backport. The reading side is already the same in 5.1.x.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sdeleuze picture sdeleuze  路  3Comments

spring-projects-issues picture spring-projects-issues  路  5Comments

manueljordan picture manueljordan  路  3Comments

spring-projects-issues picture spring-projects-issues  路  5Comments

spring-projects-issues picture spring-projects-issues  路  5Comments