Repository: googleapis/api-linter
Pull request #433 (2019-12-06 20:57:22)

[AIP-132] Enforce a consistent method signature.
Uses #431 as a base.


Patch:
diff --git a/docs/rules/0132/method-signature.md b/docs/rules/0132/method-signature.md
new file mode 100644
index 0000000..a64cf24
--- /dev/null
+++ b/docs/rules/0132/method-signature.md
@@ -0,0 +1,67 @@
+---
+rule:
+  aip: 132
+  name: [core, '0132', method-signature]
+  summary: |
+    List RPCs should annotate a method signature of "parent".
+permalink: /132/method-signature
+redirect_from:
+  - /0132/method-signature
+---
+
+# List methods: Method signature
+
+This rule enforces that all `List` standard methods have a
+`google.api.method_signature` annotation with a value of `"parent"`, as
+mandated in [AIP-132][].
+
+## Details
+
+This rule looks at any method beginning with `List`, and complains if the
+`google.api.method_signature` annotation is missing, or if it is set to any
+value other than `"parent"`. Additional method signatures, if present, are
+ignored.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc ListBooks(ListBooksRequest) returns (Book) {
+  // A google.api.method_signature annotation should be present.
+}
+```
+
+```proto
+// Incorrect.
+rpc ListBooks(ListBooksRequest) returns (Book) {
+  option (google.api.method_signature) = "publisher";  // Should be "parent".
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc ListBooks(ListBooksRequest) returns (Book) {
+  option (google.api.method_signature) = "parent";
+}
+```
+
+## 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::0132::method-signature=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc ListBooks(ListBooksRequest) returns (Book);
+```
+
+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 20e4c39..e13b1bb 100644
--- a/rules/aip0132/aip0132.go
+++ b/rules/aip0132/aip0132.go
@@ -26,6 +26,7 @@ import (
 func AddRules(r lint.RuleRegistry) error {
 	return r.Register(
 		132,
+		methodSignature,
 		requestFieldTypes,
 		requestMessageName,
 		requestParentBehavior,
diff --git a/rules/aip0132/method_signature.go b/rules/aip0132/method_signature.go
new file mode 100644
index 0000000..709329b
--- /dev/null
+++ b/rules/aip0132/method_signature.go
@@ -0,0 +1,55 @@
+// 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 (
+	"fmt"
+	"reflect"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/locations"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var methodSignature = &lint.MethodRule{
+	Name:   lint.NewRuleName(132, "method-signature"),
+	OnlyIf: isListMethod,
+	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
+		signatures := utils.GetMethodSignatures(m)
+
+		// Check if the signature is missing.
+		if len(signatures) == 0 {
+			return []lint.Problem{{
+				Message: fmt.Sprintf(
+					"List methods should include `(google.api.method_signature) = %q`",
+					"parent",
+				),
+				Descriptor: m,
+			}}
+		}
+
+		// Check if the signature is wrong.
+		if !reflect.DeepEqual(signatures[0], []string{"parent"}) {
+			return []lint.Problem{{
+				Message:    `The method signature for List methods should be "parent".`,
+				Suggestion: `option (google.api.method_signature) = "parent";`,
+				Descriptor: m,
+				Location:   locations.MethodSignature(m, 0),
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0132/method_signature_test.go b/rules/aip0132/method_signature_test.go
new file mode 100644
index 0000000..a2e84e8
--- /dev/null
+++ b/rules/aip0132/method_signature_test.go
@@ -0,0 +1,57 @@
+// 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 TestMethodSignature(t *testing.T) {
+	for _, test := range []struct {
+		name       string
+		MethodName string
+		Signature  string
+		problems   testutils.Problems
+	}{
+		{"Valid", "ListBooks", `option (google.api.method_signature) = "parent";`, testutils.Problems{}},
+		{"Missing", "ListBooks", "", testutils.Problems{{Message: `(google.api.method_signature) = "parent"`}}},
+		{
+			"Wrong",
+			"ListBooks",
+			`option (google.api.method_signature) = "publisher";`,
+			testutils.Problems{{Suggestion: `option (google.api.method_signature) = "parent";`}},
+		},
+		{"Irrelevant", "BrowseBooks", "", testutils.Problems{}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				import "google/api/client.proto";
+				service Library {
+					rpc {{.MethodName}}({{.MethodName}}Request) returns (Book) {
+						{{.Signature}}
+					}
+				}
+				message {{.MethodName}}Request {}
+				message Book {}
+			`, test)
+			m := f.GetServices()[0].GetMethods()[0]
+			if diff := test.problems.SetDescriptor(m).Diff(methodSignature.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}