Repository: googleapis/api-linter
Pull request #835 (2021-04-08 20:10:53)

feat: Require AIP-135 request force fields to be singular bool


Patch:
diff --git a/docs/rules/0135/request-force-field.md b/docs/rules/0135/request-force-field.md
new file mode 100644
index 0000000..553d132
--- /dev/null
+++ b/docs/rules/0135/request-force-field.md
@@ -0,0 +1,71 @@
+---
+rule:
+  aip: 135
+  name: [core, '0135', request-force-field]
+  summary: Delete request `force` fields must have type `bool`.
+permalink: /135/request-force-field
+redirect_from:
+  - /0135/request-force-field
+---
+
+# Delete requests: force field
+
+This rule enforces that all `Delete` request `force` fields have type `bool`, as
+mandated in [AIP-135][].
+
+## Details
+
+This rule looks at any message matching `Delete*Request` that contains a `force`
+field and complains if the field is not a singular `bool`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+message DeletePublisherRequest {
+  string name = 1 [
+    (google.api.resource_reference).type = "library.googleapis.com/Publisher",
+    (google.api.field_behavior) = REQUIRED
+  ];
+
+  int32 force = 2;  // Field type should be `bool`.
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+message DeletePublisherRequest {
+  string name = 1 [
+    (google.api.resource_reference).type = "library.googleapis.com/Publisher",
+    (google.api.field_behavior) = REQUIRED
+  ];
+
+  bool force = 2;
+}
+```
+
+## 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 DeletePublisherRequest {
+  string name = 1 [
+    (google.api.resource_reference).type = "library.googleapis.com/Publisher",
+    (google.api.field_behavior) = REQUIRED
+  ];
+
+  // (-- api-linter: core::0135::request-force-field=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  int32 force = 2;
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-135]: https://aip.dev/135
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0135/aip0135.go b/rules/aip0135/aip0135.go
index 8458ac1..192beb4 100644
--- a/rules/aip0135/aip0135.go
+++ b/rules/aip0135/aip0135.go
@@ -33,6 +33,7 @@ func AddRules(r lint.RuleRegistry) error {
 		methodSignature,
 		responseMessageName,
 		requestMessageName,
+		requestForceField,
 		requestNameBehavior,
 		requestNameField,
 		requestNameReference,
diff --git a/rules/aip0135/request_force_field.go b/rules/aip0135/request_force_field.go
new file mode 100644
index 0000000..5c4347e
--- /dev/null
+++ b/rules/aip0135/request_force_field.go
@@ -0,0 +1,29 @@
+// 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 aip0135
+
+import (
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var requestForceField = &lint.FieldRule{
+	Name: lint.NewRuleName(135, "request-force-field"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isDeleteRequestMessage(f.GetOwner()) && f.GetName() == "force"
+	},
+	LintField: utils.LintSingularBoolField,
+}


Test patch:
diff --git a/rules/aip0135/request_force_field_test.go b/rules/aip0135/request_force_field_test.go
new file mode 100644
index 0000000..915b1d5
--- /dev/null
+++ b/rules/aip0135/request_force_field_test.go
@@ -0,0 +1,52 @@
+// 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 aip0135
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestRequestForceField(t *testing.T) {
+	tests := []struct {
+		name        string
+		MessageName string
+		FieldType   string
+		FieldName   string
+		problems    testutils.Problems
+	}{
+		{"Valid", "DeleteBookRequest", "bool", "force", nil},
+		{"Invalid", "DeleteBookRequest", "int32", "force", testutils.Problems{{Suggestion: "bool"}}},
+		{"InvalidRepeated", "DeleteBookRequest", "repeated bool", "force", testutils.Problems{{Suggestion: "bool"}}},
+		{"IrrelevantMessage", "RemoveBookRequest", "int32", "force", nil},
+		{"IrrelevantField", "DeleteBookRequest", "string", "name", nil},
+	}
+
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				message {{.MessageName}} {
+					{{.FieldType}} {{.FieldName}} = 1;
+				}
+			`, test)
+			field := f.GetMessageTypes()[0].GetFields()[0]
+			problems := requestForceField.Lint(f)
+			if diff := test.problems.SetDescriptor(field).Diff(problems); diff != "" {
+				t.Error(diff)
+			}
+		})
+	}
+}