这是indexloc提供的服务,不要输入任何密码
Skip to content

Allow using pointers for non param fields in input structs #565

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

Merged
merged 1 commit into from
Sep 13, 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
18 changes: 9 additions & 9 deletions huma.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,6 @@ func findParams(registry Registry, op *Operation, t reflect.Type) *findResult[*p
return nil
}

if f.Type.Kind() == reflect.Pointer {
// TODO: support pointers? The problem is that when we dynamically
// create an instance of the input struct the `params.Every(...)`
// call cannot set them as the value is `reflect.Invalid` unless
// dynamically allocated, but we don't know when to allocate until
// after the `Every` callback has run. Doable, but a bigger change.
panic("pointers are not supported for path/query/header parameters")
}

pfi := &paramFieldInfo{
Type: f.Type,
}
Expand Down Expand Up @@ -146,6 +137,15 @@ func findParams(registry Registry, op *Operation, t reflect.Type) *findResult[*p
return nil
}

if f.Type.Kind() == reflect.Pointer {
// TODO: support pointers? The problem is that when we dynamically
// create an instance of the input struct the `params.Every(...)`
// call cannot set them as the value is `reflect.Invalid` unless
// dynamically allocated, but we don't know when to allocate until
// after the `Every` callback has run. Doable, but a bigger change.
panic("pointers are not supported for path/query/header parameters")
}

pfi.Schema = SchemaFromField(registry, f, "")

var example any
Expand Down
32 changes: 32 additions & 0 deletions huma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2059,6 +2059,38 @@ func TestResolverCompositionCalledOnce(t *testing.T) {
assert.Equal(t, http.StatusNoContent, w.Code, w.Body.String())
}

type ResolverWithPointer struct {
Ptr *string
}

func (r *ResolverWithPointer) Resolve(ctx huma.Context) []error {
r.Ptr = new(string)
*r.Ptr = "String"
return nil
}

func TestResolverWithPointer(t *testing.T) {
// Allow using pointers in input structs if they are not path/query/header/cookie parameters
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 {
ResolverWithPointer
}) (*struct{}, error) {
// Exactly one call should have been made to the resolver.
assert.Equal(t, "String", *input.Ptr)
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