Repository: googleapis/api-linter
Pull request #436 (2019-12-06 22:19:19)

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


Patch:
diff --git a/docs/rules/0135/method-signature.md b/docs/rules/0135/method-signature.md
new file mode 100644
index 0000000..c2bf3e3
--- /dev/null
+++ b/docs/rules/0135/method-signature.md
@@ -0,0 +1,69 @@
+---
+rule:
+  aip: 135
+  name: [core, '0135', method-signature]
+  summary: |
+    Delete RPCs should annotate a method signature of "name".
+permalink: /135/method-signature
+redirect_from:
+  - /0135/method-signature
+---
+
+# Delete methods: Method signature
+
+This rule enforces that all `Delete` standard methods have a
+`google.api.method_signature` annotation with a value of `"name"`, as mandated
+in [AIP-135][].
+
+## Details
+
+This rule looks at any method beginning with `Delete`, and complains if the
+`google.api.method_signature` annotation is missing, or if it is set to any
+value other than `"name"`. Additional method signatures, if present, are
+ignored.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc DeleteBook(DeleteBookRequest) returns (google.protobuf.Empty) {
+  // A google.api.method_signature annotation should be present.
+}
+```
+
+```proto
+// Incorrect.
+rpc DeleteBook(DeleteBookRequest) returns (google.protobuf.Empty) {
+  option (google.api.method_signature) = "book";  // Should be "name".
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc DeleteBook(DeleteBookRequest) returns (google.protobuf.Empty) {
+  option (google.api.method_signature) = "name";
+}
+```
+
+## 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::0135::method-signature=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc DeleteBook(DeleteBookRequest) returns (google.protobuf.Empty) {
+  option (google.api.method_signature) = "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 60ecbf1..eb180e8 100644
--- a/rules/aip0135/aip0135.go
+++ b/rules/aip0135/aip0135.go
@@ -30,6 +30,7 @@ func AddRules(r lint.RuleRegistry) error {
 		httpBody,
 		httpMethod,
 		httpNameField,
+		methodSignature,
 		responseMessageName,
 		requestMessageName,
 		requestNameBehavior,
diff --git a/rules/aip0135/method_signature.go b/rules/aip0135/method_signature.go
new file mode 100644
index 0000000..9935dd4
--- /dev/null
+++ b/rules/aip0135/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 aip0135
+
+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(135, "method-signature"),
+	OnlyIf: isDeleteMethod,
+	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(
+					"Delete methods should include `(google.api.method_signature) = %q`",
+					"name",
+				),
+				Descriptor: m,
+			}}
+		}
+
+		// Check if the signature is wrong.
+		if !reflect.DeepEqual(signatures[0], []string{"name"}) {
+			return []lint.Problem{{
+				Message:    `The method signature for Delete methods should be "name".`,
+				Suggestion: `option (google.api.method_signature) = "name";`,
+				Descriptor: m,
+				Location:   locations.MethodSignature(m, 0),
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0135/method_signature_test.go b/rules/aip0135/method_signature_test.go
new file mode 100644
index 0000000..c201c2c
--- /dev/null
+++ b/rules/aip0135/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 aip0135
+
+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", "DeleteBook", `option (google.api.method_signature) = "name";`, testutils.Problems{}},
+		{"Missing", "DeleteBook", "", testutils.Problems{{Message: `(google.api.method_signature) = "name"`}}},
+		{
+			"Wrong",
+			"DeleteBook",
+			`option (google.api.method_signature) = "book";`,
+			testutils.Problems{{Suggestion: `option (google.api.method_signature) = "name";`}},
+		},
+		{"Irrelevant", "BurnBook", "", testutils.Problems{}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				import "google/api/client.proto";
+				import "google/protobuf/empty.proto";
+				service Library {
+					rpc {{.MethodName}}({{.MethodName}}Request) returns (google.protobuf.Empty) {
+						{{.Signature}}
+					}
+				}
+				message {{.MethodName}}Request {}
+			`, test)
+			m := f.GetServices()[0].GetMethods()[0]
+			if diff := test.problems.SetDescriptor(m).Diff(methodSignature.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}