Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(AIP-4232): support nested field of required field #1339

Merged
merged 1 commit into from
Feb 20, 2024
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
9 changes: 8 additions & 1 deletion rules/aip4232/required_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ var requiredFields = &lint.MethodRule{
}
}
for i, sig := range sigs {
sigset := stringset.New(sig...)
sigset := stringset.New(sig...).Map(func(s string) string {
if !strings.Contains(s, ".") {
return s
}
// If signature contains nested fields, get the root field
// to use in top-level required field check.
return strings.Split(s, ".")[0]
})
if !sigset.Contains(requiredFields...) {
problems = append(problems, lint.Problem{
Message: fmt.Sprintf("Method signature %q missing at least one of the required fields: %q", strings.Join(sig, ","), strings.Join(requiredFields, ",")),
Expand Down
8 changes: 7 additions & 1 deletion rules/aip4232/required_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func TestRequiredFields(t *testing.T) {
FieldBehavior string
problems testutils.Problems
}{
{"Valid", "name,paperback_only", "REQUIRED", nil},
{"Valid", "name,paperback_only,shelf", "REQUIRED", nil},
{"ValidNested", "name,paperback_only,shelf.name", "REQUIRED", nil},
{"ValidNotRequired", "paperback_only", "OPTIONAL", nil},
{"Invalid", "paperback_only", "REQUIRED", testutils.Problems{{Message: "missing at least one"}}},
}
Expand All @@ -46,8 +47,13 @@ func TestRequiredFields(t *testing.T) {
string name = 1 [(google.api.field_behavior) = {{.FieldBehavior}}];

bool paperback_only = 2;

Shelf shelf = 3 [(google.api.field_behavior) = {{.FieldBehavior}}];
}
message ArchiveBookResponse {}
message Shelf {
string name = 1;
}
`, test)
method := f.GetServices()[0].GetMethods()[0]
if diff := test.problems.SetDescriptor(method).Diff(requiredFields.Lint(f)); diff != "" {
Expand Down
Loading