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

[ggj][codegen] fix: add workaround for missing default_host and oauth_scopes annotation #419

Merged
merged 10 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -130,7 +130,12 @@ public static List<Service> parseServices(
fileToGenerate);

services.addAll(
parseService(fileDescriptor, messageTypes, resourceNames, outputArgResourceNames));
parseService(
fileDescriptor,
messageTypes,
resourceNames,
serviceYamlProtoOpt,
outputArgResourceNames));
}

return services;
Expand All @@ -140,28 +145,37 @@ public static List<Service> parseService(
FileDescriptor fileDescriptor,
Map<String, Message> messageTypes,
Map<String, ResourceName> resourceNames,
Optional<com.google.api.Service> serviceYamlProtoOpt,
Set<ResourceName> outputArgResourceNames) {
String pakkage = TypeParser.getPackage(fileDescriptor);

return fileDescriptor.getServices().stream()
.map(
s -> {
// TODO(miraleung, v2): Handle missing default_host and oauth_scopes annotations.
// Workaround for a missing default_host and oauth_scopes annotation from a service
// definition. This can happen for protos that bypass the publishing process.
// TODO(miraleung): Remove this workaround later?
ServiceOptions serviceOptions = s.getOptions();
// The proto publishing process will insert these two fields from the 1P esrvice
// config YAML file, so we can assume they must be present for v1 for the
// microgenerator.
Preconditions.checkState(
serviceOptions.hasExtension(ClientProto.defaultHost),
String.format("Default host annotation not found in service %s", s.getName()));
String defaultHost = null;
if (serviceOptions.hasExtension(ClientProto.defaultHost)) {
defaultHost =
sanitizeDefaultHost(serviceOptions.getExtension(ClientProto.defaultHost));
} else if (serviceYamlProtoOpt.isPresent()) {
// Fall back to the DNS name supplied in the service .yaml config.
defaultHost = serviceYamlProtoOpt.get().getName();
}
Preconditions.checkState(
serviceOptions.hasExtension(ClientProto.oauthScopes),
String.format("Oauth scopes annotation not found in service %s", s.getName()));

String defaultHost =
sanitizeDefaultHost(serviceOptions.getExtension(ClientProto.defaultHost));
List<String> oauthScopes =
Arrays.asList(serviceOptions.getExtension(ClientProto.oauthScopes).split(COMMA));
!Strings.isNullOrEmpty(defaultHost),
String.format(
"Default host not found in service YAML config file or annotation for %s",
s.getName()));

List<String> oauthScopes = Collections.emptyList();
if (serviceOptions.hasExtension(ClientProto.oauthScopes)) {
oauthScopes =
Arrays.asList(
serviceOptions.getExtension(ClientProto.oauthScopes).split(COMMA));
}

Service.Builder serviceBuilder = Service.builder();
if (fileDescriptor.toProto().hasSourceCodeInfo()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ public void batchingDescriptor_hasSubresponseField() {
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
serviceFileDescriptor, messageTypes, resourceNames, outputResourceNames);
serviceFileDescriptor,
messageTypes,
resourceNames,
Optional.empty(),
outputResourceNames);

String filename = "pubsub_gapic.yaml";
Path path = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, filename);
Expand Down Expand Up @@ -132,7 +136,11 @@ public void batchingDescriptor_noSubresponseField() {
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
serviceFileDescriptor, messageTypes, resourceNames, outputResourceNames);
serviceFileDescriptor,
messageTypes,
resourceNames,
Optional.empty(),
outputResourceNames);

String filename = "logging_gapic.yaml";
Path path = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, filename);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -53,7 +54,8 @@ public void generateServiceClasses() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);

Service echoProtoService = services.get(0);
GapicClass clazz =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.junit.Test;

Expand All @@ -50,7 +51,8 @@ public void generateGrpcServiceStubClass_simple() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
Service echoProtoService = services.get(0);
GapicClass clazz =
GrpcServiceStubClassComposer.instance().generate(echoProtoService, messageTypes);
Expand All @@ -73,7 +75,11 @@ public void generateGrpcServiceStubClass_httpBindings() {
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
testingFileDescriptor, messageTypes, resourceNames, outputResourceNames);
testingFileDescriptor,
messageTypes,
resourceNames,
Optional.empty(),
outputResourceNames);
Service testingProtoService = services.get(0);
GapicClass clazz =
GrpcServiceStubClassComposer.instance().generate(testingProtoService, messageTypes);
Expand Down Expand Up @@ -102,7 +108,11 @@ public void generateGrpcServiceStubClass_httpBindingsWithSubMessageFields() {
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
serviceFileDescriptor, messageTypes, resourceNames, outputResourceNames);
serviceFileDescriptor,
messageTypes,
resourceNames,
Optional.empty(),
outputResourceNames);

