From 22d015afc1067f8895a2603ae859d11d33f06a36 Mon Sep 17 00:00:00 2001 From: Andrei Scripniciuc <46186670+andrei-scripniciuc@users.noreply.github.com> Date: Fri, 12 Apr 2024 10:27:18 -0700 Subject: [PATCH] feat(AIP-133): ignore create methods with invalid LRO response types (#1366) * feat(AIP-133): ignore create methods with invalid LRO response types * chore: clean up return type resolution logic --- rules/aip0133/request_required_fields.go | 2 +- rules/internal/utils/method.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/rules/aip0133/request_required_fields.go b/rules/aip0133/request_required_fields.go index 6ba022947..04111a3dd 100644 --- a/rules/aip0133/request_required_fields.go +++ b/rules/aip0133/request_required_fields.go @@ -28,7 +28,7 @@ import ( // The create request message should not have unrecognized fields. var requestRequiredFields = &lint.MethodRule{ Name: lint.NewRuleName(133, "request-required-fields"), - OnlyIf: utils.IsCreateMethod, + OnlyIf: utils.IsCreateMethodWithResolvedReturnType, LintMethod: func(m *desc.MethodDescriptor) []lint.Problem { ot := utils.GetResponseType(m) r := utils.GetResource(ot) diff --git a/rules/internal/utils/method.go b/rules/internal/utils/method.go index 3de11d923..69f2ad62d 100644 --- a/rules/internal/utils/method.go +++ b/rules/internal/utils/method.go @@ -36,6 +36,18 @@ func IsCreateMethod(m *desc.MethodDescriptor) bool { return createMethodRegexp.MatchString(m.GetName()) } +// IsCreateMethod returns true if this is a AIP-133 Create method with +// a non-nil response type. This method should be used for filtering in linter +// rules which access the response type of the method, to avoid crashing due +// to dereferencing a nil pointer to the response. +func IsCreateMethodWithResolvedReturnType(m *desc.MethodDescriptor) bool { + if !IsCreateMethod(m) { + return false + } + + return GetResponseType(m) != nil +} + // IsGetMethod returns true if this is a AIP-131 Get method. func IsGetMethod(m *desc.MethodDescriptor) bool { methodName := m.GetName()