Repository: googleapis/api-linter
Pull request #424 (2019-12-06 17:16:44)

[AIP-132] List parent field should have a resource reference.


Patch:
diff --git a/docs/rules/0132/request-parent-reference.md b/docs/rules/0132/request-parent-reference.md
new file mode 100644
index 0000000..719a467
--- /dev/null
+++ b/docs/rules/0132/request-parent-reference.md
@@ -0,0 +1,68 @@
+---
+rule:
+  aip: 132
+  name: [core, '0132', request-parent-reference]
+  summary: |
+    List RPCs should annotate the `parent` field with `google.api.resource_reference`.
+permalink: /132/request-parent-reference
+redirect_from:
+  - /0132/request-parent-reference
+---
+
+# List methods: Resource reference
+
+This rule enforces that all `List` standard methods have
+`google.api.resource_reference` on their `string parent` field, as mandated in
+[AIP-132][].
+
+## Details
+
+This rule looks at the `parent` field of any message matching `List*Request`
+and complains if it does not have a `google.api.resource_reference` annotation.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message ListBooksRequest {
+  // The `google.api.resource_reference` annotation should also be included.
+  string parent = 1 [(google.api.field_behavior) = REQUIRED];
+  int32 page_size = 2;
+  string page_token = 3;
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message ListBooksRequest {
+  string parent = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+  int32 page_size = 2;
+  string page_token = 3;
+}
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the field.
+Remember to also include an [aip.dev/not-precedent][] comment explaining why.
+
+```proto
+// (-- api-linter: core::0132::request-parent-reference=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+message ListBooksRequest {
+  string parent = 1 [(google.api.field_behavior) = REQUIRED];
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-132]: https://aip.dev/132
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0132/aip0132.go b/rules/aip0132/aip0132.go
index f8fddb9..20e4c39 100644
--- a/rules/aip0132/aip0132.go
+++ b/rules/aip0132/aip0132.go
@@ -29,6 +29,7 @@ func AddRules(r lint.RuleRegistry) error {
 		requestFieldTypes,
 		requestMessageName,
 		requestParentBehavior,
+		requestParentReference,
 		responseMessageName,
 		responseUnknownFields,
 		standardFields,
diff --git a/rules/aip0132/request_parent_reference.go b/rules/aip0132/request_parent_reference.go
new file mode 100644
index 0000000..f73f5c9
--- /dev/null
+++ b/rules/aip0132/request_parent_reference.go
@@ -0,0 +1,37 @@
+// Copyright 2019 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 aip0132
+
+import (
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var requestParentReference = &lint.FieldRule{
+	Name: lint.NewRuleName(132, "request-parent-reference"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isListRequestMessage(f.GetOwner()) && f.GetName() == "parent"
+	},
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		if ref := utils.GetResourceReference(f); ref == nil {
+			return []lint.Problem{{
+				Message:    "List methods: The `parent` field should include a `google.api.resource_reference` annotation.",
+				Descriptor: f,
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0132/request_parent_reference_test.go b/rules/aip0132/request_parent_reference_test.go
new file mode 100644
index 0000000..24a82d2
--- /dev/null
+++ b/rules/aip0132/request_parent_reference_test.go
@@ -0,0 +1,60 @@
+// Copyright 2019 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 aip0132
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestRequestParentReference(t *testing.T) {
+	t.Run("Present", func(t *testing.T) {
+		f := testutils.ParseProto3String(t, `
+			import "google/api/resource.proto";
+			message ListBooksRequest {
+				string parent = 1 [(google.api.resource_reference) = {
+					type: "library.googleapis.com/Publisher"
+				}];
+			}
+		`)
+		if diff := (testutils.Problems{}).Diff(requestParentReference.Lint(f)); diff != "" {
+			t.Errorf(diff)
+		}
+	})
+	t.Run("Absent", func(t *testing.T) {
+		for _, test := range []struct {
+			name      string
+			FieldName string
+			problems  testutils.Problems
+		}{
+			{"Error", "parent", testutils.Problems{{Message: "google.api.resource_reference"}}},
+			{"Irrelevant", "something_else", testutils.Problems{}},
+		} {
+			t.Run(test.name, func(t *testing.T) {
+				f := testutils.ParseProto3Tmpl(t, `
+					import "google/api/resource.proto";
+					message ListBooksRequest {
+						string {{.FieldName}} = 1;
+					}
+				`, test)
+				field := f.GetMessageTypes()[0].GetFields()[0]
+				if diff := test.problems.SetDescriptor(field).Diff(requestParentReference.Lint(f)); diff != "" {
+					t.Errorf(diff)
+				}
+			})
+		}
+	})
+}