Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add rules for AIP-162 Delete Revision HTTP body and method #752

Merged
merged 1 commit into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions docs/rules/0162/delete-revision-http-body.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
rule:
aip: 162
name: [core, '0162', delete-revision-http-body]
summary: Delete Revision methods should not have an HTTP body.
permalink: /162/delete-revision-http-body
redirect_from:
- /0162/delete-revision-http-body
---

# Delete Revision methods: HTTP body

This rule enforces that all Delete Revision RPCs have no HTTP `body`, as mandated in
[AIP-162][].

## Details

This rule looks at any method matching `Delete*Revision`, and complains
if it has an HTTP `body`.

## Examples

**Incorrect** code for this rule:

```proto
// Incorrect.
rpc DeleteBookRevision(DeleteBookRevisionRequest) returns (google.protobuf.Empty) {
option (google.api.http) = {
delete: "/v1/{name=publishers/*/books/*}:deleteRevision"
body: "*" // This should be removed.
};
}
```

**Correct** code for this rule:

```proto
// Correct.
rpc DeleteBookRevision(DeleteBookRevisionRequest) returns (google.protobuf.Empty) {
option (google.api.http) = {
delete: "/v1/{name=publishers/*/books/*}:deleteRevision"
};
}
```

## 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
// (-- api-linter: core::0162::delete-revision-http-body=disabled
// aip.dev/not-precedent: We need to do this because reasons. --)
rpc DeleteBookRevision(DeleteBookRevisionRequest) returns (google.protobuf.Empty) {
option (google.api.http) = {
delete: "/v1/{name=publishers/*/books/*}:deleteRevision"
body: "*"
};
}
```

If you need to violate this rule for an entire file, place the comment at the
top of the file.

[aip-162]: https://aip.dev/162
[aip.dev/not-precedent]: https://aip.dev/not-precedent
65 changes: 65 additions & 0 deletions docs/rules/0162/delete-revision-http-method.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
rule:
aip: 162
name: [core, '0162', delete-revision-http-method]
summary: Delete Revision methods must use the DELETE HTTP verb.
permalink: /162/delete-revision-http-method
redirect_from:
- /0162/delete-revision-http-method
---

# Delete Revision methods: POST HTTP verb

This rule enforces that all Delete Revision RPCs use the `DELETE` HTTP verb, as
mandated in [AIP-162][].

## Details

This rule looks at any method matching `Delete*Revision`, and complains
if the HTTP verb is anything other than `DELETE`. It _does_ check additional
bindings if they are present.

## Examples

**Incorrect** code for this rule:

```proto
// Incorrect.
rpc DeleteBookRevision(DeleteBookRevisionRequest) returns (google.protobuf.Empty) {
option (google.api.http) = {
post: "/v1/{name=publishers/*/books/*}:deleteRevision" // Should be `delete:`.
};
}
```

**Correct** code for this rule:

```proto
// Correct.
rpc DeleteBookRevision(DeleteBookRevisionRequest) returns (google.protobuf.Empty) {
option (google.api.http) = {
delete: "/v1/{name=publishers/*/books/*}:deleteRevision"
};
}
```

## 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
// (-- api-linter: core::0162::delete-revision-http-method=disabled
// aip.dev/not-precedent: We need to do this because reasons. --)
rpc DeleteBookRevision(DeleteBookRevisionRequest) returns (google.protobuf.Empty) {
option (google.api.http) = {
post: "/v1/{name=publishers/*/books/*}:deleteRevision"
};
}
```

If you need to violate this rule for an entire file, place the comment at the
top of the file.

