Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit 0ab76a4

Browse files
committed
test(serializer): add test for complex msg obj
1 parent a61065b commit 0ab76a4

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

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

Lines changed: 0 additions & 5 deletions
This file was deleted.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
@BetaApi
6464
public class ProtoRestSerializer<RequestT extends Message> {
6565
private final TypeRegistry registry;
66+
67+
// well-known values obtained from
68+
// https://github.com/googleapis/gapic-showcase/blob/fe414784c18878d704b884348d84c68fd6b87466/util/genrest/resttools/populatefield.go#L27
6669
private static final Set<Class<GeneratedMessageV3>> jsonSerializableMessages = new HashSet(
6770
Arrays.asList(
6871
com.google.protobuf.BoolValue.class,
@@ -227,7 +230,8 @@ public String toBody(String fieldName, RequestT fieldValue, boolean numericEnum)
227230
* @param fieldValue a field value to serialize
228231
*/
229232
public String toQueryParamValue(Object fieldValue) {
230-
if (fieldValue instanceof GeneratedMessageV3) {
233+
// This will match with message types that are serializable (e.g. FieldMask)
234+
if (fieldValue instanceof GeneratedMessageV3 && !isNonSerializableMessageValue(fieldValue)) {
231235
return toJson(((GeneratedMessageV3) fieldValue).toBuilder(), false)
232236
.replaceAll("^\"", "")
233237
.replaceAll("\"$", "");

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
package com.google.api.gax.httpjson;
3232

3333
import com.google.common.truth.Truth;
34+
import com.google.longrunning.Operation;
35+
import com.google.protobuf.Any;
36+
import com.google.protobuf.ByteString;
3437
import com.google.protobuf.Duration;
3538
import com.google.protobuf.Field;
3639
import com.google.protobuf.Field.Cardinality;
@@ -39,8 +42,10 @@
3942
import com.google.protobuf.Int32Value;
4043
import com.google.protobuf.Option;
4144
import com.google.protobuf.Timestamp;
45+
import com.google.rpc.Status;
4246
import java.io.IOException;
4347
import java.io.StringReader;
48+
import java.nio.charset.Charset;
4449
import java.util.Arrays;
4550
import java.util.HashMap;
4651
import java.util.List;
@@ -183,6 +188,16 @@ public void putQueryParam() {
183188
fields, "optName8", FieldMask.newBuilder().addPaths("a.b").addPaths("c.d").build());
184189
requestSerializer.putQueryParam(fields, "optName9", Int32Value.of(1));
185190
requestSerializer.putQueryParam(fields, "optName10", FloatValue.of(1.1f));
191+
com.google.longrunning.Operation operation = Operation.newBuilder()
192+
.setDone(true)
193+
.setError(Status.newBuilder()
194+
.addDetails(Any.newBuilder().setValue(ByteString.copyFrom("error-1",
195+
Charset.defaultCharset())).build())
196+
.addDetails(Any.newBuilder().setValue(ByteString.copyFrom("error-2",
197+
Charset.defaultCharset())).build()))
198+
.setName("test")
199+
.build();
200+
requestSerializer.putQueryParam(fields, "optName11", operation);
186201

187202
Map<String, List<String>> expectedFields = new HashMap<>();
188203
expectedFields.put("optName1", Arrays.asList("1"));
@@ -195,6 +210,9 @@ public void putQueryParam() {
195210
expectedFields.put("optName8", Arrays.asList("a.b,c.d"));
196211
expectedFields.put("optName9", Arrays.asList("1"));
197212
expectedFields.put("optName10", Arrays.asList("1.1"));
213+
expectedFields.put("optName11.name", Arrays.asList("test"));
214+
expectedFields.put("optName11.done", Arrays.asList("true"));
215+
expectedFields.put("optName11.error.details.value", Arrays.asList("error-1", "error-2"));
198216

199217
Truth.assertThat(fields).isEqualTo(expectedFields);
200218
}

0 commit comments

Comments
 (0)