Repository: googleapis/api-linter
Pull request #340 (2019-11-12 23:36:15)

[AIP-127] Require google.api.http annotations.


Patch:
diff --git a/docs/rules/core/0122-camel-case-uris.md b/docs/rules/core/0122-camel-case-uris.md
index 2166c88..1c583cf 100644
--- a/docs/rules/core/0122-camel-case-uris.md
+++ b/docs/rules/core/0122-camel-case-uris.md
@@ -21,11 +21,11 @@ in the URI.
 
 ```proto
 // Incorrect.
-rpc GetElectronicBook {
+rpc GetElectronicBook(GetElectronicBookRequest) returns (ElectronicBook) {
   option (google.api.http) = {
     // Should be "electronicBooks", not "electronic_books".
     get: "/v1/{name=publishers/*/electronic_books/*}"
-  }
+  };
 }
 ```
 
@@ -33,10 +33,10 @@ rpc GetElectronicBook {
 
 ```proto
 // Correct.
-rpc GetElectronicBook {
+rpc GetElectronicBook(GetElectronicBookRequest) returns (ElectronicBook) {
   option (google.api.http) = {
     get: "/v1/{name=publishers/*/electronicBooks/*}"
-  }
+  };
 }
 ```
 
@@ -46,11 +46,11 @@ If you need to violate this rule, use a leading comment above the method.
 
 ```proto
 // (-- api-linter: core::0122::camel-case-uri=disabled --)
-rpc GetElectronicBook {
+rpc GetElectronicBook(GetElectronicBookRequest) returns (ElectronicBook) {
   option (google.api.http) = {
     // Should be "electronicBooks", not "electronic_books".
     get: "/v1/{name=publishers/*/electronic_books/*}"
-  }
+  };
 }
 ```
 
diff --git a/docs/rules/core/0127-http-annotation.md b/docs/rules/core/0127-http-annotation.md
new file mode 100644
index 0000000..bb65bb1
--- /dev/null
+++ b/docs/rules/core/0127-http-annotation.md
@@ -0,0 +1,76 @@
+---
+rule:
+  aip: 127
+  name: [core, '0127', http-annotation]
+  summary: HTTP annotations must be present on non-streaming methods.
+---
+
+# HTTP URI case
+
+This rule enforces that the HTTP annotation is present on all
+non-bidi-streaming methods and absent on streaming methods, as mandated in
+[AIP-127](http://aip.dev/127).
+
+## Details
+
+This rule scans all methods that a `google.api.http` annotation is present on
+all non-streaming methods, as well as methods that only use streaming in one
+direction. It complains if an annotation is not found.
+
+For bidi-streaming methods, it complains if a `google.api.http` annotation _is_
+found.
+
+## Examples
+
+### Unary methods
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc GetBook(GetBookRequest) returns (Book);  // Missing `google.api.http`.
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc GetBook(GetBookRequest) returns (Book) {
+  option (google.api.http) = {
+    get: "/v1/{name=publishers/*/books/*}"
+  };
+}
+```
+
+### Bidi-streaming methods
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc EditBook(stream EditBookRequest) returns (stream EditBookResponse) {
+  option (google.api.http) = {  // HTTP/1.1 not supported for bi-di streaming.
+    post: "/v1/{name=publishers/*/books/*}:edit"
+    body: "*"
+  };
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc EditBook(stream EditBookRequest) returns (stream EditBookResponse);
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the method.
+
+```proto
+// (-- api-linter: core::0127::http-annotation=disabled --)
+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.
diff --git a/rules/aip0127/aip0127.go b/rules/aip0127/aip0127.go
new file mode 100644
index 0000000..58c4e4d
--- /dev/null
+++ b/rules/aip0127/aip0127.go
@@ -0,0 +1,26 @@
+// 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 contains rules defined in https://aip.dev/127.
+package aip0127
+
+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,
+	)
+}
diff --git a/rules/aip0127/http_annotation.go b/rules/aip0127/http_annotation.go
new file mode 100644
index 0000000..da0ad4e
--- /dev/null
+++ b/rules/aip0127/http_annotation.go
@@ -0,0 +1,41 @@
+// 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/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var hasAnnotation = &lint.MethodRule{
+	Name: lint.NewRuleName(127, "http-annotation"),
+	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
+		hasHTTPRule := len(utils.GetHTTPRules(m)) > 0
+		if hasHTTPRule && m.IsClientStreaming() && m.IsServerStreaming() {
+			return []lint.Problem{{
+				Message:    "Bi-directional streaming RPCs should omit `google.api.http`.",
+				Descriptor: m,
+			}}
+		}
+		if !hasHTTPRule && !(m.IsClientStreaming() && m.IsServerStreaming()) {
+			return []lint.Problem{{
+				Message:    "RPCs must include HTTP definitions using the `google.api.http` annotation.",
+				Descriptor: m,
+			}}
+		}
+		return nil
+	},
+}
diff --git a/rules/rules.go b/rules/rules.go
index 0199018..2b9cbcf 100644
--- a/rules/rules.go
+++ b/rules/rules.go
@@ -53,6 +53,7 @@ import (
 	"github.com/googleapis/api-linter/lint"
 	"github.com/googleapis/api-linter/rules/aip0122"
 	"github.com/googleapis/api-linter/rules/aip0126"
+	"github.com/googleapis/api-linter/rules/aip0127"
 	"github.com/googleapis/api-linter/rules/aip0131"
 	"github.com/googleapis/api-linter/rules/aip0132"
 	"github.com/googleapis/api-linter/rules/aip0133"
@@ -77,6 +78,7 @@ type addRulesFuncType func(lint.RuleRegistry) error
 var aipAddRulesFuncs = []addRulesFuncType{
 	aip0122.AddRules,
 	aip0126.AddRules,
+	aip0127.AddRules,
 	aip0131.AddRules,
 	aip0132.AddRules,
 	aip0133.AddRules,


Test patch:
diff --git a/rules/aip0127/aip0127_test.go b/rules/aip0127/aip0127_test.go
new file mode 100644
index 0000000..a5251ee
--- /dev/null
+++ b/rules/aip0127/aip0127_test.go
@@ -0,0 +1,26 @@
+// 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 (
+	"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/aip0127/http_annotation_test.go b/rules/aip0127/http_annotation_test.go
new file mode 100644
index 0000000..f49f4e3
--- /dev/null
+++ b/rules/aip0127/http_annotation_test.go
@@ -0,0 +1,62 @@
+// 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 TestHasAnnotation(t *testing.T) {
+	ann := `option (google.api.http).get = "/v1/foo/";`
+	tests := []struct {
+		name       string
+		Istream    string
+		Ostream    string
+		annotation string
+		problems   testutils.Problems
+	}{
+		{"ValidUU", "", "", ann, testutils.Problems{}},
+		{"InvalidUU", "", "", "", testutils.Problems{{Message: "google.api.http"}}},
+		{"ValidUS", "", "stream ", ann, testutils.Problems{}},
+		{"InvalidUS", "", "stream ", "", testutils.Problems{{Message: "google.api.http"}}},
+		{"ValidSU", "stream ", "", ann, testutils.Problems{}},
+		{"InvalidSU", "stream ", "", "", testutils.Problems{{Message: "google.api.http"}}},
+		{"ValidSS", "stream ", "stream ", "", testutils.Problems{}},
+		{"InvalidSS", "stream ", "stream ", ann, testutils.Problems{{Message: "google.api.http"}}},
+	}
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			// Use of strings.ReplaceAll here allows a replacement using quotes,
+			// which Go templates has no way to get around.
+			f := testutils.ParseProto3Tmpl(t, strings.ReplaceAll(`
+				import "google/api/annotations.proto";
+				service Library {
+					rpc ReadBook({{.Istream}}ReadBookRequest) returns ({{.Ostream}}ReadBookResponse) {
+						{{.Annotation}}
+					}
+				}
+				message ReadBookRequest {}
+				message ReadBookResponse {}
+			`, "{{.Annotation}}", test.annotation), test)
+			m := f.GetServices()[0].GetMethods()[0]
+			if diff := test.problems.SetDescriptor(m).Diff(hasAnnotation.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}