Repository: googleapis/api-linter
Pull request #628 (2020-09-19 12:45:22)

Add plural-method-name rule for AIP-235
Adapted from that of AIP-234 for #612.


Patch:
diff --git a/docs/rules/0235/plural-method-name.md b/docs/rules/0235/plural-method-name.md
new file mode 100644
index 0000000..16655e9
--- /dev/null
+++ b/docs/rules/0235/plural-method-name.md
@@ -0,0 +1,68 @@
+---
+rule:
+  aip: 235
+  name: [core, '0235', plural-method-name]
+  summary: Batch Delete methods must have plural names.
+permalink: /235/plural-method-name
+redirect_from:
+  - /0235/plural-method-name
+---
+
+# Batch Delete methods: Plural method name
+
+This rule enforces that all `BatchDelete` RPCs have a plural resource in the
+method name, as mandated in [AIP-235][].
+
+## Details
+
+This rule looks at any method whose name begins with `BatchDelete`, and complains
+if the name of the resource in the method name is singular.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+// Method name should be `BatchDeleteBooks`
+rpc BatchDeleteBook(BatchDeleteBookRequest) returns (google.protobuf.Empty) {
+  option (google.api.http) = {
+    post: "/v1/{parent=publishers/*}/books:batchDelete"
+    body: "*"
+  };
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc BatchDeleteBooks(BatchDeleteBooksRequest) returns (google.protobuf.Empty) {
+  option (google.api.http) = {
+    post: "/v1/{parent=publishers/*}/books:batchDelete"
+    body: "*"
+  };
+}
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the method.
+Remember to also include an [aip.dev/not-precedent][] comment explaining why.
+
+```proto
+// (-- api-linter: core::0235::plural-method-name=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc BatchDeleteBook(BatchDeleteBookRequest) returns (google.protobuf.Empty) {
+  option (google.api.http) = {
+    post: "/v1/{parent=publishers/*}/books:batchDelete"
+    body: "*"
+  };
+}
+```
+
+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/aip0235/aip0235.go b/rules/aip0235/aip0235.go
index 6ecf9ad..7242022 100644
--- a/rules/aip0235/aip0235.go
+++ b/rules/aip0235/aip0235.go
@@ -29,6 +29,7 @@ func AddRules(r lint.RuleRegistry) error {
 		235,
 		httpBody,
 		httpMethod,
+		pluralMethodName,
 		requestMessageName,
 	)
 }
diff --git a/rules/aip0235/plural_method_name.go b/rules/aip0235/plural_method_name.go
new file mode 100644
index 0000000..70c16c3
--- /dev/null
+++ b/rules/aip0235/plural_method_name.go
@@ -0,0 +1,52 @@
+// 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 (
+	"fmt"
+	"strings"
+
+	"github.com/gertd/go-pluralize"
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/locations"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var pluralMethodName = &lint.MethodRule{
+	Name:   lint.NewRuleName(235, "plural-method-name"),
+	OnlyIf: isBatchDeleteMethod,
+	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
+		// Note: Retrieve the resource name from the method name. For example,
+		// "BatchDeleteFoos" -> "Foos"
+		pluralMethodResourceName := strings.TrimPrefix(m.GetName(), "BatchDelete")
+
+		pluralize := pluralize.NewClient()
+
+		// Rule check: Establish that for methods such as `BatchDeleteFoos`
+		if !pluralize.IsPlural(pluralMethodResourceName) {
+			return []lint.Problem{{
+				Message: fmt.Sprintf(
+					`The resource part in method name %q shouldn't be %q, but should be its plural form %q`,
+					m.GetName(), pluralMethodResourceName, pluralize.Plural(pluralMethodResourceName),
+				),
+				Descriptor: m,
+				Location:   locations.DescriptorName(m),
+				Suggestion: "BatchDelete" + pluralize.Plural(pluralMethodResourceName),
+			}}
+		}
+
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0235/plural_method_name_test.go b/rules/aip0235/plural_method_name_test.go
new file mode 100644
index 0000000..c47e3a5
--- /dev/null
+++ b/rules/aip0235/plural_method_name_test.go
@@ -0,0 +1,95 @@
+// 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 TestMethodPluralResourceName(t *testing.T) {
+	// Set up the testing permutations.
+	tests := []struct {
+		testName   string
+		MethodName string
+		UriSuffix  string
+		problems   testutils.Problems
+	}{
+		{
+			testName:   "Valid-BatchDeleteBooks",
+			MethodName: "BatchDeleteBooks",
+			UriSuffix:  "books:batchDelete",
+			problems:   testutils.Problems{},
+		},
+		{
+			testName:   "Valid-BatchDeleteMen",
+			MethodName: "BatchDeleteMen",
+			UriSuffix:  "men:batchDelete",
+			problems:   testutils.Problems{},
+		},
+		{
+			testName:   "Invalid-SingularBus",
+			MethodName: "BatchDeleteBus",
+			UriSuffix:  "bus:batchDelete",
+			problems: testutils.Problems{{
+				Suggestion: "BatchDeleteBuses",
+			}},
+		},
+		{
+			testName:   "Invalid-SingularCorpPerson",
+			MethodName: "BatchDeleteCorpPerson",
+			UriSuffix:  "corpPerson:batchDelete",
+			problems: testutils.Problems{{
+				Suggestion: "BatchDeleteCorpPeople",
+			}},
+		},
+		{
+			testName:   "Invalid-Irrelevant",
+			MethodName: "AcquireBook",
+			UriSuffix:  "book",
+			problems:   testutils.Problems{},
+		},
+	}
+
+	// Run each test individually.
+	for _, test := range tests {
+		t.Run(test.testName, func(t *testing.T) {
+			file := testutils.ParseProto3Tmpl(t, `
+				import "google/api/annotations.proto";
+				
+				service BookService {
+					rpc {{.MethodName}}({{.MethodName}}Request) returns ({{.MethodName}}Response) {
+						option (google.api.http) = {
+							post: "/v1/{parent=publishers/*}/{{.UriSuffix}}"
+							body: "*"
+						};
+					}
+				}
+				
+				message {{.MethodName}}Request {}
+				
+				message {{.MethodName}}Response{}
+				`, test)
+
+			m := file.GetServices()[0].GetMethods()[0]
+
+			problems := pluralMethodName.Lint(file)
+			if diff := test.problems.SetDescriptor(m).Diff(problems); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}