Skip to content

Commit

Permalink
feat: Make resource pattern validation looser
Browse files Browse the repository at this point in the history
This is required for APIs that don't conform to our expectations. Initially this is unconditional; eventually we'd want to make this part of a generation context which might be strict or loose in various ways.

Unit test is included in this commit; the next commit has "golden generation result" tests.
  • Loading branch information
jskeet committed Apr 14, 2022
1 parent 3b817f3 commit df1a870
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Google.Api.Generator.Tests/ResourcePatternTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ public void ValidPattern(string pattern, string[] paramNames, string pathTemplat
Assert.Equal(pathTemplateString, pat.PathTemplateString);
}

[Theory]
[InlineData("projects/{project}/iap_tunnel/locations/{location}", new[] { "project", "location" },
"projects/{project}/iap_tunnel/locations/{location}")]
public void SemiValidPattern(string pattern, string[] paramNames, string pathTemplateString)
{
// TODO: When we make tight/loose validation conditional, this test should change to
// assert that the patterns provided are valid under loose validation, and invalid under tight validation.
var pat = new ResourcePattern(pattern);
Assert.Equal(paramNames, pat.ParameterNames);
Assert.Equal(pathTemplateString, pat.PathTemplateString);
}

[Theory]
[InlineData("as/{a}", new[] { "A" }, "as/A")]
[InlineData("as/{a=*}", new[] { "A" }, "as/A")]
Expand Down
9 changes: 7 additions & 2 deletions Google.Api.Generator/ProtoUtils/ResourcePattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,16 @@ public ResourcePattern(string pattern)
Segments = segments.ToImmutableList();
if (Segments.OfType<ResourceIdSegment>().Any())
{
// Perform tight (standard) validation if there are any resource-id path-segments in this pattern.
// We normally perform tight (standard) validation if there are any resource-id path-segments in this pattern.
// Loose validation is required to support pubsub's legacy use of the pattern '_deleted-topic_'
// Temporarily, we perform loose validation here, to unblock IAP.
// TODO: Make this configurable, so we can use tight validation in almost all cases, and loose validation
// where an API has an exemption.
bool tightValidation = false;

foreach (var segment in Segments.OfType<CollectionIdentifierSegment>())
{
segment.Validate(tightValidation: true);
segment.Validate(tightValidation);
}
}
}
Expand Down

0 comments on commit df1a870

Please sign in to comment.