Repository: googleapis/api-linter
Pull request #253 (2019-10-23 18:52:31)

[AIP-136] Add URI suffix check for custom methods.


Patch:
diff --git a/rules/aip0136/aip0136.go b/rules/aip0136/aip0136.go
index a872661..45bbbe8 100644
--- a/rules/aip0136/aip0136.go
+++ b/rules/aip0136/aip0136.go
@@ -29,6 +29,7 @@ func AddRules(r lint.RuleRegistry) {
 		httpBody,
 		httpMethod,
 		noPrepositions,
+		uriSuffix,
 		verbNoun,
 	)
 }
diff --git a/rules/aip0136/uri_suffix.go b/rules/aip0136/uri_suffix.go
new file mode 100644
index 0000000..c1fe63f
--- /dev/null
+++ b/rules/aip0136/uri_suffix.go
@@ -0,0 +1,68 @@
+// 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 (
+	"fmt"
+	"strings"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+	"github.com/stoewer/go-strcase"
+)
+
+var uriSuffix = &lint.MethodRule{
+	Name:   lint.NewRuleName("core", "0136", "uri-suffix"),
+	URI:    "https://aip.dev/136#guidance",
+	OnlyIf: isCustomMethod,
+	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
+		for _, httpRule := range utils.GetHTTPRules(m) {
+			// URIs should end in a `:` character followed by the name of the method.
+			// However, if the noun is the URI's resource, then we only use `:verb`,
+			// not `:verbNoun`.
+			//
+			// This is somewhat tricky to test for perfectly, and may need to evolve
+			// over time, but the following rules should be mostly correct:
+			//   1. If the URI contains `{name=`, expect `:verb`.
+			//   2. Otherwise, expect `:verbNoun`.
+			//
+			// We blindly assume that the verb is always one word (the "noun" may be
+			// any number of words; they often include adjectives).
+			//
+			// N.B. The LowerCamel(Snake(name)) is because strcase does not translate
+			//      from upper camel to lower camel correctly.
+			want := ":" + strcase.LowerCamelCase(strcase.SnakeCase(m.GetName()))
+			if strings.Contains(httpRule.URI, "{name=") {
+				want = ":" + strings.Split(strcase.SnakeCase(m.GetName()), "_")[0]
+			}
+
+			// Do we have the suffix we expect?
+			if !strings.HasSuffix(httpRule.URI, want) {
+				// FIXME: We intentionally only return one Problem here.
+				// When we can attach issues to the particular annotation, update this
+				// to return multiples.
+				return []lint.Problem{{
+					Message: fmt.Sprintf(
+						"Custom method should have a URI suffix matching the method name, such as %q.",
+						want,
+					),
+					Descriptor: m,
+				}}
+			}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0136/uri_suffix_test.go b/rules/aip0136/uri_suffix_test.go
new file mode 100644
index 0000000..ec7d78e
--- /dev/null
+++ b/rules/aip0136/uri_suffix_test.go
@@ -0,0 +1,60 @@
+// 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 TestURISuffix(t *testing.T) {
+	tests := []struct {
+		testName   string
+		MethodName string
+		URI        string
+		problems   testutils.Problems
+	}{
+		{"ValidVerb", "ArchiveBook", "/v1/{name=publishers/*/books}:archive", testutils.Problems{}},
+		{"InvalidVerb", "ArchiveBook", "/v1/{name=publishers/*/books}:archiveBook", testutils.Problems{{Message: ":archive"}}},
+		{"ValidVerbNounNoVars", "TranslateText", "/v3:translateText", testutils.Problems{}},
+		{"ValidVerbNounNoName", "TranslateText", "/v3/{location=projects/*/locations/*}:translateText", testutils.Problems{}},
+		{"InvalidVerbNoun", "TranslateText", "/v3:translate", testutils.Problems{{Message: ":translateText"}}},
+		{"ValidOneWord", "Translate", "/v3:translate", testutils.Problems{}},
+		{"ValidStdMethod", "GetBook", "/v1/{name=publishers/*/books/*}", 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) = {
+							post: "{{.URI}}"
+							body: "*"
+						};
+					}
+				}
+				message {{.MethodName}}Request {}
+				message {{.MethodName}}Response {}
+			`, test)
+			method := file.GetServices()[0].GetMethods()[0]
+			problems := uriSuffix.Lint(file)
+			if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}