From df1a870fe469060187c89becb6a9f39d217d9f14 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Thu, 14 Apr 2022 09:47:42 +0100 Subject: [PATCH] feat: Make resource pattern validation looser 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. --- Google.Api.Generator.Tests/ResourcePatternTest.cs | 12 ++++++++++++ Google.Api.Generator/ProtoUtils/ResourcePattern.cs | 9 +++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Google.Api.Generator.Tests/ResourcePatternTest.cs b/Google.Api.Generator.Tests/ResourcePatternTest.cs index fd1128b2..3fa4d108 100644 --- a/Google.Api.Generator.Tests/ResourcePatternTest.cs +++ b/Google.Api.Generator.Tests/ResourcePatternTest.cs @@ -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")] diff --git a/Google.Api.Generator/ProtoUtils/ResourcePattern.cs b/Google.Api.Generator/ProtoUtils/ResourcePattern.cs index 543ea616..7db54e01 100644 --- a/Google.Api.Generator/ProtoUtils/ResourcePattern.cs +++ b/Google.Api.Generator/ProtoUtils/ResourcePattern.cs @@ -288,11 +288,16 @@ public ResourcePattern(string pattern) Segments = segments.ToImmutableList(); if (Segments.OfType().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()) { - segment.Validate(tightValidation: true); + segment.Validate(tightValidation); } } }