From 7fa135c9aa60c9cb15880e098d36ea81b95ea488 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Fri, 6 Aug 2021 14:12:30 -0700 Subject: [PATCH] fix: bring back unused resnames for Ads (#821) --- .../generator/gapic/protoparser/Parser.java | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java b/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java index 8f5286e636..42a50d4308 100644 --- a/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java +++ b/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java @@ -142,8 +142,11 @@ public static GapicContext parse(CodeGeneratorRequest request) { Map resourceNames = parseResourceNames(request); messages = updateResourceNamesInMessages(messages, resourceNames.values()); - // Contains only resource names that are actually used. That is, resource name definitions - // or references that are simply defined, but not used, will not have corresponding Java helper + // Contains only resource names that are actually used. Usage refers to the presence of a + // request message's field in an RPC's method_signature annotation. That is, resource name + // definitions + // or references that are simply defined, but not used in such a manner, will not have + // corresponding Java helper // classes generated. Set outputArgResourceNames = new HashSet<>(); List mixinServices = new ArrayList<>(); @@ -160,6 +163,37 @@ public static GapicContext parse(CodeGeneratorRequest request) { transport); Preconditions.checkState(!services.isEmpty(), "No services found to generate"); + + // Temporary workaround for Ads, who still need these resource names. + if (services.get(0).protoPakkage().startsWith("google.ads.googleads.v")) { + Function typeNameFn = + r -> r.resourceTypeString().substring(r.resourceTypeString().indexOf("/") + 1); + Function, Set> typeStringSetFn = + sr -> sr.stream().map(r -> typeNameFn.apply(r)).collect(Collectors.toSet()); + + // Include all resource names present in message types for backwards-compatibility with the + // monolith. In the future, this should be removed on a client library major semver update. + // Resolve type name collisions with the ones present in the method arguments. + final Set typeStringSet = typeStringSetFn.apply(outputArgResourceNames); + outputArgResourceNames.addAll( + resourceNames.values().stream() + .filter(r -> r.hasParentMessageName() && !typeStringSet.contains(typeNameFn.apply(r))) + .collect(Collectors.toSet())); + + String servicePackage = services.get(0).pakkage(); + Map patternsToResourceNames = + ResourceParserHelpers.createPatternResourceNameMap(resourceNames); + for (ResourceReference resourceReference : outputResourceReferencesSeen) { + final Set interimTypeStringSet = typeStringSetFn.apply(outputArgResourceNames); + outputArgResourceNames.addAll( + ResourceReferenceParser.parseResourceNames( + resourceReference, servicePackage, null, resourceNames, patternsToResourceNames) + .stream() + .filter(r -> !interimTypeStringSet.contains(typeNameFn.apply(r))) + .collect(Collectors.toSet())); + } + } + return GapicContext.builder() .setServices(services) .setMixinServices(mixinServices)