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: marking visited types in findInType #641

Merged
merged 1 commit into from
Nov 7, 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
3 changes: 2 additions & 1 deletion huma.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ func _findInType[T comparable](t reflect.Type, path []int, result *findResult[T]
if _, ok := visited[t]; ok {
return
}
visited[t] = struct{}{}
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if !f.IsExported() {
Expand All @@ -425,7 +424,9 @@ func _findInType[T comparable](t reflect.Type, path []int, result *findResult[T]
// Always process embedded structs and named fields which are not
// structs. If `recurseFields` is true then we also process named
// struct fields recursively.
visited[t] = struct{}{}
_findInType(f.Type, fi, result, onType, onField, recurseFields, visited, ignore...)
delete(visited, t)
danielgtaylor marked this conversation as resolved.
Show resolved Hide resolved
}
}
case reflect.Slice:
Expand Down
11 changes: 11 additions & 0 deletions huma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ type BodyContainer struct {

type CustomStringParam string

type StructWithDefaultField struct {
Field string `json:"field" default:"default"`
}

func TestFeatures(t *testing.T) {
for _, feature := range []struct {
Name string
Expand Down Expand Up @@ -628,6 +632,11 @@ func TestFeatures(t *testing.T) {
ID int `json:"id"`
Verified bool `json:"verified,omitempty" default:"true"`
} `json:"items,omitempty"`
// Test defaults for fields in the same linked struct. Even though
// we have seen the struct before we still need to set the default
// since it's a new/different field.
S1 StructWithDefaultField `json:"s1,omitempty"`
S2 StructWithDefaultField `json:"s2,omitempty"`
}
}) (*struct{}, error) {
assert.Equal(t, "Huma", input.Body.Name)
Expand All @@ -636,6 +645,8 @@ func TestFeatures(t *testing.T) {
assert.Equal(t, []int{1, 2, 3}, input.Body.Numbers)
assert.Equal(t, 1, input.Body.Items[0].ID)
assert.True(t, input.Body.Items[0].Verified)
assert.Equal(t, "default", input.Body.S1.Field)
assert.Equal(t, "default", input.Body.S2.Field)
return nil, nil
})
},
Expand Down
Loading