File tree Expand file tree Collapse file tree 3 files changed +24
-7
lines changed Expand file tree Collapse file tree 3 files changed +24
-7
lines changed Original file line number Diff line number Diff line change @@ -10,15 +10,15 @@ redirect_from:
10
10
11
11
# Resource annotation presence
12
12
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] [ ] .
15
15
16
16
## Details
17
17
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.
22
22
23
23
## Examples
24
24
Original file line number Diff line number Diff line change @@ -49,8 +49,11 @@ func AddRules(r lint.RuleRegistry) error {
49
49
}
50
50
51
51
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 )
52
55
return m .FindFieldByName ("name" ) != nil && ! strings .HasSuffix (m .GetName (), "Request" ) &&
53
- ! strings .HasSuffix (m .GetName (), "Response" )
56
+ ! strings .HasSuffix (m .GetName (), "Response" ) && ! nested
54
57
}
55
58
56
59
func hasResourceAnnotation (m * desc.MessageDescriptor ) bool {
Original file line number Diff line number Diff line change @@ -38,6 +38,20 @@ func TestResourceAnnotation(t *testing.T) {
38
38
}
39
39
})
40
40
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
+
41
55
// The rule should fail if the option is absent on a resource message,
42
56
// but pass on messages that are not resource messages.
43
57
for _ , test := range []struct {
You can’t perform that action at this time.
0 commit comments