Repository: googleapis/api-linter
Pull request #872 (2021-08-19 14:44:45)

feat: Add rule to check type of AIP-158 paginated request skip field
Corresponds to #845 and aip-dev/google.aip.dev#754.


Patch:
diff --git a/docs/rules/0158/request-skip-field.md b/docs/rules/0158/request-skip-field.md
new file mode 100644
index 0000000..3782e56
--- /dev/null
+++ b/docs/rules/0158/request-skip-field.md
@@ -0,0 +1,83 @@
+---
+rule:
+  aip: 158
+  name: [core, '0158', request-skip-field]
+  summary: Paginated RPC `skip` fields must have type `int32`.
+permalink: /158/request-skip-field
+redirect_from:
+  - /0158/request-skip-field
+---
+
+# Paginated methods: skip field
+
+This rule enforces that all `List` and `Search` request `skip` fields have type `int32`, as
+mandated in [AIP-158][].
+
+## Details
+
+This rule looks at any message matching `List*Request` or `Search*Request` that
+contains a `skip` field, and complains if the field is not a singular `int32`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+message ListBooksRequest {
+  string parent = 1 [
+    (google.api.resource_reference).child_type = "library.googleapis.com/Book",
+    (google.api.field_behavior) = REQUIRED
+  ];
+
+  int32 page_size = 2;
+
+  string page_token = 3;
+
+  string skip = 4;  // Field type should be `int32`.
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+message ListBooksRequest {
+  string parent = 1 [
+    (google.api.resource_reference).child_type = "library.googleapis.com/Book",
+    (google.api.field_behavior) = REQUIRED
+  ];
+
+  int32 page_size = 2;
+
+  string page_token = 3;
+
+  int32 skip = 4;
+}
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the field.
+Remember to also include an [aip.dev/not-precedent][] comment explaining why.
+
+```proto
+message ListBooksRequest {
+  string parent = 1 [
+    (google.api.resource_reference).child_type = "library.googleapis.com/Book",
+    (google.api.field_behavior) = REQUIRED
+  ];
+
+  int32 page_size = 2;
+
+  string page_token = 3;
+
+  // (-- api-linter: core::0158::request-skip-field=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  string skip = 4;
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-158]: https://aip.dev/158
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0158/aip0158.go b/rules/aip0158/aip0158.go
index fef53c0..480af1e 100644
--- a/rules/aip0158/aip0158.go
+++ b/rules/aip0158/aip0158.go
@@ -29,6 +29,7 @@ func AddRules(r lint.RuleRegistry) error {
 		158,
 		requestPaginationPageSize,
 		requestPaginationPageToken,
+		requestSkipField,
 		responsePaginationNextPageToken,
 		responseRepeatedFirstField,
 		responsePluralFirstField,
diff --git a/rules/aip0158/request_skip_field.go b/rules/aip0158/request_skip_field.go
new file mode 100644
index 0000000..873f1ba
--- /dev/null
+++ b/rules/aip0158/request_skip_field.go
@@ -0,0 +1,42 @@
+// 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 aip0158
+
+import (
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/locations"
+	"github.com/jhump/protoreflect/desc"
+	"github.com/jhump/protoreflect/desc/builder"
+)
+
+var requestSkipField = &lint.FieldRule{
+	Name: lint.NewRuleName(158, "request-skip-field"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isPaginatedRequestMessage(f.GetOwner()) && f.GetName() == "skip"
+	},
+	LintField: func(f *desc.FieldDescriptor) (problems []lint.Problem) {
+		// Rule check: Ensure that the name page_size is the correct type.
+		if f.GetType() != builder.FieldTypeInt32().GetType() || f.IsRepeated() {
+			return []lint.Problem{{
+				Message:    "`skip` field on List RPCs should be a singular int32",
+				Suggestion: "int32",
+				Descriptor: f,
+				Location:   locations.FieldType(f),
+			}}
+		}
+
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0158/request_skip_field_test.go b/rules/aip0158/request_skip_field_test.go
new file mode 100644
index 0000000..a6898a5
--- /dev/null
+++ b/rules/aip0158/request_skip_field_test.go
@@ -0,0 +1,56 @@
+// 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 aip0158
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+func TestRequestSkipField(t *testing.T) {
+	tests := []struct {
+		name        string
+		MessageName string
+		Fields      string
+		problems    testutils.Problems
+	}{
+		{"ValidPresent", "FooRequest", `int32 page_size = 1; int32 skip = 2;`, nil},
+		{"ValidAbsent", "FooRequest", `int32 page_size = 1;`, nil},
+		{"InvalidType", "FooRequest", `int32 page_size = 1; string skip = 2;`, testutils.Problems{{Suggestion: "int32"}}},
+		{"InvalidRepeated", "FooRequest", `int32 page_size = 1; repeated int32 skip = 2;`, testutils.Problems{{Suggestion: "int32"}}},
+		{"IrrelevantMessageName", "Foo", `int32 page_size = 1; string skip = 2;`, nil},
+		{"IrrelevantMessageFields", "FooRequest", `string skip = 1;`, nil},
+	}
+
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				message {{.MessageName}} {
+					{{.Fields}}
+				}
+			`, test)
+			var d desc.Descriptor = nil
+			if len(test.problems) > 0 {
+				d = f.GetMessageTypes()[0].GetFields()[1]
+			}
+			problems := requestSkipField.Lint(f)
+			if diff := test.problems.SetDescriptor(d).Diff(problems); diff != "" {
+				t.Error(diff)
+			}
+		})
+	}
+}