Repository: googleapis/api-linter
Pull request #438 (2019-12-10 21:27:46)

[Refactor] Split 0132::request-parernt-field into two rules.


Patch:
diff --git a/docs/rules/0132/request-parent-field.md b/docs/rules/0132/request-parent-field.md
index a7087ea..249754a 100644
--- a/docs/rules/0132/request-parent-field.md
+++ b/docs/rules/0132/request-parent-field.md
@@ -15,8 +15,8 @@ field in the request message, as mandated in [AIP-132](http://aip.dev/132).
 
 ## Details
 
-This rule looks at any message matching `List*Request` and complains if either
-the `parent` field is missing, or if it has any type other than `string`.
+This rule looks at any message matching `List*Request` and complains if the 
+`parent` field has any type other than `string`.
 
 ## Examples
 
@@ -25,16 +25,8 @@ the `parent` field is missing, or if it has any type other than `string`.
 ```proto
 // Incorrect.
 message ListBooksRequest {
-  string publisher = 1;  // Field name should be `parent`.
-  int32 page_size = 2;
-  string page_token = 3;
-}
-```
-
-```proto
-// Incorrect.
-message ListBooksRequest {
-  bytes parent = 1;  // Field type should be `string`.
+  // Field type should be `string`.
+  bytes parent = 1;
   int32 page_size = 2;
   string page_token = 3;
 }
@@ -53,15 +45,14 @@ message ListBooksRequest {
 
 ## Disabling
 
-If you need to violate this rule, use a leading comment above the message (if
-the `parent` 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::0132::request-parent-field=disabled
-//     aip.dev/not-precedent: We need to do this because reasons. --)
 message ListBooksRequest {
-  string publisher = 1;
+  // (-- api-linter: core::0132::request-parent-field=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  bytes parent = 1;
   int32 page_size = 2;
   string page_token = 3;
 }
diff --git a/docs/rules/0132/request-parent-required.md b/docs/rules/0132/request-parent-required.md
new file mode 100644
index 0000000..40b41af
--- /dev/null
+++ b/docs/rules/0132/request-parent-required.md
@@ -0,0 +1,64 @@
+---
+rule:
+  aip: 132
+  name: [core, '0132', request-parent-required]
+  summary: List RPCs must have a `parent` field in the request.
+permalink: /132/request-parent-required
+redirect_from:
+  - /0132/request-parent-required
+---
+
+# List methods: Parent field
+
+This rule enforces that all `List` standard methods have a `string parent`
+field in the request message, as mandated in [AIP-132](http://aip.dev/132).
+
+## Details
+
+This rule looks at any message matching `List*Request` and complains if
+the `parent` field is missing.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message ListBooksRequest {
+  // Field name should be `parent`.
+  string publisher = 1;  
+  int32 page_size = 2;
+  string page_token = 3;
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message ListBooksRequest {
+  string parent = 1;
+  int32 page_size = 2;
+  string page_token = 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::0132::request-parent-required=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+message ListBooksRequest {
+  string publisher = 1;
+  int32 page_size = 2;
+  string page_token = 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/aip0132/aip0132.go b/rules/aip0132/aip0132.go
index e13b1bb..aafdcd6 100644
--- a/rules/aip0132/aip0132.go
+++ b/rules/aip0132/aip0132.go
@@ -30,10 +30,11 @@ func AddRules(r lint.RuleRegistry) error {
 		requestFieldTypes,
 		requestMessageName,
 		requestParentBehavior,
+		requestParentField,
 		requestParentReference,
+		requestParentRequired,
 		responseMessageName,
 		responseUnknownFields,
-		standardFields,
 		unknownFields,
 	)
 }
diff --git a/rules/aip0132/request_parent_field.go b/rules/aip0132/request_parent_field.go
index 4e4ae48..358b55e 100644
--- a/rules/aip0132/request_parent_field.go
+++ b/rules/aip0132/request_parent_field.go
@@ -15,35 +15,26 @@
 package aip0132
 
 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 List standard method should contain a parent field.
-var standardFields = &lint.MessageRule{
-	Name:   lint.NewRuleName(132, "request-parent-field"),
-	OnlyIf: isListRequestMessage,
-	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 the List request message should
+// be string.
+var requestParentField = &lint.FieldRule{
+	Name: lint.NewRuleName(132, "request-parent-field"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isListRequestMessage(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 List RPCs must be a string",
 				Suggestion: "string",
-				Descriptor: parentField,
-				Location:   locations.FieldType(parentField),
+				Descriptor: f,
+				Location:   locations.FieldType(f),
 			}}
 		}
 
diff --git a/rules/aip0132/request_parent_required.go b/rules/aip0132/request_parent_required.go
new file mode 100644
index 0000000..2a52c32
--- /dev/null
+++ b/rules/aip0132/request_parent_required.go
@@ -0,0 +1,25 @@
+package aip0132
+
+import (
+	"fmt"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/jhump/protoreflect/desc"
+)
+
+// The List standard method should contain a parent field.
+var requestParentRequired = &lint.MessageRule{
+	Name:   lint.NewRuleName(132, "request-parent-required"),
+	OnlyIf: isListRequestMessage,
+	LintMessage: func(m *desc.MessageDescriptor) []lint.Problem {
+		// Rule check: Establish that a `parent` field is present.
+		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/aip0132/request_parent_field_test.go b/rules/aip0132/request_parent_field_test.go
index ed0a810..5b69758 100644
--- a/rules/aip0132/request_parent_field_test.go
+++ b/rules/aip0132/request_parent_field_test.go
@@ -18,56 +18,39 @@ import (
 	"testing"
 
 	"github.com/googleapis/api-linter/rules/internal/testutils"
-	"github.com/jhump/protoreflect/desc"
-	"github.com/jhump/protoreflect/desc/builder"
 )
 
-func TestStandardFields(t *testing.T) {
+func TestRequestParentField(t *testing.T) {
 	// Set up the testing permutations.
 	tests := []struct {
-		testName      string
-		messageName   string
-		nameFieldName string
-		nameFieldType *builder.FieldType
-		problems      testutils.Problems
-		problemDesc   func(m *desc.MessageDescriptor) desc.Descriptor
+		name        string
+		MessageName string
+		FieldName   string
+		FieldType   string
+		problems    testutils.Problems
 	}{
-		{"Valid", "ListBooksRequest", "parent", builder.FieldTypeString(), testutils.Problems{}, nil},
-		{"InvalidName", "ListBooksRequest", "publisher", builder.FieldTypeString(), testutils.Problems{{Message: "no `parent` field"}}, nil},
+		{"Valid", "ListBooksRequest", "parent", "string", nil},
+		{"IrrelevantMessageName", "EnumerateBooksRequest", "id", "bytes", nil},
+		{"IrrelevantFieldName", "ListBooksRequest", "id", "bytes", nil},
 		{
 			"InvalidType",
 			"ListBooksRequest",
 			"parent",
-			builder.FieldTypeBytes(),
+			"bytes",
 			testutils.Problems{{Suggestion: "string"}},
-			func(m *desc.MessageDescriptor) desc.Descriptor {
-				return m.GetFields()[0]
-			},
 		},
-		{"Irrelevant", "EnumerateBooksRequest", "id", builder.FieldTypeString(), testutils.Problems{}, nil},
 	}
 
-	// Run each test individually.
 	for _, test := range tests {
-		t.Run(test.testName, func(t *testing.T) {
-			// Create an appropriate message descriptor.
-			message, err := builder.NewMessage(test.messageName).AddField(
-				builder.NewField(test.nameFieldName, test.nameFieldType),
-			).Build()
-			if err != nil {
-				t.Fatalf("Could not build %s message.", test.messageName)
-			}
-
-			// What descriptor is the problem expected to be attached 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.
-			// number of problems.
-			problems := standardFields.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/aip0132/request_parent_required_test.go b/rules/aip0132/request_parent_required_test.go
new file mode 100644
index 0000000..c21cdc1
--- /dev/null
+++ b/rules/aip0132/request_parent_required_test.go
@@ -0,0 +1,34 @@
+package aip0132
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestRequestParentRequired(t *testing.T) {
+	tests := []struct {
+		name        string
+		MessageName string
+		FieldName   string
+		problems    testutils.Problems
+	}{
+		{"Valid", "ListBooksRequest", "parent", nil},
+		{"InvalidName", "ListBooksRequest", "publisher", testutils.Problems{{Message: "no `parent` field"}}},
+		{"Irrelevant", "EnumerateBooksRequest", "id", nil},
+	}
+
+	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)
+			if diff := test.problems.SetDescriptor(f.GetMessageTypes()[0]).Diff(problems); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}