-
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.
feat(AIP-122): Flag self-link fields in resources
- Loading branch information
Showing
4 changed files
with
167 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,76 @@ | ||
--- | ||
rule: | ||
aip: 122 | ||
name: [core, '0122', no-self-links] | ||
summary: Resources should not contain self-links. | ||
permalink: /122/no-self-links | ||
redirect_from: | ||
- /0122/no-self-links | ||
--- | ||
|
||
# No self links | ||
|
||
This rule enforces that resource messages do not contain any fields called | ||
`string self_link`, as mandated in [AIP-122][]. | ||
|
||
## Details | ||
|
||
This rule complains if it sees a resource field of type `string` that is also | ||
named `self_link`. | ||
|
||
## Examples | ||
|
||
**Incorrect** code for this rule: | ||
|
||
```proto | ||
// Incorrect. | ||
message Book { | ||
option (google.api.resource) = { | ||
type: "library.googleapis.com/Book" | ||
pattern: "books/{book}" | ||
}; | ||
string name = 1; | ||
// Incorrect. Resources should contain self-links. | ||
string self_link = 2; | ||
} | ||
``` | ||
|
||
**Correct** code for this rule: | ||
|
||
```proto | ||
// Correct. | ||
message Book { | ||
option (google.api.resource) = { | ||
type: "library.googleapis.com/Book" | ||
pattern: "books/{book}" | ||
}; | ||
string name = 1; | ||
} | ||
``` | ||
|
||
## 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 | ||
// Incorrect. | ||
message Book { | ||
option (google.api.resource) = { | ||
type: "library.googleapis.com/Book" | ||
pattern: "books/{book}" | ||
}; | ||
string name = 1; | ||
// (-- api-linter: core::0122::no-self-links=disabled | ||
// aip.dev/not-precedent: We need to do this because reasons. --) | ||
string self_link = 2; | ||
} | ||
``` | ||
|
||
If you need to violate this rule for an entire file, place the comment at the | ||
top of the file. | ||
|
||
[aip-122]: https://aip.dev/122 | ||
[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,40 @@ | ||
// Copyright 2023 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 aip0122 | ||
|
||
import ( | ||
"github.com/googleapis/api-linter/lint" | ||
"github.com/googleapis/api-linter/rules/internal/utils" | ||
"github.com/jhump/protoreflect/desc" | ||
"google.golang.org/protobuf/types/descriptorpb" | ||
) | ||
|
||
var noSelfLinks = &lint.MessageRule{ | ||
Name: lint.NewRuleName(122, "no-self-links"), | ||
OnlyIf: utils.IsResource, | ||
LintMessage: func(m *desc.MessageDescriptor) []lint.Problem { | ||
problems := []lint.Problem{} | ||
for _, field := range m.GetFields() { | ||
if field.GetType() == descriptorpb.FieldDescriptorProto_TYPE_STRING && | ||
field.GetName() == "self_link" { | ||
problems = append(problems, lint.Problem{ | ||
Message: "Resources must not contain self-links.", | ||
Descriptor: field, | ||
}) | ||
} | ||
} | ||
return problems | ||
}, | ||
} |
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,50 @@ | ||
// Copyright 2023 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 aip0122 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/googleapis/api-linter/rules/internal/testutils" | ||
) | ||
|
||
func TestNoSelfLinks(t *testing.T) { | ||
for _, test := range []struct { | ||
name string | ||
FieldName string | ||
problems testutils.Problems | ||
}{ | ||
{"Valid", "author", nil}, | ||
{"InvalidSelfLink", "self_link", testutils.Problems{{Message: "self-links"}}}, | ||
} { | ||
t.Run(test.name, func(t *testing.T) { | ||
f := testutils.ParseProto3Tmpl(t, ` | ||
import "google/api/resource.proto"; | ||
message Book { | ||
option (google.api.resource) = { | ||
type: "library.googleapis.com/Book" | ||
}; | ||
string name = 1; | ||
string {{ .FieldName }} = 2; | ||
} | ||
`, test) | ||
field := f.GetMessageTypes()[0].GetFields()[1] | ||
if diff := test.problems.SetDescriptor(field).Diff(noSelfLinks.Lint(f)); diff != "" { | ||
t.Errorf(diff) | ||
} | ||
}) | ||
} | ||
} |