Skip to content

Commit f7ceab9

Browse files
authored
fix: [REGAPIC] Fix repeated fields handling for query parameters (#989)
if a field is declared as repeated in proto then its getter's name is `get<FieldName>List` not `get<FieldName>` instead.
1 parent d22b966 commit f7ceab9

File tree

8 files changed

+30
-13
lines changed

8 files changed

+30
-13
lines changed

src/main/java/com/google/api/generator/gapic/composer/rest/HttpJsonServiceStubClassComposer.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,9 @@ private Expr createFieldsExtractorClassInstance(
907907
for (int i = 0; i < descendantFields.length; i++) {
908908
String currFieldName = descendantFields[i];
909909
String bindingFieldMethodName =
910-
String.format("get%s", JavaStyle.toUpperCamelCase(currFieldName));
910+
(i < descendantFields.length - 1 || !httpBindingFieldName.isRepeated())
911+
? String.format("get%s", JavaStyle.toUpperCamelCase(currFieldName))
912+
: String.format("get%sList", JavaStyle.toUpperCamelCase(currFieldName));
911913
requestFieldGetterExprBuilder =
912914
requestFieldGetterExprBuilder.setMethodName(bindingFieldMethodName);
913915

src/main/java/com/google/api/generator/gapic/model/HttpBindings.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ public abstract static class HttpBinding implements Comparable<HttpBinding> {
4040

4141
public abstract boolean isOptional();
4242

43+
public abstract boolean isRepeated();
44+
4345
@Nullable
4446
public abstract String valuePattern();
4547

46-
public static HttpBinding create(String name, boolean isOptional, String valuePattern) {
48+
public static HttpBinding create(
49+
String name, boolean isOptional, boolean isRepeated, String valuePattern) {
4750
return new AutoValue_HttpBindings_HttpBinding(
48-
name, JavaStyle.toLowerCamelCase(name), isOptional, valuePattern);
51+
name, JavaStyle.toLowerCamelCase(name), isOptional, isRepeated, valuePattern);
4952
}
5053

5154
// Do not forget to keep it in sync with equals() implementation.

src/main/java/com/google/api/generator/gapic/protoparser/HttpRuleParser.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private static Set<HttpBinding> validateAndConstructHttpBindings(
128128
patternSampleValues != null ? patternSampleValues.get(paramName) : null;
129129
String[] subFields = paramName.split("\\.");
130130
if (inputMessage == null) {
131-
httpBindings.add(HttpBinding.create(paramName, false, patternSampleValue));
131+
httpBindings.add(HttpBinding.create(paramName, false, false, patternSampleValue));
132132
continue;
133133
}
134134
Message nestedMessage = inputMessage;
@@ -150,7 +150,8 @@ private static Set<HttpBinding> validateAndConstructHttpBindings(
150150
}
151151
Field field = nestedMessage.fieldMap().get(subFieldName);
152152
httpBindings.add(
153-
HttpBinding.create(paramName, field.isProto3Optional(), patternSampleValue));
153+
HttpBinding.create(
154+
paramName, field.isProto3Optional(), field.isRepeated(), patternSampleValue));
154155
}
155156
}
156157
}

src/test/java/com/google/api/generator/gapic/composer/grpcrest/goldens/EchoClient.golden

+4-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,10 @@ public class EchoClient implements BackgroundResource {
407407
* // It may require modifications to work in your environment.
408408
* try (EchoClient echoClient = EchoClient.create()) {
409409
* ExpandRequest request =
410-
* ExpandRequest.newBuilder().setContent("content951530617").setInfo("info3237038").build();
410+
* ExpandRequest.newBuilder()
411+
* .setContent("content951530617")
412+
* .addAllInfo(new ArrayList<String>())
413+
* .build();
411414
* ServerStream<EchoResponse> stream = echoClient.expandCallable().call(request);
412415
* for (EchoResponse response : stream) {
413416
* // Do something when a response is received.

src/test/java/com/google/api/generator/gapic/composer/grpcrest/goldens/EchoClientTest.golden

+9-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.google.protobuf.Timestamp;
2323
import com.google.rpc.Status;
2424
import io.grpc.StatusRuntimeException;
2525
import java.io.IOException;
26+
import java.util.ArrayList;
2627
import java.util.Arrays;
2728
import java.util.List;
2829
import java.util.UUID;
@@ -400,7 +401,10 @@ public class EchoClientTest {
400401
.build();
401402
mockEcho.addResponse(expectedResponse);
402403
ExpandRequest request =
403-
ExpandRequest.newBuilder().setContent("content951530617").setInfo("info3237038").build();
404+
ExpandRequest.newBuilder()
405+
.setContent("content951530617")
406+
.addAllInfo(new ArrayList<String>())
407+
.build();
404408

405409
MockStreamObserver<EchoResponse> responseObserver = new MockStreamObserver<>();
406410

@@ -417,7 +421,10 @@ public class EchoClientTest {
417421
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
418422
mockEcho.addException(exception);
419423
ExpandRequest request =
420-
ExpandRequest.newBuilder().setContent("content951530617").setInfo("info3237038").build();
424+
ExpandRequest.newBuilder()
425+
.setContent("content951530617")
426+
.addAllInfo(new ArrayList<String>())
427+
.build();
421428

422429
MockStreamObserver<EchoResponse> responseObserver = new MockStreamObserver<>();
423430

src/test/java/com/google/api/generator/gapic/composer/grpcrest/goldens/HttpJsonEchoStub.golden

+3-2
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,12 @@ public class HttpJsonEchoStub extends EchoStub {
108108
Map<String, List<String>> fields = new HashMap<>();
109109
ProtoRestSerializer<ExpandRequest> serializer =
110110
ProtoRestSerializer.create();
111+
serializer.putQueryParam(fields, "content", request.getContent());
112+
serializer.putQueryParam(fields, "info", request.getInfoList());
111113
return fields;
112114
})
113115
.setRequestBodyExtractor(
114-
request ->
115-
ProtoRestSerializer.create().toBody("*", request.toBuilder().build()))
116+
request -> ProtoRestSerializer.create().toBody("error", request.getError()))
116117
.build())
117118
.setResponseParser(
118119
ProtoMessageResponseParser.<EchoResponse>newBuilder()

src/test/java/com/google/api/generator/gapic/model/MethodTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class MethodTest {
3333
.build();
3434
private static final HttpBindings HTTP_BINDINGS =
3535
HttpBindings.builder()
36-
.setPathParameters(ImmutableSet.of(HttpBinding.create("table", true, "")))
36+
.setPathParameters(ImmutableSet.of(HttpBinding.create("table", true, false, "")))
3737
.setPattern("/pattern/test")
3838
.setIsAsteriskBody(false)
3939
.setHttpVerb(HttpVerb.GET)

src/test/proto/echo_grpcrest.proto

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ service Echo {
6565
rpc Expand(ExpandRequest) returns (stream EchoResponse) {
6666
option (google.api.http) = {
6767
post: "/v1beta1/echo:expand"
68-
body: "*"
68+
body: "error"
6969
};
7070
option (google.api.method_signature) = "content,error";
7171
}
@@ -202,7 +202,7 @@ message ExpandRequest {
202202
// The error that is thrown after all words are sent on the stream.
203203
google.rpc.Status error = 2;
204204

205-
string info = 3;
205+
repeated string info = 3;
206206
}
207207

208208
// The request for the PagedExpand method.

0 commit comments

Comments
 (0)