这是indexloc提供的服务,不要输入任何密码
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
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)
}
}
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