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

Commit

Permalink
fix(queryparam): use numeric value for root enums
Browse files Browse the repository at this point in the history
enums passed as root object to putQueryParam will now be serialized as
their numeric value
  • Loading branch information
diegomarquezp committed Sep 9, 2022
1 parent 2edd120 commit 9095fab
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;
import com.google.protobuf.MessageOrBuilder;
import com.google.protobuf.ProtocolMessageEnum;
import com.google.protobuf.TypeRegistry;
import com.google.protobuf.util.JsonFormat;
import com.google.protobuf.util.JsonFormat.Printer;
Expand Down Expand Up @@ -157,6 +158,8 @@ public void putQueryParam(Map<String, List<String>> fields, String fieldName, Ob
String json = toJson(((Message) fieldValueItem).toBuilder(), true);
JsonElement parsed = JsonParser.parseString(json);
putDecomposedMessageQueryParam(fields, fieldName, parsed);
} else if (fieldValueItem instanceof ProtocolMessageEnum){
paramValueList.add(String.valueOf(((ProtocolMessageEnum) fieldValueItem).getNumber()));
} else {
paramValueList.add(String.valueOf(fieldValueItem));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
package com.google.api.gax.httpjson;

import com.google.common.truth.Truth;
import com.google.protobuf.DescriptorProtos;
import com.google.protobuf.Duration;
import com.google.protobuf.Field;
import com.google.protobuf.Field.Cardinality;
import com.google.protobuf.FieldMask;
import com.google.protobuf.FloatValue;
import com.google.protobuf.Int32Value;
import com.google.protobuf.Option;
import com.google.protobuf.ProtocolMessageEnum;
import com.google.protobuf.Timestamp;
import java.io.IOException;
import java.io.StringReader;
Expand Down Expand Up @@ -168,38 +170,48 @@ public void putPathParam() {
}

@Test
public void putQueryParam() {
public void putQueryParamPrimitive() {
Map<String, List<String>> fields = new HashMap<>();
requestSerializer.putQueryParam(fields, "optName1", 1);
requestSerializer.putQueryParam(fields, "optName2", 0);
requestSerializer.putQueryParam(fields, "optName3", "three");
requestSerializer.putQueryParam(fields, "optName4", "");
requestSerializer.putQueryParam(fields, "optName5", Arrays.asList("four", "five"));
requestSerializer.putQueryParam(
fields, "optName6", Duration.newBuilder().setSeconds(1).setNanos(1).build());
requestSerializer.putQueryParam(
fields, "optName7", Timestamp.newBuilder().setSeconds(1).setNanos(1).build());
requestSerializer.putQueryParam(
fields, "optName8", FieldMask.newBuilder().addPaths("a.b").addPaths("c.d").build());
requestSerializer.putQueryParam(fields, "optName9", Int32Value.of(1));
requestSerializer.putQueryParam(fields, "optName10", FloatValue.of(1.1f));
requestSerializer.putQueryParam(fields, "optName11", field);

Map<String, List<String>> expectedFields = new HashMap<>();
expectedFields.put("optName1", Arrays.asList("1"));
expectedFields.put("optName2", Arrays.asList("0"));
expectedFields.put("optName3", Arrays.asList("three"));
expectedFields.put("optName4", Arrays.asList(""));
expectedFields.put("optName5", Arrays.asList("four", "five"));
expectedFields.put("optName6", Arrays.asList("1.000000001s"));
expectedFields.put("optName7", Arrays.asList("1970-01-01T00:00:01.000000001Z"));
expectedFields.put("optName8", Arrays.asList("a.b,c.d"));
expectedFields.put("optName9", Arrays.asList("1"));
expectedFields.put("optName10", Arrays.asList("1.1"));
expectedFields.put("optName11.name", Arrays.asList("field_name1"));
expectedFields.put("optName11.number", Arrays.asList("2"));
expectedFields.put("optName11.options.name", Arrays.asList("opt_name1", "opt_name2"));
expectedFields.put("optName11.cardinality", Arrays.asList("1"));

Truth.assertThat(fields).isEqualTo(expectedFields);
}
@Test
public void putQueryParamComplexObject() {
Map<String, List<String>> fields = new HashMap<>();
requestSerializer.putQueryParam(
fields, "optName1", Duration.newBuilder().setSeconds(1).setNanos(1).build());
requestSerializer.putQueryParam(
fields, "optName2", Timestamp.newBuilder().setSeconds(1).setNanos(1).build());
requestSerializer.putQueryParam(
fields, "optName3", FieldMask.newBuilder().addPaths("a.b").addPaths("c.d").build());
requestSerializer.putQueryParam(fields, "optName4", Int32Value.of(1));
requestSerializer.putQueryParam(fields, "optName5", FloatValue.of(1.1f));
requestSerializer.putQueryParam(fields, "optName6", field);
requestSerializer.putQueryParam(fields, "optName7", Arrays.asList(Cardinality.CARDINALITY_REPEATED));

Map<String, List<String>> expectedFields = new HashMap<>();
expectedFields.put("optName1", Arrays.asList("1.000000001s"));
expectedFields.put("optName2", Arrays.asList("1970-01-01T00:00:01.000000001Z"));
expectedFields.put("optName3", Arrays.asList("a.b,c.d"));
expectedFields.put("optName4", Arrays.asList("1"));
expectedFields.put("optName5", Arrays.asList("1.1"));
expectedFields.put("optName6.name", Arrays.asList("field_name1"));
expectedFields.put("optName6.number", Arrays.asList("2"));
expectedFields.put("optName6.options.name", Arrays.asList("opt_name1", "opt_name2"));
expectedFields.put("optName6.cardinality", Arrays.asList("1"));
expectedFields.put("optName7", Arrays.asList("3"));

Truth.assertThat(fields).isEqualTo(expectedFields);
}
Expand Down

0 comments on commit 9095fab

Please sign in to comment.