Repository: googleapis/api-linter
Pull request #644 (2020-10-05 21:24:35)

Add rules for batch-method requests field behavior
Part of #612.


Patch:
diff --git a/docs/rules/0233/request-requests-behavior.md b/docs/rules/0233/request-requests-behavior.md
new file mode 100644
index 0000000..acd17cc
--- /dev/null
+++ b/docs/rules/0233/request-requests-behavior.md
@@ -0,0 +1,76 @@
+---
+rule:
+  aip: 233
+  name: [core, '0233', request-requests-behavior]
+  summary: |
+    Batch Create requests should annotate the `requests` field with `google.api.field_behavior`.
+permalink: /233/request-requests-behavior
+redirect_from:
+  - /0233/request-requests-behavior
+---
+
+# Batch Create methods: `requests` field behavior
+
+This rule enforces that all `BatchCreate` requests have
+`google.api.field_behavior` set to `REQUIRED` on their `requests` field, as
+mandated in [AIP-233][].
+
+## Details
+
+This rule looks at any message matching `BatchCreate*Request` and complains if the
+`requests` field does not have a `google.api.field_behavior` annotation with a
+value of `REQUIRED`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message BatchCreateBooksRequest {
+  string parent = 1 [
+    (google.api.resource_reference).child_type = "library.googleapis.com/Book"
+  ];
+
+  // The `google.api.field_behavior` annotation should be included.
+  repeated CreateBookRequest requests = 2;
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message BatchCreateBooksRequest {
+  string parent = 1 [
+    (google.api.resource_reference).child_type = "library.googleapis.com/Book"
+  ];
+
+  repeated CreateBookRequest requests = 2 [
+    (google.api.field_behavior) = REQUIRED
+  ];
+}
+```
+
+## 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
+message BatchCreateBooksRequest {
+  string parent = 1 [
+    (google.api.resource_reference).child_type = "library.googleapis.com/Book"
+  ];
+
+  // (-- api-linter: core::0233::request-requests-behavior=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  repeated CreateBookRequest requests = 2;
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-233]: https://aip.dev/233
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/docs/rules/0234/request-requests-behavior.md b/docs/rules/0234/request-requests-behavior.md
new file mode 100644
index 0000000..6e7f92d
--- /dev/null
+++ b/docs/rules/0234/request-requests-behavior.md
@@ -0,0 +1,76 @@
+---
+rule:
+  aip: 234
+  name: [core, '0234', request-requests-behavior]
+  summary: |
+    Batch Update requests should annotate the `requests` field with `google.api.field_behavior`.
+permalink: /234/request-requests-behavior
+redirect_from:
+  - /0234/request-requests-behavior
+---
+
+# Batch Update methods: `requests` field behavior
+
+This rule enforces that all `BatchUpdate` requests have
+`google.api.field_behavior` set to `REQUIRED` on their `requests` field, as
+mandated in [AIP-234][].
+
+## Details
+
+This rule looks at any message matching `BatchUpdate*Request` and complains if the
+`requests` field does not have a `google.api.field_behavior` annotation with a
+value of `REQUIRED`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message BatchUpdateBooksRequest {
+  string parent = 1 [
+    (google.api.resource_reference).child_type = "library.googleapis.com/Book"
+  ];
+
+  // The `google.api.field_behavior` annotation should be included.
+  repeated UpdateBookRequest requests = 2;
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message BatchUpdateBooksRequest {
+  string parent = 1 [
+    (google.api.resource_reference).child_type = "library.googleapis.com/Book"
+  ];
+
+  repeated UpdateBookRequest requests = 2 [
+    (google.api.field_behavior) = REQUIRED
+  ];
+}
+```
+
+## 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
+message BatchUpdateBooksRequest {
+  string parent = 1 [
+    (google.api.resource_reference).child_type = "library.googleapis.com/Book"
+  ];
+
+  // (-- api-linter: core::0234::request-requests-behavior=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  repeated UpdateBookRequest requests = 2;
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-234]: https://aip.dev/234
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/docs/rules/0235/request-requests-behavior.md b/docs/rules/0235/request-requests-behavior.md
new file mode 100644
index 0000000..127b907
--- /dev/null
+++ b/docs/rules/0235/request-requests-behavior.md
@@ -0,0 +1,76 @@
+---
+rule:
+  aip: 235
+  name: [core, '0235', request-requests-behavior]
+  summary: |
+    Batch Delete requests should annotate the `requests` field with `google.api.field_behavior`.
+permalink: /235/request-requests-behavior
+redirect_from:
+  - /0235/request-requests-behavior
+---
+
+# Batch Delete methods: `requests` field behavior
+
+This rule enforces that all `BatchDelete` requests have
+`google.api.field_behavior` set to `REQUIRED` on their `requests` field, as
+mandated in [AIP-235][].
+
+## Details
+
+This rule looks at any message matching `BatchDelete*Request` and complains if the
+`requests` field does not have a `google.api.field_behavior` annotation with a
+value of `REQUIRED`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message BatchDeleteBooksRequest {
+  string parent = 1 [
+    (google.api.resource_reference).child_type = "library.googleapis.com/Book"
+  ];
+
+  // The `google.api.field_behavior` annotation should be included.
+  repeated DeleteBookRequest requests = 2;
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message BatchDeleteBooksRequest {
+  string parent = 1 [
+    (google.api.resource_reference).child_type = "library.googleapis.com/Book"
+  ];
+
+  repeated DeleteBookRequest requests = 2 [
+    (google.api.field_behavior) = REQUIRED
+  ];
+}
+```
+
+## 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
+message BatchDeleteBooksRequest {
+  string parent = 1 [
+    (google.api.resource_reference).child_type = "library.googleapis.com/Book"
+  ];
+
+  // (-- api-linter: core::0235::request-requests-behavior=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  repeated DeleteBookRequest requests = 2;
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-235]: https://aip.dev/235
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0233/aip0233.go b/rules/aip0233/aip0233.go
index 73007bc..4e5d651 100644
--- a/rules/aip0233/aip0233.go
+++ b/rules/aip0233/aip0233.go
@@ -34,6 +34,7 @@ func AddRules(r lint.RuleRegistry) error {
 		httpUriSuffix,
 		httpVerb,
 		requestParentField,
+		requestRequestsBehavior,
 		requestRequestsField,
 		responseResourceField,
 	)
diff --git a/rules/aip0233/request_requests_behavior.go b/rules/aip0233/request_requests_behavior.go
new file mode 100644
index 0000000..639e4ed
--- /dev/null
+++ b/rules/aip0233/request_requests_behavior.go
@@ -0,0 +1,37 @@
+// Copyright 2020 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 aip0233
+
+import (
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var requestRequestsBehavior = &lint.FieldRule{
+	Name: lint.NewRuleName(233, "request-requests-behavior"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isBatchCreateRequestMessage(f.GetOwner()) && f.GetName() == "requests"
+	},
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		if !utils.GetFieldBehavior(f).Contains("REQUIRED") {
+			return []lint.Problem{{
+				Message:    "Batch Create requests: The `requests` field should include `(google.api.field_behavior) = REQUIRED`.",
+				Descriptor: f,
+			}}
+		}
+		return nil
+	},
+}
diff --git a/rules/aip0234/aip0234.go b/rules/aip0234/aip0234.go
index 1b6205e..f3f6d8e 100644
--- a/rules/aip0234/aip0234.go
+++ b/rules/aip0234/aip0234.go
@@ -33,6 +33,7 @@ func AddRules(r lint.RuleRegistry) error {
 		pluralMethodName,
 		requestMessageName,
 		requestParentField,
+		requestRequestsBehavior,
 		requestRequestsField,
 		responseMessageName,
 		responseResourceField,
diff --git a/rules/aip0234/request_requests_behavior.go b/rules/aip0234/request_requests_behavior.go
new file mode 100644
index 0000000..dde5f49
--- /dev/null
+++ b/rules/aip0234/request_requests_behavior.go
@@ -0,0 +1,37 @@
+// Copyright 2020 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 aip0234
+
+import (
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var requestRequestsBehavior = &lint.FieldRule{
+	Name: lint.NewRuleName(234, "request-requests-behavior"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isBatchUpdateRequestMessage(f.GetOwner()) && f.GetName() == "requests"
+	},
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		if !utils.GetFieldBehavior(f).Contains("REQUIRED") {
+			return []lint.Problem{{
+				Message:    "Batch Update requests: The `requests` field should include `(google.api.field_behavior) = REQUIRED`.",
+				Descriptor: f,
+			}}
+		}
+		return nil
+	},
+}
diff --git a/rules/aip0235/aip0235.go b/rules/aip0235/aip0235.go
index 0d268a0..b4f4037 100644
--- a/rules/aip0235/aip0235.go
+++ b/rules/aip0235/aip0235.go
@@ -36,6 +36,7 @@ func AddRules(r lint.RuleRegistry) error {
 		requestNamesField,
 		requestNamesReference,
 		requestParentField,
+		requestRequestsBehavior,
 	)
 }
 
diff --git a/rules/aip0235/request_requests_behavior.go b/rules/aip0235/request_requests_behavior.go
new file mode 100644
index 0000000..6fd312c
--- /dev/null
+++ b/rules/aip0235/request_requests_behavior.go
@@ -0,0 +1,37 @@
+// Copyright 2020 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 aip0235
+
+import (
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var requestRequestsBehavior = &lint.FieldRule{
+	Name: lint.NewRuleName(235, "request-requests-behavior"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isBatchDeleteRequestMessage(f.GetOwner()) && f.GetName() == "requests"
+	},
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		if !utils.GetFieldBehavior(f).Contains("REQUIRED") {
+			return []lint.Problem{{
+				Message:    "Batch Delete requests: The `requests` field should include `(google.api.field_behavior) = REQUIRED`.",
+				Descriptor: f,
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0233/request_requests_behavior_test.go b/rules/aip0233/request_requests_behavior_test.go
new file mode 100644
index 0000000..311e85c
--- /dev/null
+++ b/rules/aip0233/request_requests_behavior_test.go
@@ -0,0 +1,49 @@
+// Copyright 2020 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 aip0233
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestRequestRequestsBehavior(t *testing.T) {
+	for _, test := range []struct {
+		name          string
+		MessageName   string
+		FieldName     string
+		FieldBehavior string
+		problems      testutils.Problems
+	}{
+		{"Valid", "BatchCreateBooks", "requests", " [(google.api.field_behavior) = REQUIRED]", testutils.Problems{}},
+		{"Missing", "BatchCreateBooks", "requests", "", testutils.Problems{{Message: "(google.api.field_behavior) = REQUIRED"}}},
+		{"IrrelevantMessage", "CreateBooks", "requests", "", testutils.Problems{}},
+		{"IrrelevantField", "BatchCreateBooks", "something_else", "", testutils.Problems{}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				import "google/api/field_behavior.proto";
+				message {{.MessageName}}Request {
+					repeated string {{.FieldName}} = 1{{.FieldBehavior}};
+				}
+			`, test)
+			field := f.GetMessageTypes()[0].GetFields()[0]
+			if diff := test.problems.SetDescriptor(field).Diff(requestRequestsBehavior.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}
diff --git a/rules/aip0234/request_requests_behavior_test.go b/rules/aip0234/request_requests_behavior_test.go
new file mode 100644
index 0000000..e93c2e7
--- /dev/null
+++ b/rules/aip0234/request_requests_behavior_test.go
@@ -0,0 +1,49 @@
+// Copyright 2020 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 aip0234
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestRequestRequestsBehavior(t *testing.T) {
+	for _, test := range []struct {
+		name          string
+		MessageName   string
+		FieldName     string
+		FieldBehavior string
+		problems      testutils.Problems
+	}{
+		{"Valid", "BatchUpdateBooks", "requests", " [(google.api.field_behavior) = REQUIRED]", testutils.Problems{}},
+		{"Missing", "BatchUpdateBooks", "requests", "", testutils.Problems{{Message: "(google.api.field_behavior) = REQUIRED"}}},
+		{"IrrelevantMessage", "UpdateBooks", "requests", "", testutils.Problems{}},
+		{"IrrelevantField", "BatchUpdateBooks", "something_else", "", testutils.Problems{}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				import "google/api/field_behavior.proto";
+				message {{.MessageName}}Request {
+					repeated string {{.FieldName}} = 1{{.FieldBehavior}};
+				}
+			`, test)
+			field := f.GetMessageTypes()[0].GetFields()[0]
+			if diff := test.problems.SetDescriptor(field).Diff(requestRequestsBehavior.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}
diff --git a/rules/aip0235/request_requests_behavior_test.go b/rules/aip0235/request_requests_behavior_test.go
new file mode 100644
index 0000000..b5d1a38
--- /dev/null
+++ b/rules/aip0235/request_requests_behavior_test.go
@@ -0,0 +1,49 @@
+// Copyright 2020 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 aip0235
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestRequestRequestsBehavior(t *testing.T) {
+	for _, test := range []struct {
+		name          string
+		MessageName   string
+		FieldName     string
+		FieldBehavior string
+		problems      testutils.Problems
+	}{
+		{"Valid", "BatchDeleteBooks", "requests", " [(google.api.field_behavior) = REQUIRED]", testutils.Problems{}},
+		{"Missing", "BatchDeleteBooks", "requests", "", testutils.Problems{{Message: "(google.api.field_behavior) = REQUIRED"}}},
+		{"IrrelevantMessage", "DeleteBooks", "requests", "", testutils.Problems{}},
+		{"IrrelevantField", "BatchDeleteBooks", "something_else", "", testutils.Problems{}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				import "google/api/field_behavior.proto";
+				message {{.MessageName}}Request {
+					repeated string {{.FieldName}} = 1{{.FieldBehavior}};
+				}
+			`, test)
+			field := f.GetMessageTypes()[0].GetFields()[0]
+			if diff := test.problems.SetDescriptor(field).Diff(requestRequestsBehavior.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}