Repository: googleapis/api-linter
Pull request #380 (2019-11-22 22:35:36)

[AIP-132] Check unknown fields on list responses.


Patch:
diff --git a/docs/rules/0132/request-unknown-fields.md b/docs/rules/0132/request-unknown-fields.md
index f5b7cfa..b3653f5 100644
--- a/docs/rules/0132/request-unknown-fields.md
+++ b/docs/rules/0132/request-unknown-fields.md
@@ -8,7 +8,7 @@ redirect_from:
   - /0132/request-unknown-fields
 ---
 
-# Get methods: Unknown fields
+# List methods: Unknown fields (Request)
 
 This rule enforces that all `List` standard methods do not have unexpected
 fields, as mandated in [AIP-132][].
diff --git a/docs/rules/0132/response-unknown-fields.md b/docs/rules/0132/response-unknown-fields.md
new file mode 100644
index 0000000..b3b5cf9
--- /dev/null
+++ b/docs/rules/0132/response-unknown-fields.md
@@ -0,0 +1,74 @@
+---
+rule:
+  aip: 132
+  name: [core, '0132', request-unknown-fields]
+  summary: List RPCs should not have unexpected fields in the request.
+permalink: /132/request-unknown-fields
+redirect_from:
+  - /0132/request-unknown-fields
+---
+
+# List methods: Unknown fields (Response)
+
+This rule enforces that all `List` standard methods do not have unexpected
+fields, as mandated in [AIP-132][].
+
+## Details
+
+This rule looks at any message matching `List*Response` and complains if it
+comes across any fields other than:
+
+- The resource.
+- `int32/int64 total_size` ([AIP-132][])
+- `string next_page_token` ([AIP-158][])
+- `repeated string unavailable` ([AIP-217][])
+
+It only checks field names; it does not validate type correctness or
+repeated-ness.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message ListBooksResponse {
+  repeated Book books = 1;
+  string next_page_token = 2;
+  string publisher_id = 3;  // Unrecognized field.
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message ListBooksResponse {
+  repeated Book books = 1;
+  string next_page_token = 2;
+}
+```
+
+## 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 ListBooksResponse {
+  repeated Book books = 1;
+  string next_page_token = 2;
+  // (-- api-linter: core::0132::response-unknown-fields=disabled
+  //     aip.dev/not-precedent: We really need this field because reasons. --)
+  string publisher_id = 3;
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-132]: https://aip.dev/132
+[aip-135]: https://aip.dev/135
+[aip-157]: https://aip.dev/157
+[aip-158]: https://aip.dev/158
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0132/aip0132.go b/rules/aip0132/aip0132.go
index fe5eefc..49cef17 100644
--- a/rules/aip0132/aip0132.go
+++ b/rules/aip0132/aip0132.go
@@ -29,6 +29,7 @@ func AddRules(r lint.RuleRegistry) error {
 		requestFieldTypes,
 		requestMessageName,
 		responseMessageName,
+		responseUnknownFields,
 		standardFields,
 		unknownFields,
 	)
@@ -36,6 +37,7 @@ func AddRules(r lint.RuleRegistry) error {
 
 var listMethodRegexp = regexp.MustCompile("^List(?:[A-Z]|$)")
 var listReqMessageRegexp = regexp.MustCompile("^List[A-Za-z0-9]*Request$")
+var listRespMessageRegexp = regexp.MustCompile("^List([A-Za-z0-9]*)Response$")
 
 // Return true if this is an AIP-132 List method, false otherwise.
 func isListMethod(m *desc.MethodDescriptor) bool {
@@ -46,3 +48,8 @@ func isListMethod(m *desc.MethodDescriptor) bool {
 func isListRequestMessage(m *desc.MessageDescriptor) bool {
 	return listReqMessageRegexp.MatchString(m.GetName())
 }
+
+// Return true if this is an AIP-132 List response message, false otherwise.
+func isListResponseMessage(m *desc.MessageDescriptor) bool {
+	return listRespMessageRegexp.MatchString(m.GetName())
+}
diff --git a/rules/aip0132/response_unknown_fields.go b/rules/aip0132/response_unknown_fields.go
new file mode 100644
index 0000000..51fd1ba
--- /dev/null
+++ b/rules/aip0132/response_unknown_fields.go
@@ -0,0 +1,54 @@
+// 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 aip0132
+
+import (
+	"bitbucket.org/creachadair/stringset"
+	"github.com/googleapis/api-linter/lint"
+	"github.com/jhump/protoreflect/desc"
+	"github.com/stoewer/go-strcase"
+)
+
+// The resource itself is not included here, but also permitted.
+// This is covered in code in the rule itself.
+var respAllowedFields = stringset.New(
+	"next_page_token",       // AIP-158
+	"total_size",            // AIP-132
+	"unreachable",           // AIP-217
+	"unreachable_locations", // Wrong, but a separate AIP-217 rule catches it.
+)
+
+var responseUnknownFields = &lint.FieldRule{
+	Name: lint.NewRuleName(132, "response-unknown-fields"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isListResponseMessage(f.GetOwner())
+	},
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		// A repeated variant of the resource should be permitted.
+		resource := strcase.SnakeCase(listRespMessageRegexp.FindStringSubmatch(f.GetOwner().GetName())[1])
+		if f.GetName() == resource {
+			return nil
+		}
+
+		// It is not the resource field; check it against the whitelist.
+		if !respAllowedFields.Contains(f.GetName()) {
+			return []lint.Problem{{
+				Message:    "List responses should only contain fields explicitly described in AIPs.",
+				Descriptor: f,
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0132/response_unknown_fields_test.go b/rules/aip0132/response_unknown_fields_test.go
new file mode 100644
index 0000000..6c00da9
--- /dev/null
+++ b/rules/aip0132/response_unknown_fields_test.go
@@ -0,0 +1,49 @@
+// 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 aip0132
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+	"github.com/stoewer/go-strcase"
+)
+
+func TestResponseUnknownFields(t *testing.T) {
+	for _, test := range []struct {
+		FieldName string
+		problems  testutils.Problems
+	}{
+		{"total_size", testutils.Problems{}},
+		{"unreachable", testutils.Problems{}},
+		{"unreachable_locations", testutils.Problems{}},
+		{"extra", testutils.Problems{{Message: "List responses"}}},
+	} {
+		t.Run(strcase.UpperCamelCase(test.FieldName), func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				message ListBooksResponse {
+					repeated Book books = 1;
+					string next_page_token = 2;
+					string {{.FieldName}} = 3;
+				}
+				message Book {}
+			`, test)
+			field := f.GetMessageTypes()[0].GetFields()[2]
+			if diff := test.problems.SetDescriptor(field).Diff(responseUnknownFields.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}