Repository: googleapis/api-linter
Pull request #1142 (2023-05-06 18:48:07)

feat(AIP-133): enforce required request fields
Enforces the following from AIP-133:

    The request message must not contain any other required fields ...
    except those described in this or another AIP.

shwoodard: Do we have a complementary Rule that checks that fields that should be required have `REQUIRED` field_behavior on them?
toumorokoshi: > Do we have a complementary Rule that checks that fields that should be required have REQUIRED field_behavior on them?

Yep we're covered there:

https://linter.aip.dev/133/request-resource-behavior
https://linter.aip.dev/133/request-parent-behavior

shwoodard: > > Do we have a complementary Rule that checks that fields that should be required have REQUIRED field_behavior on them?
> 
> Yep we're covered there:
> 
> https://linter.aip.dev/133/request-resource-behavior https://linter.aip.dev/133/request-parent-behavior

Great!

Patch:
diff --git a/docs/rules/0133/request-required-fields.md b/docs/rules/0133/request-required-fields.md
new file mode 100644
index 0000000..894bd99
--- /dev/null
+++ b/docs/rules/0133/request-required-fields.md
@@ -0,0 +1,75 @@
+---
+rule:
+  aip: 133
+  name: [core, '0133', request-unknown-fields]
+  summary: Create RPCs must not have unexpected required fields in the request.
+permalink: /133/request-required-fields
+redirect_from:
+  - /0133/request-required-fields
+---
+
+# Create methods: Required fields
+
+This rule enforces that all `Create` standard methods do not have unexpected
+required fields, as mandated in [AIP-133][].
+
+## Details
+
+This rule looks at any message matching `Create*Request` and complains if it
+comes across any required fields other than:
+
+- `string parent` ([AIP-133][])
+- `{Resource} {resource}` ([AIP-133][])
+- `string {resource}_id` ([AIP-133][])
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message CreateBookRequest {
+  string parent = 1;
+  Book book = 2;
+  string book_id = 3;
+  // Non-standard required field.
+  string validate_only = 4 [(google.api.field_behavior) = REQUIRED];
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message CreateBookRequest {
+  string parent = 1;
+  Book book = 2;
+  string book_id = 3;
+  string validate_only = 4 [(google.api.field_behavior) = OPTIONAL];
+}
+```
+
+## 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 CreateBookRequest {
+  string parent = 1;
+  Book book = 2;
+  string book_id = 3;
+
+  // (-- api-linter: core::0133::request-required-fields=disabled
+  //     aip.dev/not-precedent: We really need this field to be required because
+  // reasons. --)
+  string validate_only = 4 [(google.api.field_behavior) = REQUIRED];
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-133]: https://aip.dev/133
+[aip-155]: https://aip.dev/155
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0133/aip0133.go b/rules/aip0133/aip0133.go
index 532879c..98f7592 100644
--- a/rules/aip0133/aip0133.go
+++ b/rules/aip0133/aip0133.go
@@ -40,6 +40,7 @@ func AddRules(r lint.RuleRegistry) error {
 		requestParentField,
 		requestParentReference,
 		requestParentRequired,
+		requestRequiredFields,
 		requestResourceBehavior,
 		resourceField,
 		resourceReferenceType,
diff --git a/rules/aip0133/request_required_fields.go b/rules/aip0133/request_required_fields.go
new file mode 100644
index 0000000..00ec361
--- /dev/null
+++ b/rules/aip0133/request_required_fields.go
@@ -0,0 +1,61 @@
+// Copyright 2023 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 aip0133
+
+import (
+	"fmt"
+	"strings"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+	"github.com/jhump/protoreflect/desc/builder"
+	"github.com/stoewer/go-strcase"
+)
+
+// The create request message should not have unrecognized fields.
+var requestRequiredFields = &lint.MessageRule{
+	Name:   lint.NewRuleName(133, "request-required-fields"),
+	OnlyIf: isCreateRequestMessage,
+	LintMessage: func(m *desc.MessageDescriptor) (problems []lint.Problem) {
+		resourceMsgName := getResourceMsgNameFromReq(m)
+
+		// Rule check: Establish that there are no unexpected fields.
+		allowedRequiredFields := map[string]*builder.FieldType{
+			"parent": nil, // AIP-133
+			fmt.Sprintf("%s_id", strings.ToLower(strcase.SnakeCase(resourceMsgName))): nil,
+		}
+
+		for _, f := range m.GetFields() {
+			if !utils.GetFieldBehavior(f).Contains("REQUIRED") {
+				continue
+			}
+			// Skip the check with the field that is the body.
+			if t := f.GetMessageType(); t != nil && t.GetName() == resourceMsgName {
+				continue
+			}
+			// Iterate remaining fields. If they're not in the allowed list,
+			// add a problem.
+			if _, ok := allowedRequiredFields[string(f.GetName())]; !ok {
+				problems = append(problems, lint.Problem{
+					Message:    fmt.Sprintf("Create RPCs must only require fields explicitly described in AIPs, not %q.", f.GetName()),
+					Descriptor: f,
+				})
+			}
+		}
+
+		return problems
+	},
+}
diff --git a/rules/aip0133/request_unknown_fields.go b/rules/aip0133/request_unknown_fields.go
index 85a4a20..33f4345 100644
--- a/rules/aip0133/request_unknown_fields.go
+++ b/rules/aip0133/request_unknown_fields.go
@@ -39,13 +39,12 @@ var unknownFields = &lint.MessageRule{
 			fmt.Sprintf("%s_id", strings.ToLower(strcase.SnakeCase(resourceMsgName))): nil,
 		}
 
-		for _, fieldDesc := range m.GetFields() {
-			if msgDesc := fieldDesc.GetMessageType(); msgDesc != nil && msgDesc.GetName() == resourceMsgName {
-				allowedFields[fieldDesc.GetName()] = nil // AIP-133
-			}
-		}
-
 		for _, field := range m.GetFields() {
+			// Skip the check with the field that is the body.
+			if t := field.GetMessageType(); t != nil && t.GetName() == resourceMsgName {
+				continue
+			}
+			// Check the remaining fields.
 			if _, ok := allowedFields[string(field.GetName())]; !ok {
 				problems = append(problems, lint.Problem{
 					Message: fmt.Sprintf(


Test patch:
diff --git a/rules/aip0133/request_required_fields_test.go b/rules/aip0133/request_required_fields_test.go
new file mode 100644
index 0000000..a16e370
--- /dev/null
+++ b/rules/aip0133/request_required_fields_test.go
@@ -0,0 +1,101 @@
+// Copyright 2023 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 aip0133
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+func TestRequiredFieldTests(t *testing.T) {
+	for _, test := range []struct {
+		name                 string
+		Fields               string
+		problematicFieldName string
+		problems             testutils.Problems
+	}{
+		{
+			"ValidNoExtraFields",
+			"",
+			"",
+			nil,
+		},
+		{
+			"ValidOptionalValidateOnly",
+			"string validate_only = 3 [(google.api.field_behavior) = OPTIONAL];",
+			"validate_only",
+			nil,
+		},
+		{
+			"InvalidRequiredValidateOnly",
+			"bool validate_only = 3 [(google.api.field_behavior) = REQUIRED];",
+			"validate_only",
+			testutils.Problems{
+				{Message: `Create RPCs must only require fields explicitly described in AIPs, not "validate_only"`},
+			},
+		},
+		{
+			"InvalidRequiredUnknownField",
+			"bool create_iam = 3 [(google.api.field_behavior) = REQUIRED];",
+			"create_iam",
+			testutils.Problems{
+				{Message: `Create RPCs must only require fields explicitly described in AIPs, not "create_iam"`},
+			},
+		},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				import "google/api/annotations.proto";
+				import "google/api/field_behavior.proto";
+				import "google/api/resource.proto";
+
+				service Library {
+					rpc CreateBook(CreateBookRequest) returns (Book) {
+						option (google.api.http) = {
+							delete: "/v1/{name=publishers/*/books/*}"
+						};
+					}
+				}
+
+				message Book {
+					option (google.api.resource) = {
+						type: "library.googleapis.com/Book"
+						pattern: "publishers/{publisher}/books/{book}"
+					};
+					string name = 1;
+				}
+
+				message CreateBookRequest {
+					string parent = 1 [
+						(google.api.field_behavior) = REQUIRED
+					];
+					Book book = 2 [
+						(google.api.field_behavior) = REQUIRED
+					];
+					{{.Fields}}
+				}
+			`, test)
+			var dbr desc.Descriptor = f.FindMessage("CreateBookRequest")
+			if test.problematicFieldName != "" {
+				dbr = f.FindMessage("CreateBookRequest").FindFieldByName(test.problematicFieldName)
+			}
+			if diff := test.problems.SetDescriptor(dbr).Diff(requestRequiredFields.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}