Repository: googleapis/api-linter
Pull request #361 (2019-11-19 21:26:27)

[AIP-127] Resource name extraction rule.


Patch:
diff --git a/docs/rules/0127/http-annotation.md b/docs/rules/0127/http-annotation.md
index 851dfe9..e7ea89c 100644
--- a/docs/rules/0127/http-annotation.md
+++ b/docs/rules/0127/http-annotation.md
@@ -69,11 +69,16 @@ rpc EditBook(stream EditBookRequest) returns (stream EditBookResponse);
 ## 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::0127::http-annotation=disabled --)
+// (-- api-linter: core::0127::http-annotation=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
 rpc GetBook(GetBookRequest) returns (Book);
 ```
 
 If you need to violate this rule for an entire file, place the comment at the
 top of the file.
+
+[aip-127]: https://aip.dev/127
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/docs/rules/0127/resource-name-extraction.md b/docs/rules/0127/resource-name-extraction.md
new file mode 100644
index 0000000..07c63d7
--- /dev/null
+++ b/docs/rules/0127/resource-name-extraction.md
@@ -0,0 +1,69 @@
+---
+rule:
+  aip: 127
+  name: [core, '0127', resource-name-extraction]
+  summary: HTTP annotations should extract full resource names into variables.
+permalink: /127/resource-name-extraction
+redirect_from:
+  - /0127/resource-name-extraction
+---
+
+# HTTP URI case
+
+This rule enforces that HTTP annotations pull whole resource names into
+variables, and not just the ID components, as mandated in [AIP-127][].
+
+## Details
+
+This rule scans all methods and complains if it finds a URI with a variable
+whose value is `*`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc GetBook(GetBookRequest) returns (Book) {
+  // Should be /v1/{name=publishers/*/books/*}
+  get: "/v1/publishers/{publisher_id}/books/{book_id}"
+}
+```
+
+```proto
+// Incorrect.
+rpc GetBook(GetBookRequest) returns (Book) {
+  // Should be /v1/{name=publishers/*/books/*}
+  get: "/v1/publishers/{publisher_id=*}/books/{book_id=*}"
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc GetBook(GetBookRequest) returns (Book) {
+  option (google.api.http) = {
+    get: "/v1/{name=publishers/*/books/*}"
+  };
+}
+```
+
+## 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::0127::resource-name-extraction=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc GetBook(GetBookRequest) returns (Book) {
+  get: "/v1/publishers/{publisher_id}/books/{book_id}"
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-127]: https://aip.dev/127
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0127/aip0127.go b/rules/aip0127/aip0127.go
index 58c4e4d..656fff6 100644
--- a/rules/aip0127/aip0127.go
+++ b/rules/aip0127/aip0127.go
@@ -15,12 +15,15 @@
 // Package aip0127 contains rules defined in https://aip.dev/127.
 package aip0127
 
-import "github.com/googleapis/api-linter/lint"
+import (
+	"github.com/googleapis/api-linter/lint"
+)
 
 // AddRules adds all of the AIP-127 rules to the provided registry.
 func AddRules(r lint.RuleRegistry) error {
 	return r.Register(
 		127,
 		hasAnnotation,
+		resourceNameExtraction,
 	)
 }
diff --git a/rules/aip0127/resource_name_extraction.go b/rules/aip0127/resource_name_extraction.go
new file mode 100644
index 0000000..a6e8202
--- /dev/null
+++ b/rules/aip0127/resource_name_extraction.go
@@ -0,0 +1,40 @@
+// 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 aip0127
+
+import (
+	"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 resourceNameExtraction = &lint.MethodRule{
+	Name: lint.NewRuleName(127, "resource-name-extraction"),
+	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
+		for _, rule := range utils.GetHTTPRules(m) {
+			for _, v := range rule.GetVariables() {
+				if v == "*" {
+					return []lint.Problem{{
+						Message:    "Extract a full resource name into a variable, not just IDs.",
+						Descriptor: m,
+						Location:   locations.MethodHTTPRule(m),
+					}}
+				}
+			}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0127/resource_name_extraction_test.go b/rules/aip0127/resource_name_extraction_test.go
new file mode 100644
index 0000000..8b14c04
--- /dev/null
+++ b/rules/aip0127/resource_name_extraction_test.go
@@ -0,0 +1,53 @@
+// 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 aip0127
+
+import (
+	"strings"
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestResourceNameExtraction(t *testing.T) {
+	for _, test := range []struct {
+		name     string
+		uri      string
+		problems testutils.Problems
+	}{
+		{"Valid", "/v1/{name=publishers/*/books/*}", testutils.Problems{}},
+		{"Invalid", "/v1/publishers/{publisher_id}/books/{book_id}", testutils.Problems{{Message: "full resource name"}}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3String(t, strings.ReplaceAll(`
+				import "google/api/annotations.proto";
+				service Library {
+					rpc GetBook(GetBookRequest) returns (Book) {
+						option (google.api.http) = {
+							get: "{{.URI}}"
+						};
+					}
+				}
+				message GetBookRequest {}
+				message Book {}
+			`, "{{.URI}}", test.uri))
+			method := f.GetServices()[0].GetMethods()[0]
+			problems := resourceNameExtraction.Lint(f)
+			if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}