Repository: googleapis/api-linter
Pull request #439 (2019-12-10 21:55:37)

[Refactor] Split 0133::request-parent-field into two rules.


Patch:
diff --git a/docs/rules/0133/request-parent-field.md b/docs/rules/0133/request-parent-field.md
index 75646a9..0ff5af4 100644
--- a/docs/rules/0133/request-parent-field.md
+++ b/docs/rules/0133/request-parent-field.md
@@ -16,26 +16,17 @@ field in the request message, as mandated in [AIP-133](http://aip.dev/133).
 ## Details
 
 This rule looks at any message matching `Create*Request` and complains if
-either the `parent` field is missing, or if it has any type other than
-`string`.
+the `parent` field has any type other than `string`.
 
 ## Examples
 
 **Incorrect** code for this rule:
 
-```proto
-// Incorrect.
-message CreateBookRequest {
-  string publisher = 1;  // Field name should be `parent`.
-  Book book = 2;
-  string book_id = 3;
-}
-```
-
 ```proto
 // Incorrect.
 message GetBookRequest {
-  bytes parent = 1;  // Field type should be `string`.
+  // Field type should be `string`.
+  bytes parent = 1;
   Book book = 2;
   string book_id = 3;
 }
@@ -54,15 +45,14 @@ message CreateBookRequest {
 
 ## Disabling
 
-If you need to violate this rule, use a leading comment above the message (if
-the `name` field is missing) or above the field (if it is the wrong type).
+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
-// (-- api-linter: core::0133::request-parent-field=disabled
-//     aip.dev/not-precedent: We need to do this because reasons. --)
 message CreateBookRequest {
-  string publisher = 1;
+  // (-- api-linter: core::0133::request-parent-field=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  bytes parent = 1;
   Book book = 2;
   string book_id = 3;
 }
diff --git a/docs/rules/0133/request-parent-required.md b/docs/rules/0133/request-parent-required.md
new file mode 100644
index 0000000..7b7ffc6
--- /dev/null
+++ b/docs/rules/0133/request-parent-required.md
@@ -0,0 +1,64 @@
+---
+rule:
+  aip: 133
+  name: [core, '0133', request-parent-required]
+  summary: Create RPCs must have a `parent` field in the request.
+permalink: /133/request-parent-required
+redirect_from:
+  - /0133/request-parent-required
+---
+
+# Create methods: Parent field
+
+This rule enforces that all `Create` standard methods have a `string parent`
+field in the request message, as mandated in [AIP-133](http://aip.dev/133).
+
+## Details
+
+This rule looks at any message matching `Create*Request` and complains if 
+the `parent` field is missing.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message CreateBookRequest {
+  // Field name should be `parent`.
+  string publisher = 1;
+  Book book = 2;
+  string book_id = 3;
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message CreateBookRequest {
+  string parent = 1;
+  Book book = 2;
+  string book_id = 3;
+}
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the message.
+Remember to also include an [aip.dev/not-precedent][] comment explaining why.
+
+```proto
+// (-- api-linter: core::0133::request-parent-required=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+message CreateBookRequest {
+  string publisher = 1;
+  Book book = 2;
+  string book_id = 3;
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0133/aip0133.go b/rules/aip0133/aip0133.go
index dd2f769..094ecb6 100644
--- a/rules/aip0133/aip0133.go
+++ b/rules/aip0133/aip0133.go
@@ -34,9 +34,10 @@ func AddRules(r lint.RuleRegistry) error {
 		inputName,
 		methodSignature,
 		outputName,
-		parentField,
 		requestParentBehavior,
+		requestParentField,
 		requestParentReference,
+		requestParentRequired,
 		requestResourceBehavior,
 		resourceField,
 		synonyms,
diff --git a/rules/aip0133/request_parent_field.go b/rules/aip0133/request_parent_field.go
index ab9dae8..dc87818 100644
--- a/rules/aip0133/request_parent_field.go
+++ b/rules/aip0133/request_parent_field.go
@@ -15,35 +15,25 @@
 package aip0133
 
 import (
-	"fmt"
-
 	"github.com/googleapis/api-linter/lint"
 	"github.com/googleapis/api-linter/locations"
 	"github.com/jhump/protoreflect/desc"
 	"github.com/jhump/protoreflect/desc/builder"
 )
 
-// The create request message should have parent field.
-var parentField = &lint.MessageRule{
-	Name:   lint.NewRuleName(133, "request-parent-field"),
-	OnlyIf: isCreateRequestMessage,
-	LintMessage: func(m *desc.MessageDescriptor) []lint.Problem {
-		// Rule check: Establish that a `parent` field is present.
-		parentField := m.FindFieldByName("parent")
-		if parentField == nil {
-			return []lint.Problem{{
-				Message:    fmt.Sprintf("Message %q has no `parent` field", m.GetName()),
-				Descriptor: m,
-			}}
-		}
-
-		// Rule check: Establish that the parent field is a string.
-		if parentField.GetType() != builder.FieldTypeString().GetType() {
+// The type of the parent field in a creat request should be string.
+var requestParentField = &lint.FieldRule{
+	Name: lint.NewRuleName(133, "request-parent-field"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isCreateRequestMessage(f.GetOwner()) && f.GetName() == "parent"
+	},
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		if f.GetType() != builder.FieldTypeString().GetType() {
 			return []lint.Problem{{
 				Message:    "`parent` field on create request message must be a string",
 				Suggestion: "string",
-				Descriptor: parentField,
-				Location:   locations.FieldType(parentField),
+				Descriptor: f,
+				Location:   locations.FieldType(f),
 			}}
 		}
 
diff --git a/rules/aip0133/request_parent_required.go b/rules/aip0133/request_parent_required.go
new file mode 100644
index 0000000..e1b18f7
--- /dev/null
+++ b/rules/aip0133/request_parent_required.go
@@ -0,0 +1,23 @@
+package aip0133
+
+import (
+	"fmt"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var requestParentRequired = &lint.MessageRule{
+	Name:   lint.NewRuleName(133, "request-parent-required"),
+	OnlyIf: isCreateRequestMessage,
+	LintMessage: func(m *desc.MessageDescriptor) []lint.Problem {
+		if m.FindFieldByName("parent") == nil {
+			return []lint.Problem{{
+				Message:    fmt.Sprintf("Message %q has no `parent` field", m.GetName()),
+				Descriptor: m,
+			}}
+		}
+
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0133/request_parent_field_test.go b/rules/aip0133/request_parent_field_test.go
index 19ea1e6..0c08f1e 100644
--- a/rules/aip0133/request_parent_field_test.go
+++ b/rules/aip0133/request_parent_field_test.go
@@ -18,69 +18,35 @@ import (
 	"testing"
 
 	"github.com/googleapis/api-linter/rules/internal/testutils"
-	"github.com/jhump/protoreflect/desc"
-	"github.com/jhump/protoreflect/desc/builder"
 )
 
-func TestParentField(t *testing.T) {
+func TestRequestParentField(t *testing.T) {
 	// Set up the testing permutations.
 	tests := []struct {
-		testName     string
-		messageName  string
-		messageField *field
-		problems     testutils.Problems
-		problemDesc  func(m *desc.MessageDescriptor) desc.Descriptor
+		name        string
+		MessageName string
+		FieldName   string
+		FieldType   string
+		problems    testutils.Problems
 	}{
-		{
-			"Valid",
-			"CreateBookRequest",
-			&field{"parent", builder.FieldTypeString()},
-			testutils.Problems{},
-			nil},
-		{
-			"MissingField",
-			"CreateBookRequest",
-			nil,
-			testutils.Problems{{Message: "parent"}},
-			nil,
-		},
-		{
-			"InvalidType",
-			"CreateBookRequest",
-			&field{"parent", builder.FieldTypeDouble()},
-			testutils.Problems{{Suggestion: "string"}},
-			func(m *desc.MessageDescriptor) desc.Descriptor {
-				return m.FindFieldByName("parent")
-			},
-		},
+		{"Valid", "CreateBookRequest", "parent", "string", nil},
+		{"InvalidType", "CreateBookRequest", "parent", "bytes", testutils.Problems{{Suggestion: "string"}}},
+		{"IrrelevantMessage", "AddBookRequest", "parent", "bytes", nil},
+		{"IrrelevantField", "CreateBookRequest", "id", "bytes", nil},
 	}
 
 	// Run each test individually.
 	for _, test := range tests {
-		t.Run(test.testName, func(t *testing.T) {
-			// Create an appropriate message descriptor.
-			messageBuilder := builder.NewMessage(test.messageName)
-
-			if test.messageField != nil {
-				messageBuilder.AddField(
-					builder.NewField(test.messageField.fieldName, test.messageField.fieldType),
-				)
-			}
-
-			message, err := messageBuilder.Build()
-			if err != nil {
-				t.Fatalf("Could not build %s message.", test.messageName)
-			}
-
-			// Determine the descriptor that a failing test will attach to.
-			var problemDesc desc.Descriptor = message
-			if test.problemDesc != nil {
-				problemDesc = test.problemDesc(message)
-			}
-
-			// Run the lint rule, and establish that it returns the correct problems.
-			problems := parentField.Lint(message.GetFile())
-			if diff := test.problems.SetDescriptor(problemDesc).Diff(problems); diff != "" {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				message {{.MessageName}} {
+					{{.FieldType}} {{.FieldName}} = 1;
+				}
+			`, test)
+
+			problems := requestParentField.Lint(f)
+			field := f.GetMessageTypes()[0].GetFields()[0]
+			if diff := test.problems.SetDescriptor(field).Diff(problems); diff != "" {
 				t.Errorf(diff)
 			}
 		})
diff --git a/rules/aip0133/request_parent_required_test.go b/rules/aip0133/request_parent_required_test.go
new file mode 100644
index 0000000..d638e8c
--- /dev/null
+++ b/rules/aip0133/request_parent_required_test.go
@@ -0,0 +1,38 @@
+package aip0133
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestRequestParentFieldRequired(t *testing.T) {
+	// Set up the testing permutations.
+	tests := []struct {
+		name        string
+		MessageName string
+		FieldName   string
+		problems    testutils.Problems
+	}{
+		{"Valid", "CreateBookRequest", "parent", nil},
+		{"MissingParent", "CreateBookRequest", "id", testutils.Problems{{Message: "parent"}}},
+		{"IrrelevantMessage", "AddBookRequest", "id", nil},
+	}
+
+	// Run each test individually.
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				message {{.MessageName}} {
+					string {{.FieldName}} = 1;
+				}
+			`, test)
+
+			problems := requestParentRequired.Lint(f)
+			message := f.GetMessageTypes()[0]
+			if diff := test.problems.SetDescriptor(message).Diff(problems); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}