Skip to content

Commit d79af9c

Browse files
fix(AIP 133-135): fix false positive in 133-135 (#1335)
* fix(AIP 133-135): fix false positive in 133-135 * chore: add new line at EoF Co-authored-by: Noah Dietz <[email protected]> --------- Co-authored-by: Noah Dietz <[email protected]>
1 parent f6108f0 commit d79af9c

12 files changed

+26
-17
lines changed

rules/aip0133/aip0133.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,6 @@ func AddRules(r lint.RuleRegistry) error {
4949
)
5050
}
5151

52-
// get resource message type name from method
53-
func getResourceMsgName(m *desc.MethodDescriptor) string {
54-
// Usually the response message will be the resource message, and its name will
55-
// be part of method name (make a double check here to avoid the issue when
56-
// method or output naming doesn't follow the right principles)
57-
if strings.Contains(m.GetName()[6:], m.GetOutputType().GetName()) {
58-
return m.GetOutputType().GetName()
59-
}
60-
return m.GetName()[6:]
61-
}
62-
6352
// get resource message type name from request message
6453
func getResourceMsgNameFromReq(m *desc.MessageDescriptor) string {
6554
// retrieve the string between the prefix "Create" and suffix "Request" from

rules/aip0133/http_body.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var httpBody = &lint.MethodRule{
2929
Name: lint.NewRuleName(133, "http-body"),
3030
OnlyIf: utils.IsCreateMethod,
3131
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
32-
resourceMsgName := getResourceMsgName(m)
32+
resourceMsgName := utils.GetResourceMessageName(m, "Create")
3333
resourceFieldName := strings.ToLower(resourceMsgName)
3434
for _, fieldDesc := range m.GetInputType().GetFields() {
3535
// when msgDesc is nil, the resource field in the request message is

rules/aip0133/method_signature.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var methodSignature = &lint.MethodRule{
3434

3535
// Determine what signature we want. The {resource}_id is desired
3636
// if and only if the field exists on the request.
37-
resourceField := strcase.SnakeCase(getResourceMsgName(m))
37+
resourceField := strcase.SnakeCase(utils.GetResourceMessageName(m, "Create"))
3838
want := []string{}
3939
if !hasNoParent(m.GetOutputType()) {
4040
want = append(want, "parent")

rules/aip0133/method_signature_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func TestMethodSignature(t *testing.T) {
3030
}{
3131
{"ValidNoID", "CreateBook", `option (google.api.method_signature) = "parent,book";`, "", testutils.Problems{}},
3232
{"ValidID", "CreateBook", `option (google.api.method_signature) = "parent,book,book_id";`, "string book_id = 3;", testutils.Problems{}},
33+
{"ValidOperation", "CreateUnitOperation", `option (google.api.method_signature) = "parent,unit_operation,unit_operation_id";`, "string unit_operation_id = 3;", testutils.Problems{}},
3334
{"MissingNoID", "CreateBook", "", "", testutils.Problems{{Message: `(google.api.method_signature) = "parent,book"`}}},
3435
{
3536
"MissingID",

rules/aip0133/response_lro.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var responseLRO = &lint.MethodRule{
2727
return utils.IsCreateMethod(m) && utils.IsDeclarativeFriendlyMethod(m)
2828
},
2929
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
30-
if m.GetOutputType().GetFullyQualifiedName() != "google.longrunning.Operation" {
30+
if !utils.IsOperation(m.GetOutputType()) {
3131
return []lint.Problem{{
3232
Message: "Declarative-friendly create methods should use an LRO.",
3333
Descriptor: m,

rules/aip0133/response_message_name.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var outputName = &lint.MethodRule{
2828
Name: lint.NewRuleName(133, "response-message-name"),
2929
OnlyIf: utils.IsCreateMethod,
3030
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
31-
want := getResourceMsgName(m)
31+
want := utils.GetResourceMessageName(m, "Create")
3232

3333
// If this is an LRO, then use the annotated response type instead of
3434
// the actual RPC return type.

rules/aip0133/response_message_name_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func TestOutputMessageName(t *testing.T) {
3131
}{
3232
{"ValidResource", "CreateBook", "Book", false, testutils.Problems{}},
3333
{"ValidLRO", "CreateBook", "Book", true, testutils.Problems{}},
34+
{"ResourceNameContainsOperation", "CreateUnitOperation", "UnitOperation", true, testutils.Problems{}},
3435
{"Invalid", "CreateBook", "CreateBookResponse", false, testutils.Problems{{Suggestion: "Book"}}},
3536
{"InvalidLRO", "CreateBook", "CreateBookResponse", true, testutils.Problems{{Suggestion: "Book"}}},
3637
{"Irrelevant", "BuildBook", "BuildBookResponse", false, testutils.Problems{}},

rules/aip0134/response_lro.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var responseLRO = &lint.MethodRule{
2727
return utils.IsUpdateMethod(m) && utils.IsDeclarativeFriendlyMethod(m)
2828
},
2929
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
30-
if m.GetOutputType().GetFullyQualifiedName() != "google.longrunning.Operation" {
30+
if !utils.IsOperation(m.GetOutputType()) {
3131
return []lint.Problem{{
3232
Message: "Declarative-friendly update methods should use an LRO.",
3333
Descriptor: m,

rules/aip0134/response_message_name_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func TestResponseMessageName(t *testing.T) {
3131
}{
3232
{"ValidResource", "UpdateBook", "Book", false, testutils.Problems{}},
3333
{"ValidLRO", "UpdateBook", "Book", true, testutils.Problems{}},
34+
{"ValidLROContainingOperation", "UpdateUnitOperation", "UnitOperation", true, testutils.Problems{}},
3435
{"Invalid", "UpdateBook", "UpdateBookResponse", false, testutils.Problems{{Suggestion: "Book"}}},
3536
{"InvalidLRO", "UpdateBook", "UpdateBookResponse", true, testutils.Problems{{Suggestion: "Book"}}},
3637
{"Irrelevant", "MutateBook", "MutateBookResponse", false, testutils.Problems{}},

rules/aip0135/response_lro.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var responseLRO = &lint.MethodRule{
2727
return utils.IsDeleteMethod(m) && utils.IsDeclarativeFriendlyMethod(m)
2828
},
2929
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
30-
if m.GetOutputType().GetFullyQualifiedName() != "google.longrunning.Operation" {
30+
if !utils.IsOperation(m.GetOutputType()) {
3131
return []lint.Problem{{
3232
Message: "Declarative-friendly delete methods should use an LRO.",
3333
Descriptor: m,

0 commit comments

Comments
 (0)