Repository: googleapis/api-linter
Pull request #366 (2019-11-20 00:43:25)

[AIP-156] Prohibit List, Create, and Delete on singletons.


Patch:
diff --git a/docs/rules/0156/forbidden-methods.md b/docs/rules/0156/forbidden-methods.md
new file mode 100644
index 0000000..0963b9f
--- /dev/null
+++ b/docs/rules/0156/forbidden-methods.md
@@ -0,0 +1,118 @@
+---
+rule:
+  aip: 156
+  name: [core, '0156', forbidden-methods]
+  summary: Singletons must not define List, Create, or Delete methods.
+permalink: /156/forbidden-methods
+redirect_from:
+  - /0156/forbidden-methods
+---
+
+# Singletons: Forbidden methods
+
+This rule enforces that singleton resources do not define `List`, `Create`, or
+`Delete` methods, as mandated in [AIP-156][].
+
+## Details
+
+This rule looks at any message with a `name` variable in the URI where the name
+ends in anything other than `*`. It assumes that this is a method operating on
+a singleton resource, and complains if the method is a `List`, `Create`, or
+`Delete` standard method.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc GetSettings(GetSettingsRequest) returns (Settings) {
+  option (google.api.http) = {
+    get: "/v1/{name=publishers/*/settings}"
+  };
+}
+
+rpc UpdateSettings(UpdateSettingsRequest) returns (Settings) {
+  option (google.api.http) = {
+    patch: "/v1/{settings.name=publishers/*/settings}"
+    body: "settings"
+  };
+}
+
+// This method should not exist. The settings should be implicitly created
+// when the publisher is created.
+rpc CreateSettings(CreateSettingsRequest) returns (Settings) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/settings}"
+    body: "settings"
+  };
+}
+
+// This method should not exist. The settings should always implicitly exist.
+rpc DeleteSettings(DeleteSettingsRequest) returns (google.protobuf.Empty) {
+  option (google.api.http) = {
+    delete: "/v1/{name=publishers/*/settings}"
+  };
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc GetSettings(GetSettingsRequest) returns (Settings) {
+  option (google.api.http) = {
+    get: "/v1/{name=publishers/*/settings}"
+  };
+}
+
+rpc UpdateSettings(UpdateSettingsRequest) returns (Settings) {
+  option (google.api.http) = {
+    patch: "/v1/{settings.name=publishers/*/settings}"
+    body: "settings"
+  };
+}
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the method.
+Remember to also include an [aip.dev/not-precedent][] comment explaining why.
+
+```proto
+rpc GetSettings(GetSettingsRequest) returns (Settings) {
+  option (google.api.http) = {
+    get: "/v1/{name=publishers/*/settings}"
+  };
+}
+
+rpc UpdateSettings(UpdateSettingsRequest) returns (Settings) {
+  option (google.api.http) = {
+    patch: "/v1/{settings.name=publishers/*/settings}"
+    body: "settings"
+  };
+}
+
+// (-- api-linter: core::0156::forbidden-methods=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc CreateSettings(CreateSettingsRequest) returns (Settings) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/settings}"
+    body: "settings"
+  };
+}
+
+// (-- api-linter: core::0156::forbidden-methods=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc DeleteSettings(DeleteSettingsRequest) returns (google.protobuf.Empty) {
+  option (google.api.http) = {
+    delete: "/v1/{name=publishers/*/settings}"
+  };
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-156]: https://aip.dev/156
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/docs/rules/0156/index.md b/docs/rules/0156/index.md
new file mode 100644
index 0000000..5c12796
--- /dev/null
+++ b/docs/rules/0156/index.md
@@ -0,0 +1,10 @@
+---
+aip_listing: 156
+permalink: /156/
+redirect_from:
+  - /0156/
+---
+
+# Singleton resources
+
+{% include linter-aip-listing.md aip=156 %}
diff --git a/rules/aip0156/aip0156.go b/rules/aip0156/aip0156.go
new file mode 100644
index 0000000..5a88629
--- /dev/null
+++ b/rules/aip0156/aip0156.go
@@ -0,0 +1,28 @@
+// 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 aip0156 contains rules defined in https://aip.dev/156.
+package aip0156
+
+import (
+	"github.com/googleapis/api-linter/lint"
+)
+
+// AddRules adds all of the AIP-156 rules to the provided registry.
+func AddRules(r lint.RuleRegistry) error {
+	return r.Register(
+		156,
+		forbiddenMethods,
+	)
+}
diff --git a/rules/aip0156/forbidden_methods.go b/rules/aip0156/forbidden_methods.go
new file mode 100644
index 0000000..c7c8298
--- /dev/null
+++ b/rules/aip0156/forbidden_methods.go
@@ -0,0 +1,56 @@
+// 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 aip0156
+
+import (
+	"fmt"
+	"strings"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var forbiddenMethods = &lint.MethodRule{
+	Name: lint.NewRuleName(156, "forbidden-methods"),
+	OnlyIf: func(m *desc.MethodDescriptor) bool {
+		// If the `name` variable in the URI ends in something other than
+		// "*", that indicates that this is a singleton.
+		//
+		// For example:
+		//   publishers/*/books/* -- not a singleton, many books
+		//   publishers/*/settings -- a singleton; one settings object per book
+		for _, http := range utils.GetHTTPRules(m) {
+			if name, ok := http.GetVariables()["name"]; ok {
+				if !strings.HasSuffix(name, "*") {
+					return true
+				}
+			}
+		}
+		return false
+	},
+	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
+		// Singletons should not use Create, List, or Delete.
+		for _, badPrefix := range []string{"Create", "List", "Delete"} {
+			if strings.HasPrefix(m.GetName(), badPrefix) {
+				return []lint.Problem{{
+					Message:    fmt.Sprintf("Singletons must not define %q methods.", badPrefix),
+					Descriptor: m,
+				}}
+			}
+		}
+		return nil
+	},
+}
diff --git a/rules/rules.go b/rules/rules.go
index 4852678..aab8ba0 100644
--- a/rules/rules.go
+++ b/rules/rules.go
@@ -65,6 +65,7 @@ import (
 	"github.com/googleapis/api-linter/rules/aip0142"
 	"github.com/googleapis/api-linter/rules/aip0143"
 	"github.com/googleapis/api-linter/rules/aip0151"
+	"github.com/googleapis/api-linter/rules/aip0156"
 	"github.com/googleapis/api-linter/rules/aip0158"
 	"github.com/googleapis/api-linter/rules/aip0191"
 	"github.com/googleapis/api-linter/rules/aip0192"
@@ -92,6 +93,7 @@ var aipAddRulesFuncs = []addRulesFuncType{
 	aip0142.AddRules,
 	aip0143.AddRules,
 	aip0151.AddRules,
+	aip0156.AddRules,
 	aip0158.AddRules,
 	aip0191.AddRules,
 	aip0192.AddRules,


Test patch:
diff --git a/rules/aip0156/aip0156_test.go b/rules/aip0156/aip0156_test.go
new file mode 100644
index 0000000..7eaf056
--- /dev/null
+++ b/rules/aip0156/aip0156_test.go
@@ -0,0 +1,27 @@
+// 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 aip0156
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/lint"
+)
+
+func TestAddRules(t *testing.T) {
+	if err := AddRules(lint.NewRuleRegistry()); err != nil {
+		t.Errorf("AddRules got an error: %v", err)
+	}
+}
diff --git a/rules/aip0156/forbidden_methods_test.go b/rules/aip0156/forbidden_methods_test.go
new file mode 100644
index 0000000..1542365
--- /dev/null
+++ b/rules/aip0156/forbidden_methods_test.go
@@ -0,0 +1,56 @@
+// 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 aip0156
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestForbiddenMethods(t *testing.T) {
+	for _, test := range []struct {
+		name       string
+		MethodName string
+		Suffix     string
+		problems   testutils.Problems
+	}{
+		{"ValidGet", "GetPublisherSettings", "/settings", testutils.Problems{}},
+		{"ValidUpdate", "UpdatePublisherSettings", "/settings", testutils.Problems{}},
+		{"InvalidCreate", "CreatePublisherSettings", "/settings", testutils.Problems{{Message: "Create"}}},
+		{"InvalidList", "ListPublisherSettings", "/settings", testutils.Problems{{Message: "List"}}},
+		{"InvalidDelete", "DeletePublisherSettings", "/settings", testutils.Problems{{Message: "Delete"}}},
+		{"Irrelevant", "CreatePublisher", "", testutils.Problems{}},
+	} {
+		f := testutils.ParseProto3Tmpl(t, `
+			import "google/api/annotations.proto";
+			service Library {
+				rpc {{.MethodName}}({{.MethodName}}Request) returns (Settings) {
+					option (google.api.http) = {
+						post: "/v1/{name=publishers/*{{.Suffix}}}"
+						body: "settings"
+					};
+				}
+			}
+			message {{.MethodName}}Request {}
+			message Settings{}
+		`, test)
+		method := f.GetServices()[0].GetMethods()[0]
+		problems := forbiddenMethods.Lint(f)
+		if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
+			t.Errorf(diff)
+		}
+	}
+}