Repository: googleapis/api-linter
Pull request #435 (2019-12-06 22:13:00)

[AIP-134] Enforce a consistent method signature.


Patch:
diff --git a/docs/rules/0134/method-signature.md b/docs/rules/0134/method-signature.md
new file mode 100644
index 0000000..5b3c871
--- /dev/null
+++ b/docs/rules/0134/method-signature.md
@@ -0,0 +1,70 @@
+---
+rule:
+  aip: 134
+  name: [core, '0134', method-signature]
+  summary: |
+    Update RPCs should annotate an appropriate method signature.
+permalink: /134/method-signature
+redirect_from:
+  - /0134/method-signature
+---
+
+# Update methods: Method signature
+
+This rule enforces that all `Update` standard methods have a
+`google.api.method_signature` annotation with an appropirate value, as mandated
+in [AIP-134][].
+
+## Details
+
+This rule looks at any method beginning with `Update`, and complains if the
+`google.api.method_signature` annotation is missing, or if it is set to an
+value other than `"{resource},update_mask`. Additional method signatures, if
+present, are ignored.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc UpdateBook(UpdateBookRequest) returns (Book) {
+  // A google.api.method_signature annotation should be present.
+}
+```
+
+```proto
+// Incorrect.
+rpc UpdateBook(UpdateBookRequest) returns (Book) {
+  // Should be "book,update_mask".
+  option (google.api.method_signature) = "book";
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc UpdateBook(UpdateBookRequest) returns (Book) {
+  option (google.api.method_signature) = "book,update_mask";
+}
+```
+
+## 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::0134::method-signature=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc UpdateBook(UpdateBookRequest) returns (Book) {
+  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-134]: https://aip.dev/134
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0134/aip0134.go b/rules/aip0134/aip0134.go
index a51fc2b..a4e797d 100644
--- a/rules/aip0134/aip0134.go
+++ b/rules/aip0134/aip0134.go
@@ -32,6 +32,7 @@ func AddRules(r lint.RuleRegistry) error {
 		httpBody,
 		httpMethod,
 		httpNameField,
+		methodSignature,
 		responseMessageName,
 		requestMaskField,
 		requestMessageName,
diff --git a/rules/aip0134/method_signature.go b/rules/aip0134/method_signature.go
new file mode 100644
index 0000000..f869088
--- /dev/null
+++ b/rules/aip0134/method_signature.go
@@ -0,0 +1,67 @@
+// 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 aip0134
+
+import (
+	"fmt"
+	"reflect"
+	"strings"
+
+	"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"
+	"github.com/stoewer/go-strcase"
+)
+
+var methodSignature = &lint.MethodRule{
+	Name:   lint.NewRuleName(134, "method-signature"),
+	OnlyIf: isUpdateMethod,
+	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
+		signatures := utils.GetMethodSignatures(m)
+		want := []string{
+			strcase.SnakeCase(strings.TrimPrefix(m.GetName(), "Update")),
+			"update_mask",
+		}
+
+		// Check if the signature is missing.
+		if len(signatures) == 0 {
+			return []lint.Problem{{
+				Message: fmt.Sprintf(
+					"Update methods should include `(google.api.method_signature) = %q`",
+					strings.Join(want, ","),
+				),
+				Descriptor: m,
+			}}
+		}
+
+		// Check if the signature is wrong.
+		if !reflect.DeepEqual(signatures[0], want) {
+			return []lint.Problem{{
+				Message: fmt.Sprintf(
+					"The method signature for Update methods should be %q.",
+					strings.Join(want, ","),
+				),
+				Suggestion: fmt.Sprintf(
+					"option (google.api.method_signature) = %q;",
+					strings.Join(want, ","),
+				),
+				Descriptor: m,
+				Location:   locations.MethodSignature(m, 0),
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0134/method_signature_test.go b/rules/aip0134/method_signature_test.go
new file mode 100644
index 0000000..19c744d
--- /dev/null
+++ b/rules/aip0134/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 aip0134
+
+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", "UpdateBook", `option (google.api.method_signature) = "book,update_mask";`, testutils.Problems{}},
+		{"Missing", "UpdateBook", "", testutils.Problems{{Message: `(google.api.method_signature) = "book,update_mask"`}}},
+		{
+			"Wrong",
+			"UpdateBook",
+			`option (google.api.method_signature) = "book";`,
+			testutils.Problems{{Suggestion: `option (google.api.method_signature) = "book,update_mask";`}},
+		},
+		{"Irrelevant", "EditBook", "", 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)
+			}
+		})
+	}
+}