Repository: googleapis/api-linter
Pull request #423 (2019-12-06 17:06:36)

[AIP-132] List parent field should be required.


Patch:
diff --git a/docs/rules/0131/request-name-behavior.md b/docs/rules/0131/request-name-behavior.md
index af55fe9..6f778a2 100644
--- a/docs/rules/0131/request-name-behavior.md
+++ b/docs/rules/0131/request-name-behavior.md
@@ -17,8 +17,8 @@ mandated in [AIP-131][].
 
 ## Details
 
-This rule looks at any message matching `Get*Request` and complains if either
-the `name` field does not have a `google.api.field_behavior` annotation with a
+This rule looks at any message matching `Get*Request` and complains if the
+`name` field does not have a `google.api.field_behavior` annotation with a
 value of `REQUIRED`.
 
 ## Examples
@@ -29,7 +29,7 @@ value of `REQUIRED`.
 // Incorrect.
 message GetBookRequest {
   // The `google.api.field_behavior` annotation should also be included.
-  string book = 1 [(google.api.resource_reference) = {
+  string name = 1 [(google.api.resource_reference) = {
     type: "library.googleapis.com/Book"
   }];
 }
@@ -54,9 +54,9 @@ Remember to also include an [aip.dev/not-precedent][] comment explaining why.
 
 ```proto
 // (-- api-linter: core::0131::request-name-behavior=disabled
-//     aip.dev/not-precedent: This is named "book" for historical reasons. --)
+//     aip.dev/not-precedent: We need to do this because reasons. --)
 message GetBookRequest {
-  string book = 1 [(google.api.resource_reference) = {
+  string name = 1 [(google.api.resource_reference) = {
     type: "library.googleapis.com/Book"
   }];
 }
diff --git a/docs/rules/0131/request-name-reference.md b/docs/rules/0131/request-name-reference.md
index 963b31b..4b7964e 100644
--- a/docs/rules/0131/request-name-reference.md
+++ b/docs/rules/0131/request-name-reference.md
@@ -28,7 +28,7 @@ complains if it does not have a `google.api.resource_reference` annotation.
 // Incorrect.
 message GetBookRequest {
   // The `google.api.resource_reference` annotation should also be included.
-  string book = 1 [(google.api.field_behavior) = REQUIRED];
+  string name = 1 [(google.api.field_behavior) = REQUIRED];
 }
 ```
 
@@ -51,9 +51,9 @@ Remember to also include an [aip.dev/not-precedent][] comment explaining why.
 
 ```proto
 // (-- api-linter: core::0131::request-name-reference=disabled
-//     aip.dev/not-precedent: This is named "book" for historical reasons. --)
+//     aip.dev/not-precedent: We need to do this because reasons. --)
 message GetBookRequest {
-  string book = 1 [(google.api.field_behavior) = REQUIRED];
+  string name = 1 [(google.api.field_behavior) = REQUIRED];
 }
 ```
 
diff --git a/docs/rules/0132/request-parent-behavior.md b/docs/rules/0132/request-parent-behavior.md
new file mode 100644
index 0000000..d9c239a
--- /dev/null
+++ b/docs/rules/0132/request-parent-behavior.md
@@ -0,0 +1,75 @@
+---
+rule:
+  aip: 132
+  name: [core, '0132', request-parent-behavior]
+  summary: |
+    List RPCs should annotate the `parent` field with `google.api.field_behavior`.
+permalink: /132/request-parent-behavior
+redirect_from:
+  - /0132/request-parent-behavior
+---
+
+# List methods: Field behavior
+
+This rule enforces that all `List` standard methods have
+`google.api.field_behavior` set to `REQUIRED` on their `string parent` field,
+as mandated in [AIP-132][].
+
+## Details
+
+This rule looks at any message matching `List*Request` and complains if the
+`parent` field does not have a `google.api.field_behavior` annotation with a
+value of `REQUIRED`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message ListBooksRequest {
+  // The `google.api.field_behavior` annotation should also be included.
+  string parent = 1 [(google.api.resource_reference) = {
+    type: "library.googleapis.com/Publisher"
+  }];
+  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/Publisher"
+  ];
+  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-behavior=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+message ListBooksRequest {
+  string parent = 1 [(google.api.resource_reference) = {
+    type: "library.googleapis.com/Publisher"
+  }];
+  int32 page_size = 2;
+  string page_token = 3;
+}
+```
+
+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 49cef17..f8fddb9 100644
--- a/rules/aip0132/aip0132.go
+++ b/rules/aip0132/aip0132.go
@@ -28,6 +28,7 @@ func AddRules(r lint.RuleRegistry) error {
 		132,
 		requestFieldTypes,
 		requestMessageName,
+		requestParentBehavior,
 		responseMessageName,
 		responseUnknownFields,
 		standardFields,
diff --git a/rules/aip0132/request_parent_behavior.go b/rules/aip0132/request_parent_behavior.go
new file mode 100644
index 0000000..d1fe3b9
--- /dev/null
+++ b/rules/aip0132/request_parent_behavior.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 requestParentBehavior = &lint.FieldRule{
+	Name: lint.NewRuleName(132, "request-parent-behavior"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isListRequestMessage(f.GetOwner()) && f.GetName() == "parent"
+	},
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		if !utils.GetFieldBehavior(f).Contains("REQUIRED") {
+			return []lint.Problem{{
+				Message:    "List requests: The `parent` field should include `(google.api.field_behavior) = REQUIRED`.",
+				Descriptor: f,
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0132/request_parent_behavior_test.go b/rules/aip0132/request_parent_behavior_test.go
new file mode 100644
index 0000000..2fed637
--- /dev/null
+++ b/rules/aip0132/request_parent_behavior_test.go
@@ -0,0 +1,47 @@
+// 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 TestRequestParentBehavior(t *testing.T) {
+	for _, test := range []struct {
+		name          string
+		FieldName     string
+		FieldBehavior string
+		problems      testutils.Problems
+	}{
+		{"Valid", "parent", " [(google.api.field_behavior) = REQUIRED]", testutils.Problems{}},
+		{"Missing", "parent", "", testutils.Problems{{Message: "(google.api.field_behavior) = REQUIRED"}}},
+		{"Irrelevant", "something_else", "", testutils.Problems{}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				import "google/api/field_behavior.proto";
+				message  ListBooksRequest {
+					string {{.FieldName}} = 1{{.FieldBehavior}};
+				}
+			`, test)
+			field := f.GetMessageTypes()[0].GetFields()[0]
+			if diff := test.problems.SetDescriptor(field).Diff(requestParentBehavior.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}