Repository: googleapis/api-linter
Pull request #797 (2021-03-16 16:00:51)

feat: AIP-123 – Complain if resource references are not strings.
/cc @jskeet


Patch:
diff --git a/docs/rules/0123/resource-reference-type.md b/docs/rules/0123/resource-reference-type.md
new file mode 100644
index 0000000..a794760
--- /dev/null
+++ b/docs/rules/0123/resource-reference-type.md
@@ -0,0 +1,68 @@
+---
+rule:
+  aip: 123
+  name: [core, '0123', resource-reference-type]
+  summary: Resource reference annotations should only apply to strings.
+permalink: /123/resource-reference-type
+redirect_from:
+  - /0123/resource-reference-type
+---
+
+# Resource annotation presence
+
+This rule enforces that any field with a `google.api.resource_reference`
+annotation has a `string` type, as described in [AIP-123][].
+
+## Details
+
+This rule scans all fields with a `google.api.resource_reference` annotation.
+If one is found, the type is checked, and the rule complains if the type is
+anything other than `string`.
+
+It suggests the removal of the annotation rather than fixing the type, because
+what we have observed in real life is that the annotation is usually what is
+in error rather than the selected type.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message Book {
+  string name = 1;
+
+  // This is not a resource reference; the annotation does not belong.
+  Author author = 2 [(google.api.resource_reference) = {
+    type: "library.googleapis.com/Author"
+  }];
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message Book {
+  string name = 1;
+
+  Author author = 2;
+}
+```
+
+```proto
+// Incorrect.
+message Book {
+  string name = 1;
+
+  string author = 2 [(google.api.resource_reference) = {
+    type: "library.googleapis.com/Author"
+  }];
+}
+```
+
+## Disabling
+
+Do not violate this rule; it will break several tools.
+
+[aip-123]: http://aip.dev/123
diff --git a/rules/aip0123/aip0123.go b/rules/aip0123/aip0123.go
index 59a809a..e3916d6 100644
--- a/rules/aip0123/aip0123.go
+++ b/rules/aip0123/aip0123.go
@@ -34,6 +34,7 @@ func AddRules(r lint.RuleRegistry) error {
 		duplicateResource,
 		resourceAnnotation,
 		resourcePattern,
+		resourceReferenceType,
 		resourceVariables,
 	)
 }
diff --git a/rules/aip0123/resource_reference_type.go b/rules/aip0123/resource_reference_type.go
new file mode 100644
index 0000000..107e398
--- /dev/null
+++ b/rules/aip0123/resource_reference_type.go
@@ -0,0 +1,43 @@
+// Copyright 2021 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 (
+	"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 resourceReferenceType = &lint.FieldRule{
+	Name: lint.NewRuleName(123, "resource-reference-type"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return utils.GetResourceReference(f) != nil
+	},
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		if utils.GetTypeName(f) != "string" {
+			// We assume that the likely mistake is probably that the annotation
+			// is wrong (and should not be there), and not that the type is wrong,
+			// because this is what we have observed in real life.
+			return []lint.Problem{{
+				Message:    "Resource references should only be applied to strings.",
+				Descriptor: f,
+				Location:   locations.FieldResourceReference(f),
+				Suggestion: "",
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0123/resource_reference_type_test.go b/rules/aip0123/resource_reference_type_test.go
new file mode 100644
index 0000000..1a126f0
--- /dev/null
+++ b/rules/aip0123/resource_reference_type_test.go
@@ -0,0 +1,53 @@
+// Copyright 2021 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 TestResourceReferenceType(t *testing.T) {
+	annotation := ` [(google.api.resource_reference) = {
+		type: "library.googleapis.com/Author"
+	}]`
+	for _, test := range []struct {
+		name       string
+		Type       string
+		Annotation string
+		problems   testutils.Problems
+	}{
+		{"Valid", "string", annotation, nil},
+		{"ValidRepeated", "repeated string", annotation, nil},
+		{"Invalid", "Author", annotation, testutils.Problems{{Suggestion: ""}}},
+		{"Irrelevant", "Author", "", nil},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			file := testutils.ParseProto3Tmpl(t, `
+			  import "google/api/resource.proto";
+				message Book {
+					{{.Type}} author = 1{{.Annotation}};
+				}
+				message Author {}  // For the failure case, to make compilation work.
+			`, test)
+			field := file.GetMessageTypes()[0].GetFields()[0]
+			got := resourceReferenceType.Lint(file)
+			if diff := test.problems.SetDescriptor(field).Diff(got); diff != "" {
+				t.Error(diff)
+			}
+		})
+	}
+}