Repository: googleapis/api-linter
Pull request #254 (2019-10-23 19:17:49)

[AIP-136] Check the HTTP `body` clause.


Patch:
diff --git a/rules/aip0136/aip0136.go b/rules/aip0136/aip0136.go
index 28003ce..a872661 100644
--- a/rules/aip0136/aip0136.go
+++ b/rules/aip0136/aip0136.go
@@ -26,6 +26,7 @@ import (
 // this AIP's rules to it.
 func AddRules(r lint.RuleRegistry) {
 	r.Register(
+		httpBody,
 		httpMethod,
 		noPrepositions,
 		verbNoun,
diff --git a/rules/aip0136/http_body.go b/rules/aip0136/http_body.go
new file mode 100644
index 0000000..1b38167
--- /dev/null
+++ b/rules/aip0136/http_body.go
@@ -0,0 +1,51 @@
+// 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 aip0136
+
+import (
+	"bitbucket.org/creachadair/stringset"
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var httpBody = &lint.MethodRule{
+	Name:   lint.NewRuleName("core", "0136", "http-body"),
+	URI:    "https://aip.dev/136",
+	OnlyIf: isCustomMethod,
+	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
+		for _, httpRule := range utils.GetHTTPRules(m) {
+			noBody := stringset.New("GET", "DELETE")
+			if !noBody.Contains(httpRule.Method) && httpRule.Body != "*" {
+				// FIXME: We intentionally only return one Problem here.
+				// When we can attach problems to the particular annotation, update this
+				// to return multiples.
+				return []lint.Problem{{
+					Message:    "Custom POST methods should set `body: \"*\"`.",
+					Descriptor: m,
+				}}
+			} else if noBody.Contains(httpRule.Method) && httpRule.Body != "" {
+				// FIXME: We intentionally only return one Problem here.
+				// When we can attach problems to the particular annotation, update this
+				// to return multiples.
+				return []lint.Problem{{
+					Message:    "Custom GET (or DELETE) methods should not set a body clause.",
+					Descriptor: m,
+				}}
+			}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0136/http_body_test.go b/rules/aip0136/http_body_test.go
new file mode 100644
index 0000000..0253ce5
--- /dev/null
+++ b/rules/aip0136/http_body_test.go
@@ -0,0 +1,61 @@
+// 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 aip0136
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestHttpBody(t *testing.T) {
+	tests := []struct {
+		testName   string
+		MethodName string
+		Method     string
+		Body       string
+		problems   testutils.Problems
+	}{
+		{"ValidPostBody", "ArchiveBook", "post", "*", testutils.Problems{}},
+		{"InvalidPostBody", "ArchiveBook", "post", "book", testutils.Problems{{Message: `body: "*"`}}},
+		{"InvalidPostNoBody", "ArchiveBook", "post", "", testutils.Problems{{Message: `body: "*"`}}},
+		{"ValidGetNoBody", "ReadBook", "get", "", testutils.Problems{}},
+		{"InvalidGetBody", "ReadBook", "get", "*", testutils.Problems{{Message: "should not set"}}},
+		{"InvalidGetOtherBody", "ReadBook", "get", "Book", testutils.Problems{{Message: "should not set"}}},
+		{"ValidStdMethod", "CreateBook", "post", "book", testutils.Problems{}},
+	}
+	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:frob"
+							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.Errorf(diff)
+			}
+		})
+	}
+}