Skip to content

Commit 79d986b

Browse files
authored
fix: Use UTF-8 as default charset for HttpJson requests (#1477)
Thank you for opening a Pull Request! For general contributing guidelines, please refer to [contributing guide](https://togithub.com/googleapis/gapic-generator-java/blob/main/CONTRIBUTING.md) Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/gapic-generator-java/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) Fixes #1437
1 parent 960067a commit 79d986b

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpRequestRunnable.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ HttpRequest createHttpRequest() throws IOException {
172172
jsonFactory.createJsonParser(requestBody).parse(tokenRequest);
173173
jsonHttpContent =
174174
new JsonHttpContent(jsonFactory, tokenRequest)
175-
.setMediaType((new HttpMediaType("application/json")));
175+
.setMediaType((new HttpMediaType("application/json; charset=utf-8")));
176176
} else {
177177
// Force underlying HTTP lib to set Content-Length header to avoid 411s.
178178
// See EmptyContent.java.

gax-java/gax-httpjson/src/test/java/com/google/api/gax/httpjson/HttpRequestRunnableTest.java

+65
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import com.google.longrunning.ListOperationsRequest;
3737
import com.google.protobuf.Empty;
3838
import com.google.protobuf.Field;
39+
import com.google.protobuf.util.JsonFormat;
40+
import java.io.ByteArrayOutputStream;
3941
import java.io.IOException;
4042
import java.util.Arrays;
4143
import java.util.HashMap;
@@ -177,4 +179,67 @@ public void testRequestUrlUnnormalizedPatch() throws IOException {
177179
Truth.assertThat(httpRequest.getRequestMethod()).isEqualTo("POST");
178180
Truth.assertThat(httpRequest.getHeaders().get("X-HTTP-Method-Override")).isEqualTo("PATCH");
179181
}
182+
183+
/*
184+
We use a separate RequestFormatter because formatting the body requests is what sets the charset to be UTF-8.
185+
The other tests above do not have a set a body request and instead send an EmptyContent (null Type/ CharSet)
186+
*/
187+
@Test
188+
public void testUnicodeValuesInBody() throws IOException {
189+
HttpRequestFormatter<Field> bodyRequestFormatter =
190+
ProtoMessageRequestFormatter.<Field>newBuilder()
191+
.setPath(
192+
"/name/{name=*}",
193+
request -> {
194+
Map<String, String> fields = new HashMap<>();
195+
ProtoRestSerializer<Field> serializer = ProtoRestSerializer.create();
196+
serializer.putPathParam(fields, "name", request.getName());
197+
return fields;
198+
})
199+
.setQueryParamsExtractor(request -> new HashMap<>())
200+
.setRequestBodyExtractor(
201+
request ->
202+
ProtoRestSerializer.create().toBody("*", request.toBuilder().build(), true))
203+
.build();
204+
205+
Field bodyRequestMessage =
206+
Field.newBuilder()
207+
.setName("feline ☺ → ←")
208+
.setNumber(2)
209+
.setDefaultValue("bird ☺ → ←")
210+
.setJsonName("mouse ☺ → ←")
211+
.setTypeUrl("small ☺ → ←")
212+
.build();
213+
214+
ApiMethodDescriptor<Field, Empty> methodDescriptor =
215+
ApiMethodDescriptor.<Field, Empty>newBuilder()
216+
.setFullMethodName("house.cat.get")
217+
.setHttpMethod("PUT")
218+
.setRequestFormatter(bodyRequestFormatter)
219+
.setResponseParser(responseParser)
220+
.build();
221+
222+
HttpRequestRunnable<Field, Empty> httpRequestRunnable =
223+
new HttpRequestRunnable<>(
224+
bodyRequestMessage,
225+
methodDescriptor,
226+
"www.googleapis.com/animals/v1/projects",
227+
HttpJsonCallOptions.newBuilder().build(),
228+
new MockHttpTransport(),
229+
HttpJsonMetadata.newBuilder().build(),
230+
(result) -> {});
231+
232+
HttpRequest httpRequest = httpRequestRunnable.createHttpRequest();
233+
Truth.assertThat(httpRequest.getContent().getType())
234+
.isEqualTo("application/json; charset=utf-8");
235+
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
236+
// writeTo() uses the Charset when writing to the stream
237+
httpRequest.getContent().writeTo(byteArrayOutputStream);
238+
String output = byteArrayOutputStream.toString();
239+
Field.Builder expectedBuilder = Field.newBuilder();
240+
JsonFormat.parser().merge(output, expectedBuilder);
241+
Field result = expectedBuilder.build();
242+
Truth.assertThat(result).isEqualTo(bodyRequestMessage);
243+
}
244+
}
180245
}

0 commit comments

Comments
 (0)