Skip to content

Commit 5961f0a

Browse files
authored
fix: run nested resolvers in fixed arrays (#1076)
1 parent 85aad8f commit 5961f0a

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

‎huma.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ func (r *findResult[T]) every(current reflect.Value, path []int, v T, f func(ref
397397
switch current.Kind() {
398398
case reflect.Struct:
399399
r.every(current.Field(path[0]), path[1:], v, f)
400-
case reflect.Slice:
400+
case reflect.Slice, reflect.Array:
401401
for j := 0; j < current.Len(); j++ {
402402
r.every(current.Index(j), path, v, f)
403403
}
@@ -432,7 +432,7 @@ func jsonName(field reflect.StructField) string {
432432
// PathBuffer, and applying a function to leaf nodes.
433433
func (r *findResult[T]) everyPB(current reflect.Value, path []int, pb *PathBuffer, v T, f func(reflect.Value, T)) {
434434
switch reflect.Indirect(current).Kind() {
435-
case reflect.Slice, reflect.Map:
435+
case reflect.Slice, reflect.Array, reflect.Map:
436436
// Ignore these. We only care about the leaf nodes.
437437
default:
438438
if len(path) == 0 {
@@ -480,7 +480,7 @@ func (r *findResult[T]) everyPB(current reflect.Value, path []int, pb *PathBuffe
480480
for i := 0; i < pops; i++ {
481481
pb.Pop()
482482
}
483-
case reflect.Slice:
483+
case reflect.Slice, reflect.Array:
484484
for j := 0; j < current.Len(); j++ {
485485
pb.PushIndex(j)
486486
r.everyPB(current.Index(j), path, pb, v, f)
@@ -566,7 +566,7 @@ func _findInType[T comparable](t reflect.Type, path []int, result *findResult[T]
566566
delete(visited, t)
567567
}
568568
}
569-
case reflect.Slice:
569+
case reflect.Slice, reflect.Array:
570570
_findInType[T](t.Elem(), path, result, onType, onField, recurseFields, visited, ignore...)
571571
case reflect.Map:
572572
_findInType[T](t.Elem(), path, result, onType, onField, recurseFields, visited, ignore...)

‎huma_test.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4041,6 +4041,45 @@ func TestNestedResolverWithPath(t *testing.T) {
40414041
assert.Contains(t, w.Body.String(), `"location":"body.field1.foo[0].field2"`)
40424042
}
40434043

4044+
type FixedArrayResolverItem struct {
4045+
Value string `json:"value,omitempty" default:"default"`
4046+
Resolved bool `json:"-"`
4047+
Path string `json:"-"`
4048+
}
4049+
4050+
func (i *FixedArrayResolverItem) Resolve(_ huma.Context, prefix *huma.PathBuffer) []error {
4051+
i.Resolved = true
4052+
i.Path = prefix.String()
4053+
return nil
4054+
}
4055+
4056+
func TestResolverInFixedArray(t *testing.T) {
4057+
r, app := humatest.New(t, huma.DefaultConfig("Test API", "1.0.0"))
4058+
var got FixedArrayResolverItem
4059+
huma.Register(app, huma.Operation{
4060+
OperationID: "test",
4061+
Method: http.MethodPost,
4062+
Path: "/test",
4063+
}, func(ctx context.Context, input *struct {
4064+
Body struct {
4065+
Items [1]FixedArrayResolverItem `json:"items"`
4066+
}
4067+
}) (*struct{}, error) {
4068+
got = input.Body.Items[0]
4069+
return nil, nil
4070+
})
4071+
4072+
req := httptest.NewRequest(http.MethodPost, "/test", strings.NewReader(`{"items":[{}]}`))
4073+
req.Header.Set("Content-Type", "application/json")
4074+
w := httptest.NewRecorder()
4075+
r.ServeHTTP(w, req)
4076+
4077+
assert.Equal(t, http.StatusNoContent, w.Code, w.Body.String())
4078+
assert.Equal(t, "default", got.Value)
4079+
assert.True(t, got.Resolved)
4080+
assert.Equal(t, "body.items[0]", got.Path)
4081+
}
4082+
40444083
type ResolverCustomStatus struct{}
40454084

40464085
func (r *ResolverCustomStatus) Resolve(ctx huma.Context) []error {

0 commit comments

Comments
 (0)