Skip to content

Commit c4dfb48

Browse files
authored
fix: default application/json charset to utf-8 (#1305)
Fixes #1102
1 parent 62818cf commit c4dfb48

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,17 @@ public String parseAsString() throws IOException {
512512
* @since 1.10
513513
*/
514514
public Charset getContentCharset() {
515-
return mediaType == null || mediaType.getCharsetParameter() == null
516-
? StandardCharsets.ISO_8859_1
517-
: mediaType.getCharsetParameter();
515+
if (mediaType != null) {
516+
// use specified charset parameter from content/type header if available
517+
if (mediaType.getCharsetParameter() != null) {
518+
return mediaType.getCharsetParameter();
519+
}
520+
// fallback to well-known charsets
521+
if ("application".equals(mediaType.getType()) && "json".equals(mediaType.getSubType())) {
522+
// https://tools.ietf.org/html/rfc4627 - JSON must be encoded with UTF-8
523+
return StandardCharsets.UTF_8;
524+
}
525+
}
526+
return StandardCharsets.ISO_8859_1;
518527
}
519528
}

google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ public void testParseAsString_none() throws Exception {
5858

5959
private static final String SAMPLE = "123\u05D9\u05e0\u05D9\u05D1";
6060
private static final String SAMPLE2 = "123abc";
61+
private static final String JSON_SAMPLE = "{\"foo\": \"ßar\"}";
6162
private static final String VALID_CONTENT_TYPE = "text/plain";
6263
private static final String VALID_CONTENT_TYPE_WITH_PARAMS =
6364
"application/vnd.com.google.datastore.entity+json; charset=utf-8; version=v1; q=0.9";
6465
private static final String INVALID_CONTENT_TYPE = "!!!invalid!!!";
66+
private static final String JSON_CONTENT_TYPE = "application/json";
6567

6668
public void testParseAsString_utf8() throws Exception {
6769
HttpTransport transport =
@@ -83,6 +85,7 @@ public LowLevelHttpResponse execute() throws IOException {
8385
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
8486
HttpResponse response = request.execute();
8587
assertEquals(SAMPLE, response.parseAsString());
88+
assertEquals("UTF-8", response.getContentCharset().name());
8689
}
8790

8891
public void testParseAsString_noContentType() throws Exception {
@@ -104,6 +107,7 @@ public LowLevelHttpResponse execute() throws IOException {
104107
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
105108
HttpResponse response = request.execute();
106109
assertEquals(SAMPLE2, response.parseAsString());
110+
assertEquals("ISO-8859-1", response.getContentCharset().name());
107111
}
108112

109113
public void testParseAsString_validContentType() throws Exception {
@@ -129,6 +133,7 @@ public LowLevelHttpResponse execute() throws IOException {
129133
assertEquals(SAMPLE2, response.parseAsString());
130134
assertEquals(VALID_CONTENT_TYPE, response.getContentType());
131135
assertNotNull(response.getMediaType());
136+
assertEquals("ISO-8859-1", response.getContentCharset().name());
132137
}
133138

134139
public void testParseAsString_validContentTypeWithParams() throws Exception {
@@ -154,6 +159,7 @@ public LowLevelHttpResponse execute() throws IOException {
154159
assertEquals(SAMPLE2, response.parseAsString());
155160
assertEquals(VALID_CONTENT_TYPE_WITH_PARAMS, response.getContentType());
156161
assertNotNull(response.getMediaType());
162+
assertEquals("UTF-8", response.getContentCharset().name());
157163
}
158164

159165
public void testParseAsString_invalidContentType() throws Exception {
@@ -179,6 +185,32 @@ public LowLevelHttpResponse execute() throws IOException {
179185
assertEquals(SAMPLE2, response.parseAsString());
180186
assertEquals(INVALID_CONTENT_TYPE, response.getContentType());
181187
assertNull(response.getMediaType());
188+
assertEquals("ISO-8859-1", response.getContentCharset().name());
189+
}
190+
191+
public void testParseAsString_jsonContentType() throws IOException {
192+
HttpTransport transport =
193+
new MockHttpTransport() {
194+
@Override
195+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
196+
return new MockLowLevelHttpRequest() {
197+
@Override
198+
public LowLevelHttpResponse execute() throws IOException {
199+
MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
200+
result.setContent(JSON_SAMPLE);
201+
result.setContentType(JSON_CONTENT_TYPE);
202+
return result;
203+
}
204+
};
205+
}
206+
};
207+
HttpRequest request =
208+
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
209+
210+
HttpResponse response = request.execute();
211+
assertEquals(JSON_SAMPLE, response.parseAsString());
212+
assertEquals(JSON_CONTENT_TYPE, response.getContentType());
213+
assertEquals("UTF-8", response.getContentCharset().name());
182214
}
183215

184216
public void testStatusCode_negative_dontThrowException() throws Exception {

0 commit comments

Comments
 (0)