Repository: googleapis/api-linter
Pull request #695 (2020-10-21 14:58:39)

Add rules for AIP-144 HTTP method, HTTP body, request message name
Part of #692.


Patch:
diff --git a/docs/rules/0144/http-body.md b/docs/rules/0144/http-body.md
new file mode 100644
index 0000000..5ea4b56
--- /dev/null
+++ b/docs/rules/0144/http-body.md
@@ -0,0 +1,67 @@
+---
+rule:
+  aip: 144
+  name: [core, '0144', http-body]
+  summary: Add/Remove methods should use `*` as the HTTP body.
+permalink: /144/http-body
+redirect_from:
+  - /0144/http-body
+---
+
+# Add/Remove methods: HTTP body
+
+This rule enforces that all `Add` and `Remove` RPCs use `*` as the HTTP `body`, as
+mandated in [AIP-144][].
+
+## Details
+
+This rule looks at any RPC methods beginning with `Add` or `Remove`, and
+complains if the HTTP `body` field is anything other than `*`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc AddAuthor(AddAuthorRequest) returns (AddAuthorResponse) {
+  option (google.api.http) = {
+    post: "/v1/{book=publishers/*/books/*}:addAuthor"
+    body: ""  // The http body should be "*".
+  };
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc AddAuthor(AddAuthorRequest) returns (AddAuthorResponse) {
+  option (google.api.http) = {
+    post: "/v1/{book=publishers/*/books/*}:addAuthor"
+    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::0144::http-body=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc AddAuthor(AddAuthorRequest) returns (AddAuthorResponse) {
+  option (google.api.http) = {
+    post: "/v1/{book=publishers/*/books/*}:addAuthor"
+    body: ""
+  };
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-144]: https://aip.dev/144
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/docs/rules/0144/http-method.md b/docs/rules/0144/http-method.md
new file mode 100644
index 0000000..3fa97bd
--- /dev/null
+++ b/docs/rules/0144/http-method.md
@@ -0,0 +1,67 @@
+---
+rule:
+  aip: 144
+  name: [core, '0144', http-method]
+  summary: Add/Remove methods must use the POST HTTP verb.
+permalink: /144/http-method
+redirect_from:
+  - /0144/http-method
+---
+
+# Add/Remove methods: POST HTTP verb
+
+This rule enforces that all `Add` and `Remove` RPCs use the `POST` HTTP verb, as
+mandated in [AIP-144][].
+
+## Details
+
+This rule looks at any RPCs with the name beginning with `Add` or `Remove`, and
+complains if the HTTP verb is anything other than `POST`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc AddAuthor(AddAuthorRequest) returns (AddAuthorResponse) {
+  option (google.api.http) = {
+    patch: "/v1/{book=publishers/*/books/*}:addAuthor" // Should be `post:`.
+    body: "*"
+  };
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc AddAuthor(AddAuthorRequest) returns (AddAuthorResponse) {
+  option (google.api.http) = {
+    post: "/v1/{book=publishers/*/books/*}:addAuthor"
+    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::0144::http-method=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc AddAuthor(AddAuthorRequest) returns (AddAuthorResponse) {
+  option (google.api.http) = {
+    patch: "/v1/{book=publishers/*/books/*}:addAuthor" // Should be `post:`.
+    body: "*"
+  };
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-144]: https://aip.dev/144
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/docs/rules/0144/index.md b/docs/rules/0144/index.md
new file mode 100644
index 0000000..aba4e56
--- /dev/null
+++ b/docs/rules/0144/index.md
@@ -0,0 +1,11 @@
+---
+aip_listing: 144
+permalink: /144/
+redirect_from:
+  - /0144/
+prose_title: add/remove methods
+---
+
+# Add/Remove methods
+
+{% include linter-aip-listing.md aip=144 %}
diff --git a/docs/rules/0144/request-message-name.md b/docs/rules/0144/request-message-name.md
new file mode 100644
index 0000000..e53a454
--- /dev/null
+++ b/docs/rules/0144/request-message-name.md
@@ -0,0 +1,69 @@
+---
+rule:
+  aip: 144
+  name: [core, '0144', request-message-name]
+  summary: Add/Remove methods must have standardized request message names.
+permalink: /144/request-message-name
+redirect_from:
+  - /0144/request-message-name
+---
+
+# Add/Remove methods: Request message
+
+This rule enforces that all `Add` and `Remove` RPCs have a request message name
+of `Add*Request` or `Remove*Request`, as mandated in [AIP-144][].
+
+## Details
+
+This rule looks at any message beginning with `Add` or `Remove`, and complains
+if the name of the corresponding input message does not match the name of the
+RPC with the suffix `Request` appended.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+// Should be `AddAuthorRequest`.
+rpc AddAuthor(AppendAuthorRequest) returns (AddAuthorResponse) {
+  option (google.api.http) = {
+    post: "/v1/{book=publishers/*/books/*}:addAuthor"
+    body: "*"
+  };
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc AddAuthor(AddAuthorRequest) returns (AddAuthorResponse) {
+  option (google.api.http) = {
+    post: "/v1/{book=publishers/*/books/*}:addAuthor"
+    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::0144::request-message-name=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc AddAuthor(AppendAuthorRequest) returns (AddAuthorResponse) {
+  option (google.api.http) = {
+    post: "/v1/{book=publishers/*/books/*}:addAuthor"
+    body: "*"
+  };
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-144]: https://aip.dev/144
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0144/aip0144.go b/rules/aip0144/aip0144.go
new file mode 100644
index 0000000..e273918
--- /dev/null
+++ b/rules/aip0144/aip0144.go
@@ -0,0 +1,41 @@
+// 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 aip0144 contains rules defined in https://aip.dev/144.
+package aip0144
+
+import (
+	"regexp"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/jhump/protoreflect/desc"
+)
+
+// AddRules accepts a register function and registers each of
+// this AIP's rules to it.
+func AddRules(r lint.RuleRegistry) error {
+	return r.Register(
+		144,
+		httpBody,
+		httpMethod,
+		requestMessageName,
+	)
+}
+
+var addRemoveMethodRegexp = regexp.MustCompile("^(?:Add|Remove)(?:[A-Z]|$)")
+
+// Returns true if this is an AIP-144 Add/Remove method, false otherwise.
+func isAddRemoveMethod(m *desc.MethodDescriptor) bool {
+	return addRemoveMethodRegexp.MatchString(m.GetName())
+}
diff --git a/rules/aip0144/http_body.go b/rules/aip0144/http_body.go
new file mode 100644
index 0000000..14743f3
--- /dev/null
+++ b/rules/aip0144/http_body.go
@@ -0,0 +1,27 @@
+// 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 aip0144
+
+import (
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+)
+
+// Add/Remove methods should use "*" as the HTTP body.
+var httpBody = &lint.MethodRule{
+	Name:       lint.NewRuleName(144, "http-body"),
+	OnlyIf:     isAddRemoveMethod,
+	LintMethod: utils.LintWildcardHTTPBody,
+}
diff --git a/rules/aip0144/http_method.go b/rules/aip0144/http_method.go
new file mode 100644
index 0000000..21accd6
--- /dev/null
+++ b/rules/aip0144/http_method.go
@@ -0,0 +1,27 @@
+// 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 aip0144
+
+import (
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+)
+
+// Add/Remove methods should use the HTTP POST method.
+var httpMethod = &lint.MethodRule{
+	Name:       lint.NewRuleName(144, "http-method"),
+	OnlyIf:     isAddRemoveMethod,
+	LintMethod: utils.LintHTTPMethod("POST"),
+}
diff --git a/rules/aip0144/request_message_name.go b/rules/aip0144/request_message_name.go
new file mode 100644
index 0000000..696594f
--- /dev/null
+++ b/rules/aip0144/request_message_name.go
@@ -0,0 +1,46 @@
+// 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 aip0144
+
+import (
+	"fmt"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/locations"
+	"github.com/jhump/protoreflect/desc"
+)
+
+// Add/Remove methods should have a properly named request message.
+var requestMessageName = &lint.MethodRule{
+	Name:   lint.NewRuleName(144, "request-message-name"),
+	OnlyIf: isAddRemoveMethod,
+	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
+		// Rule check: Establish that for methods such as `AddAuthor`, the request
+		// message is named `AddAuthorRequest`.
+		if got, want := m.GetInputType().GetName(), m.GetName()+"Request"; got != want {
+			return []lint.Problem{{
+				Message: fmt.Sprintf(
+					"Add/Remove RPCs should have a request message named after the RPC, such as %q.",
+					want,
+				),
+				Suggestion: want,
+				Descriptor: m,
+				Location:   locations.MethodRequestType(m),
+			}}
+		}
+
+		return nil
+	},
+}
diff --git a/rules/rules.go b/rules/rules.go
index 453e6f9..9af2ad8 100644
--- a/rules/rules.go
+++ b/rules/rules.go
@@ -67,6 +67,7 @@ import (
 	"github.com/googleapis/api-linter/rules/aip0141"
 	"github.com/googleapis/api-linter/rules/aip0142"
 	"github.com/googleapis/api-linter/rules/aip0143"
+	"github.com/googleapis/api-linter/rules/aip0144"
 	"github.com/googleapis/api-linter/rules/aip0146"
 	"github.com/googleapis/api-linter/rules/aip0148"
 	"github.com/googleapis/api-linter/rules/aip0151"
@@ -108,6 +109,7 @@ var aipAddRulesFuncs = []addRulesFuncType{
 	aip0141.AddRules,
 	aip0142.AddRules,
 	aip0143.AddRules,
+	aip0144.AddRules,
 	aip0146.AddRules,
 	aip0148.AddRules,
 	aip0151.AddRules,


Test patch:
diff --git a/rules/aip0144/aip0144_test.go b/rules/aip0144/aip0144_test.go
new file mode 100644
index 0000000..c290ea1
--- /dev/null
+++ b/rules/aip0144/aip0144_test.go
@@ -0,0 +1,13 @@
+package aip0144
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/lint"
+)
+
+func TestAddRules(t *testing.T) {
+	if err := AddRules(lint.NewRuleRegistry()); err != nil {
+		t.Errorf("AddRules got an error: %v", err)
+	}
+}
diff --git a/rules/aip0144/http_body_test.go b/rules/aip0144/http_body_test.go
new file mode 100644
index 0000000..12ed776
--- /dev/null
+++ b/rules/aip0144/http_body_test.go
@@ -0,0 +1,59 @@
+// 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 aip0144
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestHTTPBody(t *testing.T) {
+	tests := []struct {
+		testName   string
+		Body       string
+		MethodName string
+		problems   testutils.Problems
+	}{
+		{"ValidAdd", "*", "AddAuthor", nil},
+		{"InvalidAdd", "", "AddAuthor", testutils.Problems{{Message: "HTTP body"}}},
+		{"ValidRemove", "*", "RemoveAuthor", nil},
+		{"InvalidRemove", "", "RemoveAuthor", testutils.Problems{{Message: "HTTP body"}}},
+		{"Irrelevant", "*", "AcquireBook", nil},
+	}
+
+	for _, test := range tests {
+		t.Run(test.testName, func(t *testing.T) {
+			file := testutils.ParseProto3Tmpl(t, `
+				import "google/api/annotations.proto";
+				service Library {
+					rpc {{.MethodName}}({{.MethodName}}Request) returns ({{.MethodName}}Response) {
+						option (google.api.http) = {
+							post: "/v1/{book=publishers/*/books/*}"
+							body: "{{.Body}}"
+						};
+					}
+				}
+				message {{.MethodName}}Request {}
+				message {{.MethodName}}Response {}
+			`, test)
+			method := file.GetServices()[0].GetMethods()[0]
+			problems := httpBody.Lint(file)
+			if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
+				t.Error(diff)
+			}
+		})
+	}
+}
diff --git a/rules/aip0144/http_method_test.go b/rules/aip0144/http_method_test.go
new file mode 100644
index 0000000..f26f909
--- /dev/null
+++ b/rules/aip0144/http_method_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 aip0144
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestHTTPMethod(t *testing.T) {
+	// Set up testing permutations.
+	tests := []struct {
+		testName   string
+		Method     string
+		MethodName string
+		problems   testutils.Problems
+	}{
+		{"ValidAdd", "post", "AddAuthor", nil},
+		{"InvalidAdd", "patch", "AddAuthor", testutils.Problems{{Message: "HTTP POST"}}},
+		{"ValidRemove", "post", "RemoveAuthor", nil},
+		{"InvalidRemove", "patch", "RemoveAuthor", testutils.Problems{{Message: "HTTP POST"}}},
+		{"Irrelevant", "get", "AcquireBook", nil},
+	}
+
+	// Run each test.
+	for _, test := range tests {
+		t.Run(test.testName, func(t *testing.T) {
+			file := testutils.ParseProto3Tmpl(t, `
+				import "google/api/annotations.proto";
+				service Library {
+					rpc {{.MethodName}}({{.MethodName}}Request) returns ({{.MethodName}}Response) {
+						option (google.api.http) = {
+							{{.Method}}: "/v1/{book=publishers/*/books/*}"
+						};
+					}
+				}
+				message {{.MethodName}}Request {}
+				message {{.MethodName}}Response {}
+			`, test)
+			method := file.GetServices()[0].GetMethods()[0]
+			problems := httpMethod.Lint(file)
+			if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
+				t.Error(diff)
+			}
+		})
+	}
+}
diff --git a/rules/aip0144/request_message_name_test.go b/rules/aip0144/request_message_name_test.go
new file mode 100644
index 0000000..95fa0f3
--- /dev/null
+++ b/rules/aip0144/request_message_name_test.go
@@ -0,0 +1,54 @@
+// 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 aip0144
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestRequestMessageName(t *testing.T) {
+	// Set up the testing permutations.
+	tests := []struct {
+		testName       string
+		MethodName     string
+		ReqMessageName string
+		problems       testutils.Problems
+	}{
+		{"ValidAdd", "AddAuthor", "AddAuthorRequest", nil},
+		{"InvalidAdd", "AddAuthor", "AppendAuthorRequest", testutils.Problems{{Suggestion: "AddAuthorRequest"}}},
+		{"ValidRemove", "RemoveAuthor", "RemoveAuthorRequest", nil},
+		{"InvalidRemove", "RemoveAuthor", "DeleteAuthorRequest", testutils.Problems{{Suggestion: "RemoveAuthorRequest"}}},
+		{"Irrelevant", "AcquireBook", "GetBookRequest", nil},
+	}
+
+	// Run each test individually.
+	for _, test := range tests {
+		t.Run(test.testName, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				service Library {
+					rpc {{.MethodName}}({{.ReqMessageName}}) returns (Book) {}
+				}
+				message {{.ReqMessageName}} {}
+				message Book {}
+			`, test)
+			m := f.GetServices()[0].GetMethods()[0]
+			if diff := test.problems.SetDescriptor(m).Diff(requestMessageName.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}