Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion rules/aip0203/field_behavior_required.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)...)
}
}
Expand Down
19 changes: 17 additions & 2 deletions rules/aip0203/field_behavior_required_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
}
}

Expand Down Expand Up @@ -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;
}
`

Expand Down