这是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
15 changes: 11 additions & 4 deletions huma.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,8 @@ func findParams(registry Registry, op *Operation, t reflect.Type) *findResult[*p

func findResolvers(resolverType, t reflect.Type) *findResult[bool] {
return findInType(t, func(t reflect.Type, path []int) bool {
if reflect.PtrTo(t).Implements(resolverType) {
return true
}
if reflect.PtrTo(t).Implements(resolverWithPathType) {
tp := reflect.PtrTo(t)
if tp.Implements(resolverType) || tp.Implements(resolverWithPathType) {
return true
}
return false
Expand Down Expand Up @@ -362,9 +360,15 @@ func _findInType[T comparable](t reflect.Type, path []int, result *findResult[T]
t = deref(t)
zero := reflect.Zero(reflect.TypeOf((*T)(nil)).Elem()).Interface()

ignoreAnonymous := false
if onType != nil {
if v := onType(t, path); v != zero {
result.Paths = append(result.Paths, findResultPath[T]{path, v})

// Found what we were looking for in the type, no need to go deeper.
// We do still want to potentially process each non-anonymous field,
// so only skip anonymous ones.
ignoreAnonymous = true
}
}

Expand All @@ -378,6 +382,9 @@ func _findInType[T comparable](t reflect.Type, path []int, result *findResult[T]
if slicesContains(ignore, f.Name) {
continue
}
if ignoreAnonymous && f.Anonymous {
continue
}
fi := append([]int{}, path...)
fi = append(fi, i)
if onField != nil {
Expand Down
30 changes: 30 additions & 0 deletions huma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,36 @@ func TestResolverCustomStatus(t *testing.T) {
assert.Contains(t, w.Body.String(), "nope")
}

type ResolverCalls struct {
Calls int
}

func (r *ResolverCalls) Resolve(ctx huma.Context) []error {
r.Calls++
return nil
}

func TestResolverCompositionCalledOnce(t *testing.T) {
r, app := humatest.New(t, huma.DefaultConfig("Test API", "1.0.0"))
huma.Register(app, huma.Operation{
OperationID: "test",
Method: http.MethodPut,
Path: "/test",
}, func(ctx context.Context, input *struct {
ResolverCalls
}) (*struct{}, error) {
// Exactly one call should have been made to the resolver.
assert.Equal(t, 1, input.Calls)
return nil, nil
})

req, _ := http.NewRequest(http.MethodPut, "/test", strings.NewReader(`{}`))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusNoContent, w.Code, w.Body.String())
}

func TestParamPointerPanics(t *testing.T) {
// For now we don't support these, so we panic rather than have subtle
// bugs that are hard to track down.
Expand Down