Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Check additional_binding's custom FieldsExtractor #1565

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@
import com.google.api.generator.gapic.composer.common.AbstractTransportServiceStubClassComposer;
import com.google.api.generator.gapic.composer.store.TypeStore;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.HttpBindings;
import com.google.api.generator.gapic.model.HttpBindings.HttpBinding;
import com.google.api.generator.gapic.model.Message;
import com.google.api.generator.gapic.model.Method;
import com.google.api.generator.gapic.model.OperationResponse;
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.utils.JavaStyle;
import com.google.api.pathtemplate.PathTemplate;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -107,14 +109,15 @@ private static TypeStore createStaticTypes() {
ApiMethodDescriptor.class,
ArrayList.class,
FieldsExtractor.class,
ImmutableMap.class,
InternalApi.class,
HashMap.class,
HttpJsonCallSettings.class,
HttpJsonOperationSnapshot.class,
HttpJsonStubCallableFactory.class,
HttpRule.class,
Map.class,
ImmutableMap.class,
PathTemplate.class,
ProtoMessageRequestFormatter.class,
ProtoMessageResponseParser.class,
ProtoRestSerializer.class,
Expand Down Expand Up @@ -362,14 +365,59 @@ private List<Expr> getRequestFormatterExpr(Method protoMethod, boolean restNumer
restNumericEnumsEnabled)))
.apply(expr);

