Repository: googleapis/api-linter
Pull request #738 (2021-01-29 00:23:44)

feat: Require URIs to begin with a forward slash.

noahdietz: Per quality_checker, need to register the rule.
lukesneeringer: And that is why we have him. :-)

Patch:
diff --git a/docs/rules/0127/uri-leading-slash.md b/docs/rules/0127/uri-leading-slash.md
new file mode 100644
index 0000000..88b4330
--- /dev/null
+++ b/docs/rules/0127/uri-leading-slash.md
@@ -0,0 +1,50 @@
+---
+rule:
+  aip: 127
+  name: [core, '0127', uri-leading-slash]
+  summary: URIs should always begin with a leading slash.
+permalink: /127/uri-leading-slash
+redirect_from:
+  - /0127/uri-leading-slash
+---
+
+# URI Forward Slashes
+
+This rule enforces that URIs must begin with a forward slash, as mandated in
+[AIP-127][].
+
+## Details
+
+This rule scans all methods and complains if it finds a URI that does not start
+with `/`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc GetBook(GetBookRequest) returns (Book) {
+  option (google.api.http) = {
+    // Should be /v1/{name=publishers/*/books/*}
+    get: "v1/{name=publishers/*/books/*}"
+  };
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc GetBook(GetBookRequest) returns (Book) {
+  option (google.api.http) = {
+    get: "/v1/{name=publishers/*/books/*}"
+  };
+}
+```
+
+## Disabling
+
+Do not violate this rule. This would create an invalid URL.
+
+[aip-127]: https://aip.dev/127
diff --git a/rules/aip0127/aip0127.go b/rules/aip0127/aip0127.go
index 656fff6..8744808 100644
--- a/rules/aip0127/aip0127.go
+++ b/rules/aip0127/aip0127.go
@@ -24,6 +24,7 @@ func AddRules(r lint.RuleRegistry) error {
 	return r.Register(
 		127,
 		hasAnnotation,
+		leadingSlash,
 		resourceNameExtraction,
 	)
 }
diff --git a/rules/aip0127/uri_leading_slash.go b/rules/aip0127/uri_leading_slash.go
new file mode 100644
index 0000000..85c488f
--- /dev/null
+++ b/rules/aip0127/uri_leading_slash.go
@@ -0,0 +1,40 @@
+// Copyright 2021 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"
+
+	"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 leadingSlash = &lint.MethodRule{
+	Name: lint.NewRuleName(127, "uri-leading-slash"),
+	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
+		for _, http := range utils.GetHTTPRules(m) {
+			if !strings.HasPrefix(http.GetPlainURI(), "/") {
+				return []lint.Problem{{
+					Message:    "URIs must begin with a leading slash.",
+					Descriptor: m,
+					Location:   locations.MethodHTTPRule(m),
+				}}
+			}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0127/uri_leading_slash_test.go b/rules/aip0127/uri_leading_slash_test.go
new file mode 100644
index 0000000..42c9735
--- /dev/null
+++ b/rules/aip0127/uri_leading_slash_test.go
@@ -0,0 +1,50 @@
+// Copyright 2021 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 (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestURILeadingSlash(t *testing.T) {
+	for _, test := range []struct {
+		name     string
+		URI      string
+		problems testutils.Problems
+	}{
+		{"Valid", "/v1/{name=publishers/*/books/*}", nil},
+		{"Invalid", "v1/{name=publishers/*/books/*}", testutils.Problems{{Message: "leading slash"}}},
+	} {
+		f := testutils.ParseProto3Tmpl(t, `
+			import "google/api/annotations.proto";
+			import "google/protobuf/empty.proto";
+
+			service Foo {
+				rpc FrobFoo(google.protobuf.Empty) returns (google.protobuf.Empty) {
+					option (google.api.http) = {
+						post: "{{.URI}}"
+						body: "*"
+					};
+				}
+			}
+		`, test)
+		m := f.GetServices()[0].GetMethods()[0]
+		if diff := test.problems.SetDescriptor(m).Diff(leadingSlash.Lint(f)); diff != "" {
+			t.Errorf(diff)
+		}
+	}
+}