Skip to content

Commit 5b94d9b

Browse files
committed
Use UTF-8 for application/*+json
This commit makes sure that the StringHttpMessageConverter reads input with "application/*+json" as Content-Type with the UTF-8 character set. Closes gh-25328
1 parent b2a4d1c commit 5b94d9b

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

spring-web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
*/
4444
public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {
4545

46+
private static final MediaType APPLICATION_PLUS_JSON = new MediaType("application", "*+json");
47+
4648
/**
4749
* The default charset used by the converter.
4850
*/
@@ -104,7 +106,9 @@ protected Long getContentLength(String str, @Nullable MediaType contentType) {
104106
@Override
105107
protected void addDefaultHeaders(HttpHeaders headers, String s, @Nullable MediaType type) throws IOException {
106108
if (headers.getContentType() == null ) {
107-
if (type != null && type.isConcrete() && type.isCompatibleWith(MediaType.APPLICATION_JSON)) {
109+
if (type != null && type.isConcrete() &&
110+
(type.isCompatibleWith(MediaType.APPLICATION_JSON) ||
111+
type.isCompatibleWith(APPLICATION_PLUS_JSON))) {
108112
// Prevent charset parameter for JSON..
109113
headers.setContentType(type);
110114
}
@@ -142,7 +146,9 @@ private Charset getContentTypeCharset(@Nullable MediaType contentType) {
142146
if (contentType != null && contentType.getCharset() != null) {
143147
return contentType.getCharset();
144148
}
145-
else if (contentType != null && contentType.isCompatibleWith(MediaType.APPLICATION_JSON)) {
149+
else if (contentType != null &&
150+
(contentType.isCompatibleWith(MediaType.APPLICATION_JSON) ||
151+
contentType.isCompatibleWith(APPLICATION_PLUS_JSON))) {
146152
// Matching to AbstractJackson2HttpMessageConverter#DEFAULT_CHARSET
147153
return StandardCharsets.UTF_8;
148154
}

spring-web/src/test/java/org/springframework/http/converter/StringHttpMessageConverterTests.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ public void readJson() throws IOException {
8080
assertThat(result).as("Invalid result").isEqualTo(body);
8181
}
8282

83+
@Test // gh-25328
84+
public void readJsonApi() throws IOException {
85+
String body = "{\"result\":\"\u0414\u0410\"}";
86+
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
87+
inputMessage.getHeaders().setContentType(new MediaType("application", "vnd.api.v1+json"));
88+
String result = this.converter.read(String.class, inputMessage);
89+
90+
assertThat(result).as("Invalid result").isEqualTo(body);
91+
}
92+
8393
@Test
8494
public void writeDefaultCharset() throws IOException {
8595
String body = "H\u00e9llo W\u00f6rld";
@@ -94,7 +104,7 @@ public void writeDefaultCharset() throws IOException {
94104

95105
@Test // gh-24123
96106
public void writeJson() throws IOException {
97-
String body = "{\"foo\":\"bar\"}";
107+
String body = "{\"føø\":\"bår\"}";
98108
this.converter.write(body, MediaType.APPLICATION_JSON, this.outputMessage);
99109

100110
HttpHeaders headers = this.outputMessage.getHeaders();
@@ -104,6 +114,19 @@ public void writeJson() throws IOException {
104114
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
105115
}
106116

117+
@Test // gh-25328
118+
public void writeJsonApi() throws IOException {
119+
String body = "{\"føø\":\"bår\"}";
120+
MediaType contentType = new MediaType("application", "vnd.api.v1+json");
121+
this.converter.write(body, contentType, this.outputMessage);
122+
123+
HttpHeaders headers = this.outputMessage.getHeaders();
124+
assertThat(this.outputMessage.getBodyAsString(StandardCharsets.UTF_8)).isEqualTo(body);
125+
assertThat(headers.getContentType()).isEqualTo(contentType);
126+
assertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.UTF_8).length);
127+
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
128+
}
129+
107130
@Test
108131
public void writeUTF8() throws IOException {
109132
String body = "H\u00e9llo W\u00f6rld";

0 commit comments

Comments
 (0)