Skip to content

Commit 16316a5

Browse files
authored
fix(AIP-123): allow name in nested messages (#1325)
1 parent 7adfaba commit 16316a5

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

docs/rules/0123/resource-annotation.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ redirect_from:
1010

1111
# Resource annotation presence
1212

13-
This rule enforces that messages that appear to represent resources have a
14-
`google.api.resource` annotation, as described in [AIP-123][].
13+
This rule enforces that top-level messages that appear to represent resources
14+
have a `google.api.resource` annotation, as described in [AIP-123][].
1515

1616
## Details
1717

18-
This rule scans all messages, and assumes that messages with a `string name`
19-
field are resources unless the message name ends with `Request`. For messages
20-
that are resources, it complains if the `google.api.resource` annotation is
21-
missing.
18+
This rule scans all top-level messages, and assumes that messages with a
19+
`string name` field are resources unless the message name ends with `Request`.
20+
For messages that are resources, it complains if the `google.api.resource`
21+
annotation is missing.
2222

2323
## Examples
2424

rules/aip0123/aip0123.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ func AddRules(r lint.RuleRegistry) error {
4949
}
5050

5151
func isResourceMessage(m *desc.MessageDescriptor) bool {
52+
// If the parent of this message is a message, it is nested and shoudn't
53+
// be considered a resource, even if it has a name field.
54+
_, nested := m.GetParent().(*desc.MessageDescriptor)
5255
return m.FindFieldByName("name") != nil && !strings.HasSuffix(m.GetName(), "Request") &&
53-
!strings.HasSuffix(m.GetName(), "Response")
56+
!strings.HasSuffix(m.GetName(), "Response") && !nested
5457
}
5558

5659
func hasResourceAnnotation(m *desc.MessageDescriptor) bool {

rules/aip0123/resource_annotation_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ func TestResourceAnnotation(t *testing.T) {
3838
}
3939
})
4040

41+
t.Run("SkipNested", func(t *testing.T) {
42+
f := testutils.ParseProto3String(t, `
43+
message Foo {
44+
message Bar {
45+
string name = 1;
46+
}
47+
Bar bar = 1;
48+
}
49+
`)
50+
if diff := (testutils.Problems{}).Diff(resourceAnnotation.Lint(f)); diff != "" {
51+
t.Errorf(diff)
52+
}
53+
})
54+
4155
// The rule should fail if the option is absent on a resource message,
4256
// but pass on messages that are not resource messages.
4357
for _, test := range []struct {

0 commit comments

Comments
 (0)