[aip-162]: https://aip.dev/162
[aip.dev/not-precedent]: https://aip.dev/not-precedent
9 changes: 9 additions & 0 deletions rules/aip0162/aip0162.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func AddRules(r lint.RuleRegistry) error {
commitRequestNameField,
commitRequestNameReference,
commitResponseMessageName,
deleteRevisionHTTPBody,
deleteRevisionHTTPMethod,
rollbackHTTPBody,
rollbackHTTPMethod,
rollbackHTTPURISuffix,
Expand Down Expand Up @@ -97,3 +99,10 @@ func isRollbackMethod(m *desc.MethodDescriptor) bool {
func isRollbackRequestMessage(m *desc.MessageDescriptor) bool {
return rollbackReqMessageRegexp.MatchString(m.GetName())
}

var deleteRevisionMethodRegexp = regexp.MustCompile(`^Delete(?:[A-Za-z0-9]+)Revision$`)

// Returns true if this is an AIP-162 Delete Revision method, false otherwise.
func isDeleteRevisionMethod(m *desc.MethodDescriptor) bool {
return deleteRevisionMethodRegexp.MatchString(m.GetName())
}
27 changes: 27 additions & 0 deletions rules/aip0162/delete_revision_http_body.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2021 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 aip0162

import (
"github.com/googleapis/api-linter/lint"
"github.com/googleapis/api-linter/rules/internal/utils"
)

// Delete Revision methods should have no HTTP body.
var deleteRevisionHTTPBody = &lint.MethodRule{
Name: lint.NewRuleName(162, "delete-revision-http-body"),
OnlyIf: isDeleteRevisionMethod,
LintMethod: utils.LintNoHTTPBody,
}
57 changes: 57 additions & 0 deletions rules/aip0162/delete_revision_http_body_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2021 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 aip0162

import (
"testing"

"github.com/googleapis/api-linter/rules/internal/testutils"
)

func TestDeleteRevisionHTTPBody(t *testing.T) {
tests := []struct {
testName string
Body string
MethodName string
problems testutils.Problems
}{
{"Valid", "", "DeleteBookRevision", nil},
{"Invalid", "*", "DeleteBookRevision", testutils.Problems{{Message: "HTTP body"}}},
{"Irrelevant", "*", "AcquireBook", nil},
}

for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
file := testutils.ParseProto3Tmpl(t, `
import "google/api/annotations.proto";
import "google/protobuf/empty.proto";
service Library {
rpc {{.MethodName}}({{.MethodName}}Request) returns (google.protobuf.Empty) {
option (google.api.http) = {
delete: "/v1/{name=publishers/*/books/*}:deleteRevision"
body: "{{.Body}}"
};
}
}
message {{.MethodName}}Request {}
`, test)
method := file.GetServices()[0].GetMethods()[0]
problems := deleteRevisionHTTPBody.Lint(file)
if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
t.Error(diff)
}
})
}
}
27 changes: 27 additions & 0 deletions rules/aip0162/delete_revision_http_method.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2021 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 aip0162

import (
"github.com/googleapis/api-linter/lint"
"github.com/googleapis/api-linter/rules/internal/utils"
)

// Delete Revision methods should use the HTTP DELETE method.
var deleteRevisionHTTPMethod = &lint.MethodRule{
Name: lint.NewRuleName(162, "delete-revision-http-method"),
OnlyIf: isDeleteRevisionMethod,
LintMethod: utils.LintHTTPMethod("DELETE"),
}
58 changes: 58 additions & 0 deletions rules/aip0162/delete_revision_http_method_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2021 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 aip0162

import (
"testing"

"github.com/googleapis/api-linter/rules/internal/testutils"
)

func TestDeleteRevisionHTTPMethod(t *testing.T) {
// Set up testing permutations.
tests := []struct {
testName string
Method string
MethodName string
problems testutils.Problems
}{
{"Valid", "delete", "DeleteBookRevision", nil},
{"Invalid", "post", "DeleteBookRevision", testutils.Problems{{Message: "HTTP DELETE"}}},
{"Irrelevant", "post", "AcquireBook", nil},
}

// Run each test.
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
file := testutils.ParseProto3Tmpl(t, `
import "google/api/annotations.proto";
import "google/protobuf/empty.proto";
service Library {
rpc {{.MethodName}}({{.MethodName}}Request) returns (google.protobuf.Empty) {
option (google.api.http) = {
{{.Method}}: "/v1/{name=publishers/*/books/*}:deleteRevision"
};
}
}
message {{.MethodName}}Request {}
`, test)
method := file.GetServices()[0].GetMethods()[0]
problems := deleteRevisionHTTPMethod.Lint(file)
if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
t.Error(diff)
}
})
}
}