Skip to content

Commit e9306c6

Browse files
authored
fix(AIP-133): skip method sig for non-standard create (googleapis#1521)
When what looks like a Standard Create in fact isn't one because it doesn't actually operate on something annotated as a resource, skip the rule. Also, if after all of the munching of descriptors, there is nothing to suggest for a signature, just exit early, we don't want to suggest an empty signature (which this currently is capable of doing) Internal bug http://b/438826214
1 parent 4971f76 commit e9306c6

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

rules/aip0133/method_signature.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ import (
2727
)
2828

2929
var methodSignature = &lint.MethodRule{
30-
Name: lint.NewRuleName(133, "method-signature"),
31-
OnlyIf: utils.IsCreateMethod,
30+
Name: lint.NewRuleName(133, "method-signature"),
31+
OnlyIf: func(m *desc.MethodDescriptor) bool {
32+
return utils.IsCreateMethod(m) && utils.IsResource(utils.GetResponseType(m))
33+
},
3234
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
3335
signatures := utils.GetMethodSignatures(m)
3436

@@ -44,12 +46,19 @@ var methodSignature = &lint.MethodRule{
4446
}
4547
}
4648
// The {resource}_id is desired if and only if the field exists on the
47-
// request.
49+
// request and the request targets a resource.
4850
expectedResourceIDField := strcase.SnakeCase(utils.GetResourceMessageName(m, "Create"))
4951
if idField := expectedResourceIDField + "_id"; m.GetInputType().FindFieldByName(idField) != nil {
5052
want = append(want, idField)
5153
}
5254

55+
// The Standard Create is not standard and has nothing to suggest.
56+
// There are likely other rules warning about the non-standard nature
57+
// so just silently move on.
58+
if len(want) == 0 {
59+
return nil
60+
}
61+
5362
// Check if the signature is missing.
5463
if len(signatures) == 0 {
5564
return []lint.Problem{{

rules/aip0133/method_signature_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,27 @@ func TestMethodSignature(t *testing.T) {
115115
t.Error(diff)
116116
}
117117
})
118+
119+
// Ensure that this isn't producing a wonky finding if the "standard create"
120+
// doesn't actually interact with a resource. Other rules would capture that
121+
// errant aspect.
122+
t.Run("SkipNonResource", func(t *testing.T) {
123+
file := testutils.ParseProto3String(t, `
124+
import "google/api/client.proto";
125+
service Library {
126+
rpc CreateBook(CreateBookRequest) returns (Book) {}
127+
}
128+
message CreateBookRequest {
129+
Book book = 1;
130+
string book_id = 2;
131+
}
132+
message Book {}
133+
`)
134+
if diff := (testutils.Problems{}).Diff(methodSignature.Lint(file)); diff != "" {
135+
t.Error(diff)
136+
}
137+
})
138+
118139
// Add a separate test for the LRO case rather than introducing yet
119140
// another knob on the above test.
120141
t.Run("Longrunning", func(t *testing.T) {

0 commit comments

Comments
 (0)