-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
4 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
--- | ||
rule: | ||
aip: 162 | ||
name: [core, '0162', list-revisions-request-name-reference] | ||
summary: | | ||
List Revisions requests should annotate the `name` field with `google.api.resource_reference`. | ||
permalink: /162/list-revisions-request-name-reference | ||
redirect_from: | ||
- /0162/list-revisions-request-name-reference | ||
--- | ||
|
||
# List Revisions requests: Name resource reference | ||
|
||
This rule enforces that all List Revisions requests have | ||
`google.api.resource_reference` on their `string name` field, as mandated in | ||
[AIP-162][]. | ||
|
||
## Details | ||
|
||
This rule looks at the `name` field of any message matching | ||
`List*RevisionsRequest` and complains if it does not have a | ||
`google.api.resource_reference` annotation. | ||
|
||
## Examples | ||
|
||
**Incorrect** code for this rule: | ||
|
||
```proto | ||
// Incorrect. | ||
message ListBookRevisionsRequest { | ||
// The `google.api.resource_reference` annotation should also be included. | ||
string name = 1 [(google.api.field_behavior) = REQUIRED]; | ||
int32 page_size = 2; | ||
string page_token = 3; | ||
} | ||
``` | ||
|
||
**Correct** code for this rule: | ||
|
||
```proto | ||
// Correct. | ||
message ListBookRevisionsRequest { | ||
string name = 1 [ | ||
(google.api.field_behavior) = REQUIRED, | ||
(google.api.resource_reference).type = "library.googleapis.com/Book" | ||
]; | ||
int32 page_size = 2; | ||
string page_token = 3; | ||
} | ||
``` | ||
|
||
## Disabling | ||
|
||
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 | ||
message ListBookRevisionsRequest { | ||
// (-- api-linter: core::0162::list-revisions-request-name-reference=disabled | ||
// aip.dev/not-precedent: We need to do this because reasons. --) | ||
string name = 1 [(google.api.field_behavior) = REQUIRED]; | ||
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-162]: https://aip.dev/162 | ||
[aip.dev/not-precedent]: https://aip.dev/not-precedent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// 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" | ||
"github.com/jhump/protoreflect/desc" | ||
) | ||
|
||
var listRevisionsRequestNameReference = &lint.FieldRule{ | ||
Name: lint.NewRuleName(162, "list-revisions-request-name-reference"), | ||
OnlyIf: func(f *desc.FieldDescriptor) bool { | ||
return IsListRevisionsRequestMessage(f.GetOwner()) && f.GetName() == "name" | ||
}, | ||
LintField: utils.LintFieldResourceReference, | ||
} |
49 changes: 49 additions & 0 deletions
49
rules/aip0162/list_revisions_request_name_reference_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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 TestListRevisionsRequestNameReference(t *testing.T) { | ||
for _, test := range []struct { | ||
name string | ||
RPC string | ||
Field string | ||
FieldOpts string | ||
problems testutils.Problems | ||
}{ | ||
{"Valid", "ListBookRevisions", "name", ` [(google.api.resource_reference) = { type: "foo" }]`, nil}, | ||
{"Missing", "ListBookRevisions", "name", "", testutils.Problems{{Message: "google.api.resource_reference"}}}, | ||
{"IrrelevantMessage", "ListBooks", "name", "", nil}, | ||
{"IrrelevantField", "ListBookRevisions", "something_else", "", nil}, | ||
} { | ||
t.Run(test.name, func(t *testing.T) { | ||
f := testutils.ParseProto3Tmpl(t, ` | ||
import "google/api/resource.proto"; | ||
message {{.RPC}}Request { | ||
repeated string {{.Field}} = 1{{.FieldOpts}}; | ||
} | ||
`, test) | ||
field := f.GetMessageTypes()[0].GetFields()[0] | ||
if diff := test.problems.SetDescriptor(field).Diff(listRevisionsRequestNameReference.Lint(f)); diff != "" { | ||
t.Error(diff) | ||
} | ||
}) | ||
} | ||
} |