if (!protoMethod.httpBindings().lowerCamelAdditionalPatterns().isEmpty()) {
// If an RPC defines additional bindings, create a map between the PathTemplate
// and the FieldExtractor for the PathTemplate
if (!protoMethod.httpBindings().additionalBindings().isEmpty()) {
Expr additionalBindingsExtractorMapExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(FIXED_REST_TYPESTORE.get(ImmutableMap.class.getSimpleName()))
.setMethodName("builder")
.setGenerics(
Arrays.asList(
FIXED_REST_TYPESTORE.get(PathTemplate.class.getSimpleName()).reference(),
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(FieldsExtractor.class)
.setGenerics(
protoMethod.inputType().reference(),
extractorVarType.reference())
.build())
.reference()))
.build();
for (HttpBindings httpBindings : protoMethod.httpBindings().additionalBindings()) {
additionalBindingsExtractorMapExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(additionalBindingsExtractorMapExpr)
.setMethodName("put")
.setArguments(
Arrays.asList(
MethodInvocationExpr.builder()
.setStaticReferenceType(
FIXED_REST_TYPESTORE.get(PathTemplate.class.getSimpleName()))
.setMethodName("create")
.setArguments(
ValueExpr.withValue(
StringObjectValue.withValue(httpBindings.lowerCamelPattern())))
.build(),
createFieldsExtractorClassInstance(
protoMethod,
extractorVarType,
httpBindings.pathParameters(),
"putPathParam",
restNumericEnumsEnabled)))
.build();
}
additionalBindingsExtractorMapExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(additionalBindingsExtractorMapExpr)
.setMethodName("build")
.setReturnType(FIXED_REST_TYPESTORE.get(ImmutableMap.class.getSimpleName()))
.build();
expr =
methodMaker
.apply(
"setAdditionalPaths",
protoMethod.httpBindings().lowerCamelAdditionalPatterns().stream()
.map(a -> ValueExpr.withValue(StringObjectValue.withValue(a)))
.collect(Collectors.toList()))
"setAdditionalPathsExtractorMap",
Arrays.asList(additionalBindingsExtractorMapExpr))
.apply(expr);
}
TypeNode fieldsVarGenericType =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.google.api.generator.gapic.utils.JavaStyle;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -108,11 +109,14 @@ public int compareTo(HttpBinding o) {

public abstract boolean isAsteriskBody();

public abstract List<HttpBindings> additionalBindings();

public static HttpBindings.Builder builder() {
return new AutoValue_HttpBindings.Builder()
.setPathParameters(ImmutableSet.of())
.setQueryParameters(ImmutableSet.of())
.setBodyParameters(ImmutableSet.of());
.setBodyParameters(ImmutableSet.of())
.setAdditionalBindings(ImmutableList.of());
}

// Protobuf fields and template patterns follow snake_case style. When translated into actual Java
Expand Down Expand Up @@ -166,6 +170,8 @@ public abstract static class Builder {

public abstract HttpBindings.Builder setIsAsteriskBody(boolean asteriskBody);

public abstract HttpBindings.Builder setAdditionalBindings(List<HttpBindings> httpBindings);

public abstract HttpBindings autoBuild();

public HttpBindings build() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ private static HttpBindings parseHttpRuleHelper(
httpRule.getAdditionalBindingsList().stream()
.map(HttpRuleParser::getHttpVerbPattern)
.collect(Collectors.toList()))
.setAdditionalBindings(
httpRule.getAdditionalBindingsList().stream()
.map(x -> parseHttpRuleHelper(x, inputMessageOpt, messageTypes))
.collect(Collectors.toList()))
Comment on lines +118 to +121
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setAdditionalPatterns above just gets the HttpVerb associated with the additional_bindings list. setAdditionalBindings recursively calls this function to get the nested additional_bindings.

.setPathParameters(
validateAndConstructHttpBindings(
pathParamNames, message, messageTypes, patternSampleValues))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.google.api.core.InternalApi;
import com.google.api.gax.core.BackgroundResource;
import com.google.api.gax.core.BackgroundResourceAggregation;
import com.google.api.gax.httpjson.ApiMethodDescriptor;
import com.google.api.gax.httpjson.FieldsExtractor;
import com.google.api.gax.httpjson.HttpJsonCallSettings;
import com.google.api.gax.httpjson.HttpJsonOperationSnapshot;
import com.google.api.gax.httpjson.HttpJsonStubCallableFactory;
Expand All @@ -22,6 +23,7 @@ import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.OperationCallable;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.api.pathtemplate.PathTemplate;
import com.google.common.collect.ImmutableMap;
import com.google.longrunning.Operation;
import com.google.protobuf.TypeRegistry;
Expand Down Expand Up @@ -311,9 +313,32 @@ 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")
.setAdditionalPathsExtractorMap(
ImmutableMap
.<PathTemplate, FieldsExtractor<EchoRequest, Map<String, String>>>
builder()
.put(
PathTemplate.create("/v1beta1/{fooBar.name=cats/*/dogs/*}/echo:foo"),
request -> {
Map<String, String> fields = new HashMap<>();
ProtoRestSerializer<EchoRequest> serializer =
ProtoRestSerializer.create();
serializer.putPathParam(
fields, "fooBar.name", request.getFooBar().getName());
return fields;
})
.put(
PathTemplate.create(
"/v1beta1/{fooBar.name=turtles/*/parrots/*}/echo:foo"),
request -> {
Map<String, String> fields = new HashMap<>();
ProtoRestSerializer<EchoRequest> serializer =
ProtoRestSerializer.create();
serializer.putPathParam(
fields, "fooBar.name", request.getFooBar().getName());
return fields;
})
.build())
.setQueryParamsExtractor(
request -> {
Map<String, List<String>> fields = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.api.pathtemplate.PathTemplate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.Message;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/** Creates parts of a HTTP request from a protobuf message. */
public class ProtoMessageRequestFormatter<RequestT extends Message>
Expand All @@ -50,35 +50,36 @@ public class ProtoMessageRequestFormatter<RequestT extends Message>
private final String rawPath;
private final PathTemplate pathTemplate;
private final FieldsExtractor<RequestT, Map<String, String>> pathVarsExtractor;
private final List<String> additionalRawPaths;
private final List<PathTemplate> additionalPathTemplates;
private final Map<PathTemplate, FieldsExtractor<RequestT, Map<String, String>>>
additionalPathsExtractorMap;

private ProtoMessageRequestFormatter(
FieldsExtractor<RequestT, String> requestBodyExtractor,
FieldsExtractor<RequestT, Map<String, List<String>>> queryParamsExtractor,
String rawPath,
PathTemplate pathTemplate,
FieldsExtractor<RequestT, Map<String, String>> pathVarsExtractor,
List<String> additionalRawPaths,
List<PathTemplate> additionalPathTemplates) {
Map<PathTemplate, FieldsExtractor<RequestT, Map<String, String>>>
additionalPathsExtractorMap) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a private constructor which should give us some flexibility to update the parameters.

this.requestBodyExtractor = requestBodyExtractor;
this.queryParamsExtractor = queryParamsExtractor;
this.rawPath = rawPath;
this.pathTemplate = pathTemplate;
this.pathVarsExtractor = pathVarsExtractor;
this.additionalRawPaths = additionalRawPaths;
this.additionalPathTemplates = additionalPathTemplates;
// RPCs may not define additional bindings -- Set as an empty map
this.additionalPathsExtractorMap =
additionalPathsExtractorMap == null ? ImmutableMap.of() : additionalPathsExtractorMap;
}

public static <RequestT extends Message>
ProtoMessageRequestFormatter.Builder<RequestT> newBuilder() {
return new Builder<RequestT>().setAdditionalPaths();
return new Builder<>();
}

public Builder<RequestT> toBuilder() {
return new Builder<RequestT>()
.setPath(rawPath, pathVarsExtractor)
.setAdditionalPaths(additionalRawPaths.toArray(new String[] {}))
.setAdditionalPathsExtractorMap(additionalPathsExtractorMap)
.setQueryParamsExtractor(queryParamsExtractor)
.setRequestBodyExtractor(requestBodyExtractor);
}
Expand Down Expand Up @@ -110,8 +111,11 @@ public String getPath(RequestT apiMessage) {
if (pathTemplate.matches(path)) {
return path;
}
for (PathTemplate additionalPathTemplate : additionalPathTemplates) {
String additionalPath = additionalPathTemplate.instantiate(pathVarsMap);
for (Map.Entry<PathTemplate, FieldsExtractor<RequestT, Map<String, String>>> entrySet :
additionalPathsExtractorMap.entrySet()) {
PathTemplate additionalPathTemplate = entrySet.getKey();
FieldsExtractor<RequestT, Map<String, String>> pathExtractor = entrySet.getValue();
String additionalPath = additionalPathTemplate.instantiate(pathExtractor.extract(apiMessage));
if (additionalPathTemplate.matches(additionalPath)) {
return additionalPath;
}
Expand All @@ -124,7 +128,7 @@ public String getPath(RequestT apiMessage) {
@BetaApi
@Override
public List<PathTemplate> getAdditionalPathTemplates() {
return additionalPathTemplates;
return ImmutableList.copyOf(additionalPathsExtractorMap.keySet());
}

/* {@inheritDoc} */
Expand All @@ -140,7 +144,8 @@ public static class Builder<RequestT extends Message> {
private FieldsExtractor<RequestT, Map<String, List<String>>> queryParamsExtractor;
private String rawPath;
private FieldsExtractor<RequestT, Map<String, String>> pathVarsExtractor;
private List<String> rawAdditionalPaths;
private Map<PathTemplate, FieldsExtractor<RequestT, Map<String, String>>>
additionalPathsExtractorMap;

public Builder<RequestT> setRequestBodyExtractor(
FieldsExtractor<RequestT, String> requestBodyExtractor) {
Expand All @@ -161,9 +166,10 @@ public Builder<RequestT> setPath(
return this;
}

@BetaApi
public Builder<RequestT> setAdditionalPaths(String... rawAdditionalPaths) {
this.rawAdditionalPaths = Arrays.asList(rawAdditionalPaths);
public Builder<RequestT> setAdditionalPathsExtractorMap(
Map<PathTemplate, FieldsExtractor<RequestT, Map<String, String>>>
additionalPathsExtractorMap) {
this.additionalPathsExtractorMap = additionalPathsExtractorMap;
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
return this;
}

Expand All @@ -186,8 +192,7 @@ public ProtoMessageRequestFormatter<RequestT> build() {
rawPath,
PathTemplate.create(rawPath),
pathVarsExtractor,
rawAdditionalPaths,
rawAdditionalPaths.stream().map(PathTemplate::create).collect(Collectors.toList()));
additionalPathsExtractorMap);
}
}
}
Loading