Service service = services.get(0);
GapicClass clazz = GrpcServiceStubClassComposer.instance().generate(service, messageTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -53,7 +54,8 @@ public void generateServiceClasses() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);

Service echoProtoService = services.get(0);
GapicClass clazz = MockServiceClassComposer.instance().generate(echoProtoService, messageTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -53,7 +54,8 @@ public void generateServiceClasses() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);

Service echoProtoService = services.get(0);
GapicClass clazz =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -88,7 +89,8 @@ public void generateResourceNameClass_echoFoobarMultiplePatterns() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);

ResourceName foobarResname = resourceNames.get("showcase.googleapis.com/Foobar");
assertThat(outputResourceNames).contains(foobarResname);
Expand All @@ -114,7 +116,11 @@ public void generateResourceNameClass_testingSessionOnePattern() {
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
testingFileDescriptor, messageTypes, resourceNames, outputResourceNames);
testingFileDescriptor,
messageTypes,
resourceNames,
Optional.empty(),
outputResourceNames);

ResourceName sessionResname = resourceNames.get("showcase.googleapis.com/Session");
assertThat(outputResourceNames).contains(sessionResname);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public void paramDefinitionsBlock_noConfigsFound() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
assertEquals(1, services.size());

Service service = services.get(0);
Expand Down Expand Up @@ -118,7 +119,8 @@ public void paramDefinitionsBlock_basic() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
assertEquals(1, services.size());

Service service = services.get(0);
Expand Down Expand Up @@ -159,7 +161,8 @@ public void codesDefinitionsBlock_noConfigsFound() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
assertEquals(1, services.size());

Service service = services.get(0);
Expand Down Expand Up @@ -196,7 +199,8 @@ public void codesDefinitionsBlock_basic() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
assertEquals(1, services.size());

Service service = services.get(0);
Expand Down Expand Up @@ -236,7 +240,8 @@ public void simpleBuilderExpr_basic() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
assertEquals(1, services.size());

Service service = services.get(0);
Expand Down Expand Up @@ -318,7 +323,8 @@ public void lroBuilderExpr() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
assertEquals(1, services.size());

Service service = services.get(0);
Expand Down Expand Up @@ -379,7 +385,11 @@ public void batchingSettings_minimalFlowControlSettings() {
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
serviceFileDescriptor, messageTypes, resourceNames, outputResourceNames);
serviceFileDescriptor,
messageTypes,
resourceNames,
Optional.empty(),
outputResourceNames);

String filename = "pubsub_gapic.yaml";
Path path = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, filename);
Expand Down Expand Up @@ -456,7 +466,11 @@ public void batchingSettings_fullFlowControlSettings() {
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
serviceFileDescriptor, messageTypes, resourceNames, outputResourceNames);
serviceFileDescriptor,
messageTypes,
resourceNames,
Optional.empty(),
outputResourceNames);

String filename = "logging_gapic.yaml";
Path path = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, filename);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.junit.Test;

Expand All @@ -47,7 +48,8 @@ public void generateServiceClasses() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);

Service echoProtoService = services.get(0);
GapicClass clazz =
Expand All @@ -70,7 +72,8 @@ public void generateServiceClasses_methodSignatureHasNestedFields() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(fileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(fileDescriptor, messageTypes, resourceNames, outputResourceNames);
Parser.parseService(
fileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);

Service protoService = services.get(0);
GapicClass clazz = ServiceClientClassComposer.instance().generate(protoService, messageTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.junit.Test;

Expand All @@ -54,7 +55,8 @@ public void generateClientTest_echoClient() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);

Service echoProtoService = services.get(0);
GapicClass clazz =
Expand Down Expand Up @@ -85,7 +87,11 @@ public void generateClientTest_pubSubPublisherClient() {
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
serviceFileDescriptor, messageTypes, resourceNames, outputResourceNames);
serviceFileDescriptor,
messageTypes,
resourceNames,
Optional.empty(),
outputResourceNames);

Service subscriptionService = services.get(1);
assertEquals("Subscriber", subscriptionService.name());
Expand Down Expand Up @@ -128,7 +134,11 @@ public void generateClientTest_logging() {
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
serviceFileDescriptor, messageTypes, resourceNames, outputResourceNames);
serviceFileDescriptor,
messageTypes,
resourceNames,
Optional.empty(),
outputResourceNames);

Service loggingService = services.get(0);
GapicClass clazz =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -53,7 +54,8 @@ public void generateServiceClasses() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);

Service echoProtoService = services.get(0);
GapicClass clazz =
Expand Down
Loading