Repository: googleapis/api-linter
Pull request #716 (2020-12-04 18:50:21)

feat: Add rule for AIP-165 long-running response type
Part of #704.


Patch:
diff --git a/docs/rules/0165/response-message-name.md b/docs/rules/0165/response-message-name.md
new file mode 100644
index 0000000..a9ae36f
--- /dev/null
+++ b/docs/rules/0165/response-message-name.md
@@ -0,0 +1,90 @@
+---
+rule:
+  aip: 165
+  name: [core, '0165', response-message-name]
+  summary: Purge methods must return a long-running operation.
+permalink: /165/response-message-name
+redirect_from:
+  - /0165/response-message-name
+---
+
+# Purge methods: Response message
+
+This rule enforces that all `Purge` RPCs return a long-running operation that
+resolves to a response with a corresponding name, as mandated in [AIP-165][].
+
+## Details
+
+This rule looks at any message beginning with `Purge`, and complains if the
+response is not a long-running operation that resolves to a response matching
+the name of the method with a `Response` suffix.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+// Should be `google.longrunning.Operation`.
+rpc PurgeBooks(PurgeBooksRequest) returns (PurgeBooksResponse) {
+  option (google.api.http) = {
+    post: "/v1/{parent=publishers/*}/books:purge"
+    body: "*"
+  };
+}
+```
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc PurgeBooks(PurgeBooksRequest) returns (google.longrunning.Operation) {
+  option (google.api.http) = {
+    post: "/v1/{parent=publishers/*}/books:purge"
+    body: "*"
+  };
+  option (google.longrunning.operation_info) = {
+    // Should be "PurgeBooksResponse".
+    response_type: "Book"
+    metadata_type: "PurgeBooksMetadata"
+  };
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc PurgeBooks(PurgeBooksRequest) returns (google.longrunning.Operation) {
+  option (google.api.http) = {
+    post: "/v1/{parent=publishers/*}/books:purge"
+    body: "*"
+  };
+  option (google.longrunning.operation_info) = {
+    response_type: "PurgeBooksResponse"
+    metadata_type: "PurgeBooksMetadata"
+  };
+}
+```
+
+## 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::0165::response-message-name=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc PurgeBooks(PurgeBooksRequest) returns (PurgeBooksResponse) {
+  option (google.api.http) = {
+    post: "/v1/{parent=publishers/*}/books:purge"
+    body: "*"
+  };
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-165]: https://aip.dev/165
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0165/aip0165.go b/rules/aip0165/aip0165.go
index 656060c..0177c53 100644
--- a/rules/aip0165/aip0165.go
+++ b/rules/aip0165/aip0165.go
@@ -36,6 +36,7 @@ func AddRules(r lint.RuleRegistry) error {
 		requestParentBehavior,
 		requestParentField,
 		requestParentReference,
+		responseMessageName,
 		responsePurgeCountField,
 		responsePurgeSampleField,
 		responsePurgeSampleReference,
diff --git a/rules/aip0165/response_message_name.go b/rules/aip0165/response_message_name.go
new file mode 100644
index 0000000..eb12e4f
--- /dev/null
+++ b/rules/aip0165/response_message_name.go
@@ -0,0 +1,60 @@
+// 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 aip0165
+
+import (
+	"fmt"
+
+	"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 responseMessageName = &lint.MethodRule{
+	Name:   lint.NewRuleName(165, "response-message-name"),
+	OnlyIf: isPurgeMethod,
+	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
+		if m.GetOutputType().GetFullyQualifiedName() != "google.longrunning.Operation" {
+			return []lint.Problem{{
+				Message:    "Purge methods should use an LRO.",
+				Descriptor: m,
+				Location:   locations.MethodResponseType(m),
+				Suggestion: "google.longrunning.Operation",
+			}}
+		}
+
+		// Rule check: Establish that for methods such as `PurgeBooks`, the
+		// response message should be named `Pu,geBooksResponse`.
+		//
+		// Note: If `got` is empty string, this is an unannotated LRO.
+		// The AIP-151 rule will whine about that, and this rule should not as it
+		// would be confusing.
+		got := utils.GetOperationInfo(m).GetResponseType()
+		want := m.GetName() + "Response"
+		if got != want && got != "" {
+			return []lint.Problem{{
+				Message: fmt.Sprintf(
+					"Purge RPCs should have response message type of `%s`, not %q.",
+					want, got,
+				),
+				Suggestion: want,
+				Descriptor: m,
+				Location:   locations.MethodResponseType(m),
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0165/response_message_name_test.go b/rules/aip0165/response_message_name_test.go
new file mode 100644
index 0000000..0d6ac00
--- /dev/null
+++ b/rules/aip0165/response_message_name_test.go
@@ -0,0 +1,55 @@
+// 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 aip0165
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestResponseMessageName(t *testing.T) {
+	for _, test := range []struct {
+		name         string
+		Method       string
+		ResponseType string
+		LROType      string
+		problems     testutils.Problems
+	}{
+		{"Valid", "PurgeBooks", "google.longrunning.Operation", "PurgeBooksResponse", nil},
+		{"InvalidNotLRO", "PurgeBooks", "PurgeBooksResponse", "", testutils.Problems{{Suggestion: "google.longrunning.Operation"}}},
+		{"InvalidLROType", "PurgeBooks", "google.longrunning.Operation", "PurgeBooksRequest", testutils.Problems{{Suggestion: "PurgeBooksResponse"}}},
+		{"Irrelevant", "AcquireBook", "PurgeBooksResponse", "", nil},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				import "google/longrunning/operations.proto";
+				service Library {
+					rpc {{.Method}}(PurgeBooksRequest) returns ({{.ResponseType}}) {
+						option (google.longrunning.operation_info) = {
+							response_type: "{{.LROType}}"
+						};
+					}
+				}
+				message PurgeBooksRequest {}
+				message PurgeBooksResponse {}
+			`, test)
+			m := f.GetServices()[0].GetMethods()[0]
+			if diff := test.problems.SetDescriptor(m).Diff(responseMessageName.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}