From ce58c181691d0be826634d1bd8373f07c1295156 Mon Sep 17 00:00:00 2001 From: Vadym Matsishevskyi <25311427+vam-google@users.noreply.github.com> Date: Wed, 18 May 2022 18:31:31 -0700 Subject: [PATCH] feat: Add support for additional_bindings (#993) This PR depends on the corresponding one in gax: https://github.com/googleapis/gax-java/pull/1680 --- .../HttpJsonServiceStubClassComposer.java | 10 ++++++++++ .../generator/gapic/model/HttpBindings.java | 20 +++++++++++++++++-- .../gapic/protoparser/HttpRuleParser.java | 10 ++++++++-- .../gapic/protoparser/PatternParser.java | 2 +- .../gapic/protoparser/RoutingRuleParser.java | 2 +- .../grpcrest/goldens/HttpJsonEchoStub.golden | 3 +++ .../api/generator/gapic/model/MethodTest.java | 2 ++ .../gapic/protoparser/PatternParserTest.java | 6 +++--- src/test/proto/echo_grpcrest.proto | 8 ++++++++ 9 files changed, 54 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/rest/HttpJsonServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/rest/HttpJsonServiceStubClassComposer.java index 7cf0532a06..839b4d27b0 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/rest/HttpJsonServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/rest/HttpJsonServiceStubClassComposer.java @@ -348,6 +348,16 @@ private List getRequestFormatterExpr(Method protoMethod) { "putPathParam"))) .apply(expr); + if (!protoMethod.httpBindings().lowerCamelAdditionalPatterns().isEmpty()) { + expr = + methodMaker + .apply( + "setAdditionalPaths", + protoMethod.httpBindings().lowerCamelAdditionalPatterns().stream() + .map(a -> ValueExpr.withValue(StringObjectValue.withValue(a))) + .collect(Collectors.toList())) + .apply(expr); + } TypeNode fieldsVarGenericType = TypeNode.withReference( ConcreteReference.builder() diff --git a/src/main/java/com/google/api/generator/gapic/model/HttpBindings.java b/src/main/java/com/google/api/generator/gapic/model/HttpBindings.java index 7126bfc3ba..61eea88504 100644 --- a/src/main/java/com/google/api/generator/gapic/model/HttpBindings.java +++ b/src/main/java/com/google/api/generator/gapic/model/HttpBindings.java @@ -18,8 +18,10 @@ import com.google.auto.value.AutoValue; import com.google.common.collect.ImmutableSet; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import javax.annotation.Nullable; @AutoValue @@ -63,6 +65,8 @@ public int compareTo(HttpBinding o) { public abstract String pattern(); + public abstract List additionalPatterns(); + public abstract Set pathParameters(); public abstract Set queryParameters(); @@ -84,8 +88,18 @@ public static HttpBindings.Builder builder() { // in .proto file: "/global/instanceTemplates/{instance_template=*}" // in .java file: "/global/instanceTemplates/{instanceTemplate=*}" public String lowerCamelPattern() { - String lowerCamelPattern = pattern(); - for (HttpBinding pathParam : pathParameters()) { + return lowerCamelPattern(pattern(), pathParameters()); + } + + public List lowerCamelAdditionalPatterns() { + return additionalPatterns().stream() + .map(a -> lowerCamelPattern(a, pathParameters())) + .collect(Collectors.toList()); + } + + private static String lowerCamelPattern(String originalPattern, Set pathParameters) { + String lowerCamelPattern = originalPattern; + for (HttpBinding pathParam : pathParameters) { lowerCamelPattern = lowerCamelPattern.replaceAll( "\\{" + pathParam.name(), "{" + JavaStyle.toLowerCamelCase(pathParam.name())); @@ -107,6 +121,8 @@ public abstract static class Builder { public abstract HttpBindings.Builder setPattern(String pattern); + public abstract HttpBindings.Builder setAdditionalPatterns(List additionalPatterns); + abstract String pattern(); public abstract HttpBindings.Builder setPathParameters(Set pathParameters); diff --git a/src/main/java/com/google/api/generator/gapic/protoparser/HttpRuleParser.java b/src/main/java/com/google/api/generator/gapic/protoparser/HttpRuleParser.java index 86012e9bcd..bead11e082 100644 --- a/src/main/java/com/google/api/generator/gapic/protoparser/HttpRuleParser.java +++ b/src/main/java/com/google/api/generator/gapic/protoparser/HttpRuleParser.java @@ -36,6 +36,7 @@ import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; public class HttpRuleParser { private static final String ASTERISK = "*"; @@ -68,11 +69,12 @@ private static HttpBindings parseHttpRuleHelper( // Get pattern. String pattern = getHttpVerbPattern(httpRule); ImmutableSet.Builder bindingsBuilder = ImmutableSet.builder(); - bindingsBuilder.addAll(PatternParser.getPattenBindings(pattern)); + bindingsBuilder.addAll(PatternParser.getPatternBindings(pattern)); if (httpRule.getAdditionalBindingsCount() > 0) { for (HttpRule additionalRule : httpRule.getAdditionalBindingsList()) { // TODO: save additional bindings path in HttpRuleBindings - bindingsBuilder.addAll(PatternParser.getPattenBindings(getHttpVerbPattern(additionalRule))); + bindingsBuilder.addAll( + PatternParser.getPatternBindings(getHttpVerbPattern(additionalRule))); } } @@ -104,6 +106,10 @@ private static HttpBindings parseHttpRuleHelper( return HttpBindings.builder() .setHttpVerb(HttpBindings.HttpVerb.valueOf(httpRule.getPatternCase().toString())) .setPattern(pattern) + .setAdditionalPatterns( + httpRule.getAdditionalBindingsList().stream() + .map(HttpRuleParser::getHttpVerbPattern) + .collect(Collectors.toList())) .setPathParameters( validateAndConstructHttpBindings( pathParamNames, message, messageTypes, patternSampleValues)) diff --git a/src/main/java/com/google/api/generator/gapic/protoparser/PatternParser.java b/src/main/java/com/google/api/generator/gapic/protoparser/PatternParser.java index 2a374c47af..61253eaa1b 100644 --- a/src/main/java/com/google/api/generator/gapic/protoparser/PatternParser.java +++ b/src/main/java/com/google/api/generator/gapic/protoparser/PatternParser.java @@ -22,7 +22,7 @@ public class PatternParser { // This method tries to parse all named segments from pattern and sort in natual order // e.g. /v1beta1/{table_name=tests/*}/{routing_id=instances/*}/** -> (routing_id, table_name) - public static Set getPattenBindings(String pattern) { + public static Set getPatternBindings(String pattern) { ImmutableSortedSet.Builder bindings = ImmutableSortedSet.naturalOrder(); if (pattern.isEmpty()) { return bindings.build(); diff --git a/src/main/java/com/google/api/generator/gapic/protoparser/RoutingRuleParser.java b/src/main/java/com/google/api/generator/gapic/protoparser/RoutingRuleParser.java index 23f8ed0741..33c34ff532 100644 --- a/src/main/java/com/google/api/generator/gapic/protoparser/RoutingRuleParser.java +++ b/src/main/java/com/google/api/generator/gapic/protoparser/RoutingRuleParser.java @@ -49,7 +49,7 @@ public static RoutingHeaderRule parse( key = fieldName; pathTemplate = String.format("{%s=**}", key); } else { - Set namedSegments = PatternParser.getPattenBindings(pathTemplate); + Set namedSegments = PatternParser.getPatternBindings(pathTemplate); Preconditions.checkArgument( namedSegments.size() == 1, String.format( diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpcrest/goldens/HttpJsonEchoStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpcrest/goldens/HttpJsonEchoStub.golden index 9dd1f47647..989800c321 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpcrest/goldens/HttpJsonEchoStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpcrest/goldens/HttpJsonEchoStub.golden @@ -302,6 +302,9 @@ public class HttpJsonEchoStub extends EchoStub { fields, "fooBar.name", request.getFooBar().getName()); return fields; }) + .setAdditionalPaths( + "/v1beta1/{fooBar.name=cats/*/dogs/*}/echo:foo", + "/v1beta1/{fooBar.name=turtles/*/parrots/*}/echo:foo") .setQueryParamsExtractor( request -> { Map> fields = new HashMap<>(); diff --git a/src/test/java/com/google/api/generator/gapic/model/MethodTest.java b/src/test/java/com/google/api/generator/gapic/model/MethodTest.java index 43b59c73fe..93fe4b8135 100644 --- a/src/test/java/com/google/api/generator/gapic/model/MethodTest.java +++ b/src/test/java/com/google/api/generator/gapic/model/MethodTest.java @@ -21,6 +21,7 @@ import com.google.api.generator.gapic.model.HttpBindings.HttpVerb; import com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam; import com.google.common.collect.ImmutableSet; +import java.util.Arrays; import org.junit.Test; public class MethodTest { @@ -35,6 +36,7 @@ public class MethodTest { HttpBindings.builder() .setPathParameters(ImmutableSet.of(HttpBinding.create("table", true, false, ""))) .setPattern("/pattern/test") + .setAdditionalPatterns(Arrays.asList("/extra_pattern/test", "/extra_pattern/hey")) .setIsAsteriskBody(false) .setHttpVerb(HttpVerb.GET) .build(); diff --git a/src/test/java/com/google/api/generator/gapic/protoparser/PatternParserTest.java b/src/test/java/com/google/api/generator/gapic/protoparser/PatternParserTest.java index 8415ca2b21..764c719af7 100644 --- a/src/test/java/com/google/api/generator/gapic/protoparser/PatternParserTest.java +++ b/src/test/java/com/google/api/generator/gapic/protoparser/PatternParserTest.java @@ -22,19 +22,19 @@ public class PatternParserTest { @Test public void getPattenBindings_shouldReturnEmptySetIfPatternIsEmpty() { - assertThat(PatternParser.getPattenBindings("")).isEmpty(); + assertThat(PatternParser.getPatternBindings("")).isEmpty(); } @Test public void getPattenBindings_shouldFilterOutUnboundVariables() { - Set actual = PatternParser.getPattenBindings("{routing_id=projects/*}/**"); + Set actual = PatternParser.getPatternBindings("{routing_id=projects/*}/**"); assertThat(actual).hasSize(1); } @Test public void getPattenBindings_shouldReturnBindingsInNatualOrder() { Set actual = - PatternParser.getPattenBindings("{routing_id=projects/*}/{name=instance/*}"); + PatternParser.getPatternBindings("{routing_id=projects/*}/{name=instance/*}"); assertThat(actual).containsExactly("name", "routing_id").inOrder(); } } diff --git a/src/test/proto/echo_grpcrest.proto b/src/test/proto/echo_grpcrest.proto index 21a62c575b..92389232e7 100644 --- a/src/test/proto/echo_grpcrest.proto +++ b/src/test/proto/echo_grpcrest.proto @@ -125,6 +125,14 @@ service Echo { option (google.api.http) = { post: "/v1beta1/{foo_bar.name=projects/*/foobars/*}/echo:foo" body: "*" + additional_bindings { + post: "/v1beta1/{foo_bar.name=cats/*/dogs/*}/echo:foo" + body: "*" + } + additional_bindings { + post: "/v1beta1/{foo_bar.name=turtles/*/parrots/*}/echo:foo" + body: "*" + } }; } }