diff --git a/rules/aip0203/field_behavior_required.go b/rules/aip0203/field_behavior_required.go index 2f5f0a0fd..947bb853d 100644 --- a/rules/aip0203/field_behavior_required.go +++ b/rules/aip0203/field_behavior_required.go @@ -49,6 +49,12 @@ var fieldBehaviorRequired = &lint.MethodRule{ func problems(m *desc.MessageDescriptor, pkg string, visited map[desc.Descriptor]bool) []lint.Problem { var ps []lint.Problem + // Ensure the input type, or recursively visited message, is part of the + // same package before linting. + if m.GetFile().GetPackage() != pkg { + return nil + } + for _, f := range m.GetFields() { // ignore the field if it was already visited if ok := visited[f]; ok { @@ -68,7 +74,7 @@ func problems(m *desc.MessageDescriptor, pkg string, visited map[desc.Descriptor } } - if mt := f.GetMessageType(); mt != nil && !mt.IsMapEntry() && mt.GetFile().GetPackage() == pkg { + if mt := f.GetMessageType(); mt != nil && !mt.IsMapEntry() { ps = append(ps, problems(mt, pkg, visited)...) } } diff --git a/rules/aip0203/field_behavior_required_test.go b/rules/aip0203/field_behavior_required_test.go index fd934f847..3c442a0ab 100644 --- a/rules/aip0203/field_behavior_required_test.go +++ b/rules/aip0203/field_behavior_required_test.go @@ -253,26 +253,37 @@ func TestFieldBehaviorRequired_NestedMessages_MultipleFile(t *testing.T) { name string MessageType string MessageFieldName string + RequestMessage string problems testutils.Problems }{ { "ValidAnnotatedAndChildAnnotated", "Annotated", "annotated", + "UpdateBookRequest", nil, }, { "ValidAnnotatedAndChildInOtherPackageUnannotated", "unannotated.NonAnnotated", "non_annotated", + "UpdateBookRequest", nil, }, { "InvalidChildNotAnnotated", "NonAnnotated", "non_annotated", + "UpdateBookRequest", testutils.Problems{{Message: "must be set"}}, }, + { + "SkipRequestInOtherPackageUnannotated", + "Annotated", // set this so that the template compiles + "annotated", // set this so that the template compiles + "unannotated.NonAnnotated", // unannotated message as request + nil, + }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { @@ -284,7 +295,7 @@ func TestFieldBehaviorRequired_NestedMessages_MultipleFile(t *testing.T) { import "unannotated.proto"; service Library { - rpc UpdateBook(UpdateBookRequest) returns (UpdateBookResponse) { + rpc UpdateBook({{.RequestMessage}}) returns (UpdateBookResponse) { } } @@ -317,7 +328,11 @@ func TestFieldBehaviorRequired_NestedMessages_MultipleFile(t *testing.T) { package apilinter.test.unannotated; message NonAnnotated { - string nested = 1; + OtherNonAnnotated nested = 1; + } + + message OtherNonAnnotated { + string foo = 1; } `