Repository: googleapis/api-linter
Pull request #427 (2019-12-06 18:03:08)

[AIP-135] Delete: Name field should be required.


Patch:
diff --git a/docs/rules/0131/request-name-behavior.md b/docs/rules/0131/request-name-behavior.md
index 6f778a2..9655992 100644
--- a/docs/rules/0131/request-name-behavior.md
+++ b/docs/rules/0131/request-name-behavior.md
@@ -53,9 +53,9 @@ 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::0131::request-name-behavior=disabled
-//     aip.dev/not-precedent: We need to do this because reasons. --)
 message GetBookRequest {
+  // (-- api-linter: core::0131::request-name-behavior=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
   string name = 1 [(google.api.resource_reference) = {
     type: "library.googleapis.com/Book"
   }];
diff --git a/docs/rules/0135/request-name-behavior.md b/docs/rules/0135/request-name-behavior.md
new file mode 100644
index 0000000..800c4d3
--- /dev/null
+++ b/docs/rules/0135/request-name-behavior.md
@@ -0,0 +1,69 @@
+---
+rule:
+  aip: 135
+  name: [core, '0135', request-name-behavior]
+  summary: |
+    Delete RPCs should annotate the `name` field with `google.api.field_behavior`.
+permalink: /135/request-name-behavior
+redirect_from:
+  - /0135/request-name-behavior
+---
+
+# Delete methods: Field behavior
+
+This rule enforces that all `Delete` standard methods have
+`google.api.field_behavior` set to `REQUIRED` on their `string name` field, as
+mandated in [AIP-135][].
+
+## Details
+
+This rule looks at any message matching `Delete*Request` and complains if the
+`name` field does not have a `google.api.field_behavior` annotation with a
+value of `REQUIRED`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message DeleteBookRequest {
+  // The `google.api.field_behavior` annotation should also be included.
+  string name = 1 [(google.api.resource_reference) = {
+    type: "library.googleapis.com/Book"
+  }];
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message DeleteBookRequest {
+  string name = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+}
+```
+
+## 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 DeleteBookRequest {
+  // (-- api-linter: core::0135::request-name-behavior=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  string name = 1 [(google.api.resource_reference) = {
+    type: "library.googleapis.com/Book"
+  }];
+}
+```
+
+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 b9f12d2..60ecbf1 100644
--- a/rules/aip0135/aip0135.go
+++ b/rules/aip0135/aip0135.go
@@ -32,6 +32,7 @@ func AddRules(r lint.RuleRegistry) error {
 		httpNameField,
 		responseMessageName,
 		requestMessageName,
+		requestNameBehavior,
 		requestNameReference,
 		standardFields,
 		unknownFields,
diff --git a/rules/aip0135/request_name_behavior.go b/rules/aip0135/request_name_behavior.go
new file mode 100644
index 0000000..cff9ff6
--- /dev/null
+++ b/rules/aip0135/request_name_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 aip0135
+
+import (
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var requestNameBehavior = &lint.FieldRule{
+	Name: lint.NewRuleName(135, "request-name-behavior"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isDeleteRequestMessage(f.GetOwner()) && f.GetName() == "name"
+	},
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		if !utils.GetFieldBehavior(f).Contains("REQUIRED") {
+			return []lint.Problem{{
+				Message:    "Delete requests: The `name` field should include `(google.api.field_behavior) = REQUIRED`.",
+				Descriptor: f,
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0135/request_name_behavior_test.go b/rules/aip0135/request_name_behavior_test.go
new file mode 100644
index 0000000..c32071b
--- /dev/null
+++ b/rules/aip0135/request_name_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 aip0135
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestRequestNameBehavior(t *testing.T) {
+	for _, test := range []struct {
+		name          string
+		FieldName     string
+		FieldBehavior string
+		problems      testutils.Problems
+	}{
+		{"Valid", "name", " [(google.api.field_behavior) = REQUIRED]", testutils.Problems{}},
+		{"Missing", "name", "", 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 DeleteBookRequest {
+					string {{.FieldName}} = 1{{.FieldBehavior}};
+				}
+			`, test)
+			field := f.GetMessageTypes()[0].GetFields()[0]
+			if diff := test.problems.SetDescriptor(field).Diff(requestNameBehavior.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}