Repository: googleapis/api-linter
Pull request #657 (2020-10-12 22:24:02)

feat: Add rule for validate_only for declarative-friendly APIs.


Patch:
diff --git a/docs/rules/0163/declarative-friendly-required.md b/docs/rules/0163/declarative-friendly-required.md
new file mode 100644
index 0000000..5305635
--- /dev/null
+++ b/docs/rules/0163/declarative-friendly-required.md
@@ -0,0 +1,73 @@
+---
+rule:
+  aip: 163
+  name: [core, '0163', declarative-friendly-required]
+  summary: Declarative-friendly mutations should have a validate_only field.
+permalink: /163/declarative-friendly-required
+redirect_from:
+  - /0163/declarative-friendly-required
+---
+
+# Required etags
+
+This rule enforces that declarative-friendly mutations have a `validate_only`
+field, as mandated in [AIP-163][].
+
+## Details
+
+This rule looks at any mutation (`POST`, `PATCH`, `DELETE`) connected to a
+resource with a `google.api.resource` annotation that includes
+`style: DECLARATIVE_FRIENDLY`, and complains if it lacks a `bool validate_only`
+field.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+// Assuming that Book is styled declarative-friendly...
+message DeleteBookRequest {
+  string name = 1 [(google.api.resource_reference) = {
+    type: "library.googleapis.com/Book"
+  }];
+  // A bool validate_only field should exist.
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+// Assuming that Book is styled declarative-friendly...
+message DeleteBookRequest {
+  string name = 1 [(google.api.resource_reference) = {
+    type: "library.googleapis.com/Book"
+  }];
+  bool validate_only = 2;
+}
+```
+
+## 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::0163::declarative-friendly-required=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+message DeleteBookRequest {
+  string name = 1 [(google.api.resource_reference) = {
+    type: "library.googleapis.com/Book"
+  }];
+}
+```
+
+**Note:** Violations of declarative-friendly rules should be rare, as tools are
+likely to expect strong consistency.
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-163]: https://aip.dev/163
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0163/aip0163.go b/rules/aip0163/aip0163.go
index 0499b36..d6cf188 100644
--- a/rules/aip0163/aip0163.go
+++ b/rules/aip0163/aip0163.go
@@ -23,6 +23,7 @@ import (
 func AddRules(r lint.RuleRegistry) error {
 	return r.Register(
 		163,
+		declarativeFriendlyRequired,
 		synonyms,
 	)
 }
diff --git a/rules/aip0163/declarative_friendly_required.go b/rules/aip0163/declarative_friendly_required.go
new file mode 100644
index 0000000..cdb8880
--- /dev/null
+++ b/rules/aip0163/declarative_friendly_required.go
@@ -0,0 +1,51 @@
+// Copyright 2020 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 aip0163
+
+import (
+	"strings"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var declarativeFriendlyRequired = &lint.MessageRule{
+	Name: lint.NewRuleName(163, "declarative-friendly-required"),
+	OnlyIf: func(m *desc.MessageDescriptor) bool {
+		// We only want to look at request methods, not the resources themselves.
+		if name := m.GetName(); strings.HasSuffix(name, "Request") && utils.IsDeclarativeFriendlyMessage(m) {
+			// If the corresponding method is a GET method, it does not need
+			// validate_only.
+			method := utils.FindMethod(m.GetFile(), strings.TrimSuffix(name, "Request"))
+			for _, http := range utils.GetHTTPRules(method) {
+				if http.Method == "GET" {
+					return false
+				}
+			}
+			return true
+		}
+		return false
+	},
+	LintMessage: func(m *desc.MessageDescriptor) []lint.Problem {
+		if vo := m.FindFieldByName("validate_only"); vo == nil || utils.GetTypeName(vo) != "bool" {
+			return []lint.Problem{{
+				Message:    "Declarative-friendly mutate requests should include `bool validate_only`.",
+				Descriptor: m,
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0163/declarative_friendly_required_test.go b/rules/aip0163/declarative_friendly_required_test.go
new file mode 100644
index 0000000..7604aea
--- /dev/null
+++ b/rules/aip0163/declarative_friendly_required_test.go
@@ -0,0 +1,79 @@
+// Copyright 2020 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 aip0163
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestDeclarativeFriendlyRequired(t *testing.T) {
+	problems := testutils.Problems{{Message: "`bool validate_only`"}}
+	for _, test := range []struct {
+		name         string
+		Style        string
+		ValidateOnly string
+		problems     testutils.Problems
+	}{
+		{"ValidNotDF", "", "", nil},
+		{"ValidDF", "style: DECLARATIVE_FRIENDLY", "bool validate_only = 2;", nil},
+		{"InvalidDFWrongType", "style: DECLARATIVE_FRIENDLY", "int32 validate_only = 2;", problems},
+		{"InvalidDFMissing", "style: DECLARATIVE_FRIENDLY", "", problems},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				import "google/api/annotations.proto";
+				import "google/api/resource.proto";
+
+				service Library {
+					rpc GetBook(GetBookRequest) returns (Book) {
+						option (google.api.http) = {
+							get: "/v1/{name=publishers/*/books/*}"
+						};
+					}
+
+					rpc DeleteBook(DeleteBookRequest) 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}"
+						{{.Style}}
+					};
+					string name = 1;
+				}
+
+				message GetBookRequest {
+					string name = 1;
+				}
+
+				message DeleteBookRequest {
+					string name = 1;
+					{{.ValidateOnly}}
+				}
+			`, test)
+			dbr := f.FindMessage("DeleteBookRequest")
+			if diff := test.problems.SetDescriptor(dbr).Diff(declarativeFriendlyRequired.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}