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

feat: Numeric enums in routing headers #2328

Merged
merged 27 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4311ad5
chore: Numeric enums in routing headers
ddixit14 Jan 2, 2024
aae2578
chore: cleaning up and updating golden files
ddixit14 Jan 2, 2024
26a5f10
chore: modifying AbstractTransportServiceStubClassComposer.java
ddixit14 Jan 4, 2024
ba687b3
chore: encoding in numeric only if it an enum
ddixit14 Jan 4, 2024
d63e854
chore: adding Value in fieldmethodname for enums
ddixit14 Jan 8, 2024
a6323ac
chore: passing field info in routingHeaderRule
ddixit14 Jan 9, 2024
6c24cbd
chore: undo changes in gapiccontext.java
ddixit14 Jan 11, 2024
ba1946e
chore: undo changes in this file
ddixit14 Jan 12, 2024
68228f4
chore: undo changes in this file
ddixit14 Jan 12, 2024
7f3a660
chore: undo changes in this file
ddixit14 Jan 12, 2024
18db3a9
chore: simplify the changes in this file
ddixit14 Jan 12, 2024
00772f3
chore: updating the golden files
ddixit14 Jan 12, 2024
a7f57b3
chore: fixing lint errors
ddixit14 Jan 12, 2024
b959375
chore: changing f_kingdom to enum_value
ddixit14 Jan 12, 2024
a2eff68
chore: renaming f_kingdom to enum_test
ddixit14 Jan 12, 2024
db81236
chore: undo changes in this file
ddixit14 Jan 12, 2024
6ab11bf
chore: adding this file again
ddixit14 Jan 12, 2024
fa44f42
chore: handling edge cases and updating showcase files
ddixit14 Jan 12, 2024
71301cc
chore: fixing lint error
ddixit14 Jan 12, 2024
0bcf1a3
chore: working on comments
ddixit14 Jan 16, 2024
98b83f5
chore: Adding an integration test for implicit routing headers
ddixit14 Jan 16, 2024
473743c
chore: disabling wildcard imports
ddixit14 Jan 16, 2024
eea3b52
chore: fixing lint errors
ddixit14 Jan 16, 2024
f5d41b0
chore: working on comments
ddixit14 Jan 16, 2024
e7a8162
chore: adding httptests
ddixit14 Jan 16, 2024
941c410
chore: adding httptests
ddixit14 Jan 16, 2024
b764170
chore: lint
ddixit14 Jan 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,7 @@ private void createRequestParamsExtractorBodyForHttpBindings(
for (HttpBindings.HttpBinding httpBindingFieldBinding :
method.httpBindings().pathParameters()) {
MethodInvocationExpr requestBuilderExpr =
createRequestFieldGetterExpr(requestVarExpr, httpBindingFieldBinding.name());
createRequestFieldGetterExpr(requestVarExpr, httpBindingFieldBinding.name(),httpBindingFieldBinding.field().isEnum());
Expr valueOfExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(TypeNode.STRING)
Expand Down Expand Up @@ -1361,7 +1361,7 @@ private void createRequestParamsExtractorBodyForRoutingHeaders(
for (int i = 0; i < routingHeaderParams.size(); i++) {
RoutingHeaderRule.RoutingHeaderParam routingHeaderParam = routingHeaderParams.get(i);
MethodInvocationExpr requestFieldGetterExpr =
createRequestFieldGetterExpr(requestVarExpr, routingHeaderParam.fieldName());
createRequestFieldGetterExpr(requestVarExpr, routingHeaderParam.fieldName(),false);
ddixit14 marked this conversation as resolved.
Show resolved Hide resolved
Expr routingHeaderKeyExpr =
ValueExpr.withValue(StringObjectValue.withValue(routingHeaderParam.key()));
String pathTemplateName =
Expand Down Expand Up @@ -1462,7 +1462,7 @@ private Expr fieldValuesNotNullConditionExpr(
}

private MethodInvocationExpr createRequestFieldGetterExpr(
VariableExpr requestVarExpr, String fieldName) {
VariableExpr requestVarExpr, String fieldName, Boolean isFieldEnum) {
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
MethodInvocationExpr.Builder requestFieldGetterExprBuilder =
MethodInvocationExpr.builder().setExprReferenceExpr(requestVarExpr);
List<String> descendantFields = Splitter.on(".").splitToList(fieldName);
Expand All @@ -1471,7 +1471,10 @@ private MethodInvocationExpr createRequestFieldGetterExpr(
for (int i = 0; i < descendantFields.size(); i++) {
String currFieldName = descendantFields.get(i);
String bindingFieldMethodName =
String.format("get%s", JavaStyle.toUpperCamelCase(currFieldName));
String.format("get%s", JavaStyle.toUpperCamelCase(currFieldName));
if (i==descendantFields.size()-1 && isFieldEnum) {
bindingFieldMethodName = bindingFieldMethodName+"Value";
}
requestFieldGetterExprBuilder =
requestFieldGetterExprBuilder.setMethodName(bindingFieldMethodName);
if (i < descendantFields.size() - 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gapic.metadata.GapicMetadata;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;

import javax.annotation.Nullable;
import java.util.*;
ddixit14 marked this conversation as resolved.
Show resolved Hide resolved
import java.util.stream.Collectors;

@AutoValue
public abstract class GapicContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.RequestParamsBuilder;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.api.pathtemplate.PathTemplate;
import com.google.explicit.dynamic.routing.header.RepeatRequest;
import com.google.explicit.dynamic.routing.header.RepeatResponse;
import com.google.explicit.dynamic.routing.header.Request;
import com.google.explicit.dynamic.routing.header.RequestWithNestedField;
import com.google.longrunning.stub.GrpcOperationsStub;
Expand Down Expand Up @@ -156,6 +158,26 @@ public class GrpcExplicitDynamicRoutingHeaderTestingStub
.setResponseMarshaller(ProtoUtils.marshaller(Empty.getDefaultInstance()))
.build();

private static final MethodDescriptor<RepeatRequest, RepeatResponse>
repeatDataPathEnumMethodDescriptor =
MethodDescriptor.<RepeatRequest, RepeatResponse>newBuilder()
.setType(MethodDescriptor.MethodType.UNARY)
.setFullMethodName(
"google.explicit.dynamic.routing.header.ExplicitDynamicRoutingHeaderTesting/RepeatDataPathEnum")
.setRequestMarshaller(ProtoUtils.marshaller(RepeatRequest.getDefaultInstance()))
.setResponseMarshaller(ProtoUtils.marshaller(RepeatResponse.getDefaultInstance()))
.build();

private static final MethodDescriptor<RepeatRequest, RepeatResponse>
repeatDataPathEnumOptionalMethodDescriptor =
MethodDescriptor.<RepeatRequest, RepeatResponse>newBuilder()
.setType(MethodDescriptor.MethodType.UNARY)
.setFullMethodName(
"google.explicit.dynamic.routing.header.ExplicitDynamicRoutingHeaderTesting/RepeatDataPathEnumOptional")
.setRequestMarshaller(ProtoUtils.marshaller(RepeatRequest.getDefaultInstance()))
.setResponseMarshaller(ProtoUtils.marshaller(RepeatResponse.getDefaultInstance()))
.build();

private final UnaryCallable<Request, Empty> example1TestCallable;
private final UnaryCallable<Request, Empty> example2TestCallable;
private final UnaryCallable<Request, Empty> example3TestCallable;
Expand All @@ -170,6 +192,8 @@ public class GrpcExplicitDynamicRoutingHeaderTestingStub
private final UnaryCallable<Request, Empty> backwardsCompatible2TestCallable;
private final UnaryCallable<Request, Empty> backwardsCompatible3TestCallable;
private final UnaryCallable<RequestWithNestedField, Empty> nestedFieldTestCallable;
private final UnaryCallable<RepeatRequest, RepeatResponse> repeatDataPathEnumCallable;
private final UnaryCallable<RepeatRequest, RepeatResponse> repeatDataPathEnumOptionalCallable;

private final BackgroundResource backgroundResources;
private final GrpcOperationsStub operationsStub;
Expand Down Expand Up @@ -427,6 +451,26 @@ public class GrpcExplicitDynamicRoutingHeaderTestingStub
return builder.build();
})
.build();
GrpcCallSettings<RepeatRequest, RepeatResponse> repeatDataPathEnumTransportSettings =
GrpcCallSettings.<RepeatRequest, RepeatResponse>newBuilder()
.setMethodDescriptor(repeatDataPathEnumMethodDescriptor)
.setParamsExtractor(
request -> {
RequestParamsBuilder builder = RequestParamsBuilder.create();
builder.add("info.f_kingdom", Integer.valueOf(request.getInfo().getFKingdom()));
return builder.build();
})
.build();
GrpcCallSettings<RepeatRequest, RepeatResponse> repeatDataPathEnumOptionalTransportSettings =
GrpcCallSettings.<RepeatRequest, RepeatResponse>newBuilder()
.setMethodDescriptor(repeatDataPathEnumOptionalMethodDescriptor)
.setParamsExtractor(
request -> {
RequestParamsBuilder builder = RequestParamsBuilder.create();
builder.add("info.p_kingdom", Integer.valueOf(request.getInfo().getPKingdom()));
return builder.build();
})
.build();

this.example1TestCallable =
callableFactory.createUnaryCallable(
Expand Down Expand Up @@ -476,6 +520,16 @@ public class GrpcExplicitDynamicRoutingHeaderTestingStub
this.nestedFieldTestCallable =
callableFactory.createUnaryCallable(
nestedFieldTestTransportSettings, settings.nestedFieldTestSettings(), clientContext);
this.repeatDataPathEnumCallable =
callableFactory.createUnaryCallable(
repeatDataPathEnumTransportSettings,
settings.repeatDataPathEnumSettings(),
clientContext);
this.repeatDataPathEnumOptionalCallable =
callableFactory.createUnaryCallable(
repeatDataPathEnumOptionalTransportSettings,
settings.repeatDataPathEnumOptionalSettings(),
clientContext);

this.backgroundResources =
new BackgroundResourceAggregation(clientContext.getBackgroundResources());
Expand Down Expand Up @@ -555,6 +609,16 @@ public class GrpcExplicitDynamicRoutingHeaderTestingStub
return nestedFieldTestCallable;
}

@Override
public UnaryCallable<RepeatRequest, RepeatResponse> repeatDataPathEnumCallable() {
return repeatDataPathEnumCallable;
}

@Override
public UnaryCallable<RepeatRequest, RepeatResponse> repeatDataPathEnumOptionalCallable() {
return repeatDataPathEnumOptionalCallable;
}

@Override
public final void close() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public class GrpcTestingStub extends TestingStub {
builder.add("name", String.valueOf(request.getName()));
builder.add(
"test_to_verify.name", String.valueOf(request.getTestToVerify().getName()));
builder.add("type", String.valueOf(request.getType()));
builder.add("type", Integer.valueOf(request.getType()));
return builder.build();
})
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public class HttpJsonComplianceStub extends ComplianceStub {
builder.add("info.f_bool", String.valueOf(request.getInfo().getFBool()));
builder.add("info.f_double", String.valueOf(request.getInfo().getFDouble()));
builder.add("info.f_int32", String.valueOf(request.getInfo().getFInt32()));
builder.add("info.f_kingdom", String.valueOf(request.getInfo().getFKingdom()));
builder.add("info.f_kingdom", Integer.valueOf(request.getInfo().getFKingdom()));
builder.add("info.f_string", String.valueOf(request.getInfo().getFString()));
return builder.build();
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.RequestParamsBuilder;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.api.pathtemplate.PathTemplate;
import com.google.explicit.dynamic.routing.header.RepeatRequest;
import com.google.explicit.dynamic.routing.header.RepeatResponse;
import com.google.explicit.dynamic.routing.header.Request;
import com.google.explicit.dynamic.routing.header.RequestWithNestedField;
import com.google.protobuf.Empty;
Expand Down Expand Up @@ -140,9 +142,89 @@ public class HttpJsonExplicitDynamicRoutingHeaderTestingStub
.build())
.build();

private static final ApiMethodDescriptor<RepeatRequest, RepeatResponse>
repeatDataPathEnumMethodDescriptor =
ApiMethodDescriptor.<RepeatRequest, RepeatResponse>newBuilder()
.setFullMethodName(
"google.explicit.dynamic.routing.header.ExplicitDynamicRoutingHeaderTesting/RepeatDataPathEnum")
.setHttpMethod("POST")
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
ProtoMessageRequestFormatter.<RepeatRequest>newBuilder()
.setPath(
"/v1beta1/repeat/kingdom/{info.fKingdom}:pathenum",
request -> {
Map<String, String> fields = new HashMap<>();
ProtoRestSerializer<RepeatRequest> serializer =
ProtoRestSerializer.create();
serializer.putPathParam(
fields, "info.fKingdom", request.getInfo().getFKingdomValue());
return fields;
})
.setQueryParamsExtractor(
request -> {
Map<String, List<String>> fields = new HashMap<>();
ProtoRestSerializer<RepeatRequest> serializer =
ProtoRestSerializer.create();
return fields;
})
.setRequestBodyExtractor(
request ->
ProtoRestSerializer.create()
.toBody("*", request.toBuilder().build(), false))
.build())
.setResponseParser(
ProtoMessageResponseParser.<RepeatResponse>newBuilder()
.setDefaultInstance(RepeatResponse.getDefaultInstance())
.setDefaultTypeRegistry(typeRegistry)
.build())
.build();

private static final ApiMethodDescriptor<RepeatRequest, RepeatResponse>
repeatDataPathEnumOptionalMethodDescriptor =
ApiMethodDescriptor.<RepeatRequest, RepeatResponse>newBuilder()
.setFullMethodName(
"google.explicit.dynamic.routing.header.ExplicitDynamicRoutingHeaderTesting/RepeatDataPathEnumOptional")
.setHttpMethod("POST")
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
ProtoMessageRequestFormatter.<RepeatRequest>newBuilder()
.setPath(
"/v1beta1/repeat/kingdom/{info.pKingdom}:pathenumoptional",
request -> {
Map<String, String> fields = new HashMap<>();
ProtoRestSerializer<RepeatRequest> serializer =
ProtoRestSerializer.create();
if (request.getInfo().hasPKingdom()) {
serializer.putPathParam(
fields, "info.pKingdom", request.getInfo().getPKingdomValue());
}
return fields;
})
.setQueryParamsExtractor(
request -> {
Map<String, List<String>> fields = new HashMap<>();
ProtoRestSerializer<RepeatRequest> serializer =
ProtoRestSerializer.create();
return fields;
})
.setRequestBodyExtractor(
request ->
ProtoRestSerializer.create()
.toBody("*", request.toBuilder().build(), false))
.build())
.setResponseParser(
ProtoMessageResponseParser.<RepeatResponse>newBuilder()
.setDefaultInstance(RepeatResponse.getDefaultInstance())
.setDefaultTypeRegistry(typeRegistry)
.build())
.build();

private final UnaryCallable<Request, Empty> backwardsCompatible1TestCallable;
private final UnaryCallable<Request, Empty> backwardsCompatible2TestCallable;
private final UnaryCallable<Request, Empty> backwardsCompatible3TestCallable;
private final UnaryCallable<RepeatRequest, RepeatResponse> repeatDataPathEnumCallable;
private final UnaryCallable<RepeatRequest, RepeatResponse> repeatDataPathEnumOptionalCallable;

private final BackgroundResource backgroundResources;
private final HttpJsonStubCallableFactory callableFactory;
Expand Down Expand Up @@ -223,6 +305,30 @@ public class HttpJsonExplicitDynamicRoutingHeaderTestingStub
return builder.build();
})
.build();
HttpJsonCallSettings<RepeatRequest, RepeatResponse> repeatDataPathEnumTransportSettings =
HttpJsonCallSettings.<RepeatRequest, RepeatResponse>newBuilder()
.setMethodDescriptor(repeatDataPathEnumMethodDescriptor)
.setTypeRegistry(typeRegistry)
.setParamsExtractor(
request -> {
RequestParamsBuilder builder = RequestParamsBuilder.create();
builder.add("info.f_kingdom", Integer.valueOf(request.getInfo().getFKingdom()));
return builder.build();
})
.build();
HttpJsonCallSettings<RepeatRequest, RepeatResponse>
repeatDataPathEnumOptionalTransportSettings =
HttpJsonCallSettings.<RepeatRequest, RepeatResponse>newBuilder()
.setMethodDescriptor(repeatDataPathEnumOptionalMethodDescriptor)
.setTypeRegistry(typeRegistry)
.setParamsExtractor(
request -> {
RequestParamsBuilder builder = RequestParamsBuilder.create();
builder.add(
"info.p_kingdom", Integer.valueOf(request.getInfo().getPKingdom()));
return builder.build();
})
.build();

this.backwardsCompatible1TestCallable =
callableFactory.createUnaryCallable(
Expand All @@ -239,6 +345,16 @@ public class HttpJsonExplicitDynamicRoutingHeaderTestingStub
backwardsCompatible3TestTransportSettings,
settings.backwardsCompatible3TestSettings(),
clientContext);
this.repeatDataPathEnumCallable =
callableFactory.createUnaryCallable(
repeatDataPathEnumTransportSettings,
settings.repeatDataPathEnumSettings(),
clientContext);
this.repeatDataPathEnumOptionalCallable =
callableFactory.createUnaryCallable(
repeatDataPathEnumOptionalTransportSettings,
settings.repeatDataPathEnumOptionalSettings(),
clientContext);

this.backgroundResources =
new BackgroundResourceAggregation(clientContext.getBackgroundResources());
Expand All @@ -250,6 +366,8 @@ public class HttpJsonExplicitDynamicRoutingHeaderTestingStub
methodDescriptors.add(backwardsCompatible1TestMethodDescriptor);
methodDescriptors.add(backwardsCompatible2TestMethodDescriptor);
methodDescriptors.add(backwardsCompatible3TestMethodDescriptor);
methodDescriptors.add(repeatDataPathEnumMethodDescriptor);
methodDescriptors.add(repeatDataPathEnumOptionalMethodDescriptor);
return methodDescriptors;
}

Expand All @@ -268,6 +386,16 @@ public class HttpJsonExplicitDynamicRoutingHeaderTestingStub
return backwardsCompatible3TestCallable;
}

@Override
public UnaryCallable<RepeatRequest, RepeatResponse> repeatDataPathEnumCallable() {
return repeatDataPathEnumCallable;
}

@Override
public UnaryCallable<RepeatRequest, RepeatResponse> repeatDataPathEnumOptionalCallable() {
return repeatDataPathEnumOptionalCallable;
}

@Override
public UnaryCallable<Request, Empty> example1TestCallable() {
throw new UnsupportedOperationException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public GapicContext parseExplicitDynamicRoutingHeaderTesting() {
ExplicitDynamicRoutingHeaderTestingOuterClass.getDescriptor();
ServiceDescriptor testingService = testingFileDescriptor.getServices().get(0);
assertEquals(testingService.getName(), "ExplicitDynamicRoutingHeaderTesting");

System.out.println("dynamic routing test is being run.............");
Map<String, Message> messageTypes = Parser.parseMessages(testingFileDescriptor);
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(testingFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
Expand Down