Skip to content

Commit 40a69bd

Browse files
authored
fix(AIP-123): handle errant slash in pattern parsing (#1517)
When parsing the resource name `pattern`s to evaluate if a resource is a root/top-level resource, and pull out the parent resource if not, there was a bug where a leading slash on a root/top-level resource would make it think the pattern was multi-segmented (because it just checks for how many segments separators the `pattern` has). This would result in following through to logic that expected multiple items in a slice when there was only one, resulting in an out of bounds panic. We can handle this by trimming leading/trailing separators from the pattern before evaluating the pattern as a root/top-level resource Updates #1514
1 parent 2454606 commit 40a69bd

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

rules/aip0123/aip0123.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func isRootLevelResource(resource *apb.ResourceDescriptor) bool {
9696
// root-level resource by checking how many segments it has - root-level
9797
// resource patterns have only two segments, thus one delimeter.
9898
func isRootLevelResourcePattern(pattern string) bool {
99-
return strings.Count(pattern, "/") <= 1
99+
return strings.Count(strings.Trim(pattern, "/"), "/") <= 1
100100
}
101101

102102
// getParentIDVariable is a helper that returns the parent resource ID segment

rules/aip0123/aip0123_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ func TestIsNestedName(t *testing.T) {
4343
},
4444
want: false,
4545
},
46+
{
47+
name: "top level with extra leading slash",
48+
resource: &apb.ResourceDescriptor{
49+
Type: "example.googleapis.com/Project",
50+
Pattern: []string{"/projects/{project}"},
51+
Singular: "project",
52+
Plural: "projects",
53+
},
54+
want: false,
55+
},
56+
{
57+
name: "top level with extra trailing slash",
58+
resource: &apb.ResourceDescriptor{
59+
Type: "example.googleapis.com/Project",
60+
Pattern: []string{"projects/{project}/"},
61+
Singular: "project",
62+
Plural: "projects",
63+
},
64+
want: false,
65+
},
4666
{
4767
name: "non-nested child collection",
4868
resource: &apb.ResourceDescriptor{

0 commit comments

Comments
 (0)