Repository: googleapis/api-linter
Pull request #935 (2022-03-03 21:56:11)

feat(AIP-123): validate resource type name format


Patch:
diff --git a/docs/rules/0123/resource-type-name.md b/docs/rules/0123/resource-type-name.md
new file mode 100644
index 0000000..0c6f44f
--- /dev/null
+++ b/docs/rules/0123/resource-type-name.md
@@ -0,0 +1,73 @@
+---
+rule:
+  aip: 123
+  name: [core, '0123', resource-type-name]
+  summary: Resource type names must be of the form {Service Name}/{Type}.
+permalink: /123/resource-type-name
+redirect_from:
+  - /0123/resource-type-name
+---
+
+# Resource type name
+
+This rule enforces that messages that have a `google.api.resource` annotation,
+have a properly formatted `type`, as described in [AIP-123][].
+
+## Details
+
+This rule scans messages with a `google.api.resource` annotation, and validates
+the format of the `type` field conforms to `{Service Name}/{Type}`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message Book {
+  option (google.api.resource) = {
+    // Should not have more than one separating '/'.
+    type: "library.googleapis.com/Genre/Mystery/Book"
+    pattern: "publishers/{publisher}/books/{book}"
+  };
+
+  string name = 1;
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message Book {
+  option (google.api.resource) = {
+    type: "library.googleapis.com/Book"
+    pattern: "publishers/{publisher}/books/{book}"
+  };
+
+  string name = 1;
+}
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the message.
+
+```proto
+// (-- api-linter: core::0123::resource-type-name=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+message Book {
+  option (google.api.resource) = {
+    type: "library.googleapis.com/Genre/Mystery/Book"
+    pattern: "publishers/{publisher}/books/{book}"
+  };
+
+  string name = 1;
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-123]: http://aip.dev/123
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0123/aip0123.go b/rules/aip0123/aip0123.go
index 0f0af19..5c6d977 100644
--- a/rules/aip0123/aip0123.go
+++ b/rules/aip0123/aip0123.go
@@ -36,6 +36,7 @@ func AddRules(r lint.RuleRegistry) error {
 		resourceNameField,
 		resourcePattern,
 		resourceReferenceType,
+		resourceTypeName,
 		resourceVariables,
 	)
 }
diff --git a/rules/aip0123/resource_type_name.go b/rules/aip0123/resource_type_name.go
new file mode 100644
index 0000000..41971c6
--- /dev/null
+++ b/rules/aip0123/resource_type_name.go
@@ -0,0 +1,43 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package aip0123
+
+import (
+	"strings"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/locations"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var resourceTypeName = &lint.MessageRule{
+	Name: lint.NewRuleName(123, "resource-type-name"),
+	OnlyIf: func(m *desc.MessageDescriptor) bool {
+		return utils.GetResource(m) != nil
+	},
+	LintMessage: func(m *desc.MessageDescriptor) []lint.Problem {
+		resource := utils.GetResource(m)
+		if strings.Count(resource.GetType(), "/") != 1 {
+			return []lint.Problem{{
+				Message:    "Resource type names must be of the form {Service Name}/{Type}.",
+				Descriptor: m,
+				Location:   locations.MessageResource(m),
+			}}
+		}
+
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0123/resource_type_name_test.go b/rules/aip0123/resource_type_name_test.go
new file mode 100644
index 0000000..45670e5
--- /dev/null
+++ b/rules/aip0123/resource_type_name_test.go
@@ -0,0 +1,50 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package aip0123
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestResourceTypeName(t *testing.T) {
+	for _, test := range []struct {
+		name     string
+		TypeName string
+		problems testutils.Problems
+	}{
+		{"Valid", "library.googleapis.com/Book", testutils.Problems{}},
+		{"InvalidTooMany", "library.googleapis.com/shelf/Book", testutils.Problems{{Message: "{Service Name}/{Type}"}}},
+		{"InvalidNotEnough", "library.googleapis.com~Book", testutils.Problems{{Message: "{Service Name}/{Type}"}}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+			import "google/api/resource.proto";
+			message Book {
+				option (google.api.resource) = {
+					type: "{{ .TypeName }}"
+					pattern: "publishers/{publisher}/books/{book}"
+				};
+				string name = 1;
+			}
+		`, test)
+			m := f.GetMessageTypes()[0]
+			if diff := test.problems.SetDescriptor(m).Diff(resourceTypeName.Lint(f)); diff != "" {
+				t.Error(diff)
+			}
+		})
+	}
+}