Repository: googleapis/api-linter
Pull request #429 (2019-12-06 18:46:09)

[AIP-133] Create: The resource field should be required.


Patch:
diff --git a/docs/rules/0133/request-parent-behavior.md b/docs/rules/0133/request-parent-behavior.md
index 149b1cd..8c97b74 100644
--- a/docs/rules/0133/request-parent-behavior.md
+++ b/docs/rules/0133/request-parent-behavior.md
@@ -32,7 +32,7 @@ message CreateBooksRequest {
   string parent = 1 [(google.api.resource_reference) = {
     type: "library.googleapis.com/Publisher"
   }];
-  Book book = 2;
+  Book book = 2 [(google.api.field_behavior) = REQUIRED];
 }
 ```
 
@@ -45,7 +45,7 @@ message CreateBooksRequest {
     (google.api.field_behavior) = REQUIRED,
     (google.api.resource_reference).type = "library.googleapis.com/Publisher"
   ];
-  Book book = 2;
+  Book book = 2 [(google.api.field_behavior) = REQUIRED];
 }
 ```
 
@@ -61,7 +61,7 @@ message CreateBooksRequest {
   string parent = 1 [(google.api.resource_reference) = {
     type: "library.googleapis.com/Publisher"
   }];
-  Book book = 2;
+  Book book = 2 [(google.api.field_behavior) = REQUIRED];
 }
 ```
 
diff --git a/docs/rules/0133/request-resource-behavior.md b/docs/rules/0133/request-resource-behavior.md
new file mode 100644
index 0000000..a417a58
--- /dev/null
+++ b/docs/rules/0133/request-resource-behavior.md
@@ -0,0 +1,74 @@
+---
+rule:
+  aip: 133
+  name: [core, '0133', request-resource-behavior]
+  summary: |
+    Create RPCs should annotate the resource field with `google.api.field_behavior`.
+permalink: /133/request-resource-behavior
+redirect_from:
+  - /0133/request-resource-behavior
+---
+
+# Create methods: Field behavior
+
+This rule enforces that all `Create` standard methods have
+`google.api.field_behavior` set to `REQUIRED` on the field representing the
+resource, as mandated in [AIP-133][].
+
+## Details
+
+This rule looks at any message matching `Create*Request` and complains if the
+resource field does not have a `google.api.field_behavior` annotation with a
+value of `REQUIRED`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message CreateBooksRequest {
+  string parent = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Publisher"
+  ];
+  Book book = 2;  // Should also have (google.api.field_behavior) = REQUIRED.
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message CreateBooksRequest {
+  string parent = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Publisher"
+  ];
+  Book book = 2 [(google.api.field_behavior) = REQUIRED];
+}
+```
+
+## 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 CreateBooksRequest {
+  string parent = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Publisher"
+  ];
+
+  // (-- api-linter: core::0133::request-resource-behavior=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  Book book = 2;
+}
+```
+
+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.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0133/aip0133.go b/rules/aip0133/aip0133.go
index af1af7a..d62459f 100644
--- a/rules/aip0133/aip0133.go
+++ b/rules/aip0133/aip0133.go
@@ -33,10 +33,11 @@ func AddRules(r lint.RuleRegistry) error {
 		httpMethod,
 		inputName,
 		outputName,
-		requestParentReference,
-		resourceField,
 		parentField,
 		requestParentBehavior,
+		requestParentReference,
+		requestResourceBehavior,
+		resourceField,
 		synonyms,
 		unknownFields,
 	)
diff --git a/rules/aip0133/request_resource_behavior.go b/rules/aip0133/request_resource_behavior.go
new file mode 100644
index 0000000..9691a32
--- /dev/null
+++ b/rules/aip0133/request_resource_behavior.go
@@ -0,0 +1,50 @@
+// 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 aip0133
+
+import (
+	"fmt"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var requestResourceBehavior = &lint.FieldRule{
+	Name: lint.NewRuleName(133, "request-resource-behavior"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		message := f.GetOwner()
+		if !isCreateRequestMessage(message) {
+			return false
+		}
+		resourceMsgName := getResourceMsgNameFromReq(message)
+		if m := f.GetMessageType(); m != nil && m.GetName() == resourceMsgName {
+			return true
+		}
+		return false
+	},
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		if !utils.GetFieldBehavior(f).Contains("REQUIRED") {
+			return []lint.Problem{{
+				Message: fmt.Sprintf(
+					"Create requests: The `%s` field should include `(google.api.field_behavior) = REQUIRED`.",
+					f.GetName(),
+				),
+				Descriptor: f,
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0133/request_resource_behavior_test.go b/rules/aip0133/request_resource_behavior_test.go
new file mode 100644
index 0000000..402325c
--- /dev/null
+++ b/rules/aip0133/request_resource_behavior_test.go
@@ -0,0 +1,50 @@
+// 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 aip0133
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestRequestResourceBehavior(t *testing.T) {
+	for _, test := range []struct {
+		name          string
+		MessageName   string
+		FieldType     string
+		FieldBehavior string
+		problems      testutils.Problems
+	}{
+		{"Valid", "CreateBookRequest", "Book", " [(google.api.field_behavior) = REQUIRED]", testutils.Problems{}},
+		{"Missing", "CreateBookRequest", "Book", "", testutils.Problems{{Message: "(google.api.field_behavior) = REQUIRED"}}},
+		{"IrrelevantOtherField", "CreateBookRequest", "Parchment", "", testutils.Problems{}},
+		{"IrrelevantNotCreateRequest", "FrobBookRequest", "Book", "", testutils.Problems{}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				import "google/api/field_behavior.proto";
+				message {{.MessageName}} {
+					{{.FieldType}} book = 1{{.FieldBehavior}};
+				}
+				message {{.FieldType}} {}
+			`, test)
+			field := f.GetMessageTypes()[0].GetFields()[0]
+			if diff := test.problems.SetDescriptor(field).Diff(requestResourceBehavior.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}