Repository: googleapis/api-linter
Pull request #630 (2020-09-24 16:10:40)

feat: Add http-uri-suffix rule for AIP-235.
Adapted from that of AIP-234 for #612.


Patch:
diff --git a/docs/rules/0235/http-uri-suffix.md b/docs/rules/0235/http-uri-suffix.md
new file mode 100644
index 0000000..74e8e4f
--- /dev/null
+++ b/docs/rules/0235/http-uri-suffix.md
@@ -0,0 +1,67 @@
+---
+rule:
+  aip: 235
+  name: [core, '0235', http-uri-suffix]
+  summary: Batch Delete methods must have the correct URI suffix
+permalink: /235/http-uri-suffix
+redirect_from:
+  - /0235/http-uri-suffix
+---
+
+# Batch Delete methods: URI suffix
+
+This rule enforces that `BatchDelete` methods include the `:batchDelete` suffix
+in the REST URI, as mandated in [AIP-235][].
+
+## Details
+
+This rule looks at any method whose name starts with `BatchDelete`, and
+complains if the HTTP URI does not end with `:batchDelete`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc BatchDeleteBooks(BatchDeleteBooksRequest) returns (google.protobuf.Empty) {
+  option (google.api.http) = {
+    post: "/v1/{parent=publishers/*}/books:batch" // Should end with `: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::http-uri-suffix=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc BatchDeleteBooks(BatchDeleteBooksRequest) returns (google.protobuf.Empty) {
+  option (google.api.http) = {
+    post: "/v1/{parent=publishers/*}/books:batch" // Should end with `: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 7242022..9758007 100644
--- a/rules/aip0235/aip0235.go
+++ b/rules/aip0235/aip0235.go
@@ -29,12 +29,14 @@ func AddRules(r lint.RuleRegistry) error {
 		235,
 		httpBody,
 		httpMethod,
+		httpUriSuffix,
 		pluralMethodName,
 		requestMessageName,
 	)
 }
 
 var batchDeleteMethodRegexp = regexp.MustCompile("^BatchDelete(?:[A-Z]|$)")
+var batchDeleteURIRegexp = regexp.MustCompile(`:batchDelete$`)
 
 // Returns true if this is a AIP-235 Batch Delete method, false otherwise.
 func isBatchDeleteMethod(m *desc.MethodDescriptor) bool {
diff --git a/rules/aip0235/http_uri_suffix.go b/rules/aip0235/http_uri_suffix.go
new file mode 100644
index 0000000..03d4ffd
--- /dev/null
+++ b/rules/aip0235/http_uri_suffix.go
@@ -0,0 +1,39 @@
+// 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 (
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+// Batch Delete methods should have a proper HTTP pattern.
+var httpUriSuffix = &lint.MethodRule{
+	Name:   lint.NewRuleName(235, "http-uri-suffix"),
+	OnlyIf: isBatchDeleteMethod,
+	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
+		for _, httpRule := range utils.GetHTTPRules(m) {
+			if !batchDeleteURIRegexp.MatchString(httpRule.URI) {
+				return []lint.Problem{{
+					Message:    `Batch Delete URI should end with ":batchDelete".`,
+					Descriptor: m,
+				}}
+			}
+		}
+
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0235/http_uri_suffix_test.go b/rules/aip0235/http_uri_suffix_test.go
new file mode 100644
index 0000000..92b1d32
--- /dev/null
+++ b/rules/aip0235/http_uri_suffix_test.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 aip0235
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestHttpUriSuffix(t *testing.T) {
+	tests := []struct {
+		testName   string
+		HttpUri    string
+		MethodName string
+		problems   testutils.Problems
+	}{
+		{"Valid", "/v1/{parent=publishers/*}/books:batchDelete", "BatchDeleteBooks", nil},
+		{"ValidNoParent", "/v1/books:batchDelete", "BatchDeleteBooks", nil},
+		{"InvalidSuffix", "/v1/{parent=publishers/*}/books", "BatchDeleteBooks", testutils.Problems{{Message: ":batchDelete"}}},
+		{"Irrelevant", "/v1/{book=publishers/*/books/*}", "AcquireBook", nil},
+	}
+
+	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: "{{.HttpUri}}"
+							body: "*"
+						};
+					}
+				}
+				message {{.MethodName}}Request{}
+				message {{.MethodName}}Response{}
+				`, test)
+
+			// Run the method, ensure we get what we expect.
+			problems := httpUriSuffix.Lint(file)
+			if diff := test.problems.SetDescriptor(file.GetServices()[0].GetMethods()[0]).Diff(problems); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}