Skip to content

Commit

Permalink
fix: Disambiguate resource name collisions
Browse files Browse the repository at this point in the history
For example, if an `Account` resource has `name` and `account_name`
properties, we don't want to generate an `AccountName` property in
GAPIC, as that collides with the one generated by protoc. Instead,
we generate `NameAsAccountName`.

No tests for this edge case, but I've run the generator against the
Merchant Accounts API (which flagged this up) and the result builds
where it didn't before.

Fixes b/343924087
  • Loading branch information
jskeet committed May 31, 2024
1 parent 101ea91 commit dbdc2f3
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Google.Api.Generator/ProtoUtils/ResourceDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,11 @@ public Field(FieldDescriptor fieldDesc, Definition resourceDef, IReadOnlyList<De
// innerFields only non-null for the IResourceName property of child_type refs.
Descriptor = fieldDesc;
ResourceDefinition = resourceDef;
var unqualifiedName = (resourceDef.IsWildcardOnly ? "ResourceName" : resourceDef.ResourceNameTyp.Name) + (fieldDesc.IsRepeated ? "s" : "");
var requireIdentifier = !((fieldDesc.IsRepeated && fieldDesc.Name.ToLowerInvariant() == "names") ||
(!fieldDesc.IsRepeated && fieldDesc.Name.ToLowerInvariant() == "name"));
ResourcePropertyName = (requireIdentifier ? $"{UnderlyingPropertyName}As" : "") +
(resourceDef.IsWildcardOnly ? "ResourceName" : resourceDef.ResourceNameTyp.Name) + (fieldDesc.IsRepeated ? "s" : "");
(!fieldDesc.IsRepeated && fieldDesc.Name.ToLowerInvariant() == "name")) ||
fieldDesc.ContainingType.Fields.InDeclarationOrder().Any(f => f.CSharpPropertyName() == unqualifiedName);
ResourcePropertyName = (requireIdentifier ? $"{UnderlyingPropertyName}As" : "") + unqualifiedName;
InnerDefs = innerDefs;
ContainsWildcard = containsWildcard;
}
Expand Down

0 comments on commit dbdc2f3

Please sign in to comment.