Skip to content

Commit

Permalink
feat(AIP-122): Flag self-link fields in resources
Browse files Browse the repository at this point in the history
  • Loading branch information
acamadeo committed Dec 6, 2023
1 parent ebf2763 commit de603f6
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
76 changes: 76 additions & 0 deletions docs/rules/0122/no-self-links.md
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
1 change: 1 addition & 0 deletions rules/aip0122/aip0122.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func AddRules(r lint.RuleRegistry) error {
resourceCollectionIdentifiers,
httpURICase,
nameSuffix,
noSelfLinks,
resourceReferenceType,
resourceIdOutputOnly,
embeddedResource,
Expand Down
40 changes: 40 additions & 0 deletions rules/aip0122/no_self_links.go
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
},
}
50 changes: 50 additions & 0 deletions rules/aip0122/no_self_links_test.go
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)
}
})
}
}

0 comments on commit de603f6

Please sign in to comment.