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

nil pointer dereference attempting to override input Body contentType with application/x-www-form-urlencoded #697

Closed
ScottCUSA opened this issue Jan 2, 2025 · 0 comments

Comments

@ScottCUSA
Copy link

ScottCUSA commented Jan 2, 2025

Hello,

I have been trying to implement an endpoint which accepts and validates application/x-www-form-urlencoded using some of the examples in previous issues for reference. I have the endpoint working. However, the OpenAPI docs lists it accepts both application/json and application/x-www-form-urlencoded.

image

I'd like to remove application/json support if possible. To do this I tried adding a contentType overwrite tag to the the Body struct in the handler func's input struct.

func(ctx context.Context, input *struct {
	Body *Request `contentType:"application/x-www-form-urlencoded"`
}) (*Resp, error) {
	return &Resp{Body: input.Body}, nil
})

This resolves the docs reporting it accepts json but it causes the huma validator to panic from a nil pointer dereference when I submit a request:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x5dfc65]

goroutine 1 [running]:
github.com/danielgtaylor/huma/v2.Validate({0x6e3800, 0xc000026540}, 0x21?, 0xc00006c8e0, 0x1, {0x631ce0, 0xc000017d70}, 0xc000010420)
   /tmp/gopath2204094610/pkg/mod/github.com/danielgtaylor/huma/[email protected]/validate.go:362 +0xa5
github.com/danielgtaylor/huma/v2.Register[...].func1()
   /tmp/gopath2204094610/pkg/mod/github.com/danielgtaylor/huma/[email protected]/huma.go:1346 +0x1605
github.com/danielgtaylor/huma/v2/adapters/humago.(*goAdapter).Handle.func1({0x6e3368, 0xc000026780}, 0xc000143400)
   /tmp/gopath2204094610/pkg/mod/github.com/danielgtaylor/huma/[email protected]/adapters/humago/humago.go:145 +0xa2
net/http.HandlerFunc.ServeHTTP(0xc000196000?, {0x6e3368?, 0xc000026780?}, 0xc000173a68?)
   /usr/local/go-faketime/src/net/http/server.go:2220 +0x29
net/http.(*ServeMux).ServeHTTP(0x654900?, {0x6e3368, 0xc000026780}, 0xc000143400)
   /usr/local/go-faketime/src/net/http/server.go:2747 +0x1ca
main.main()
   /tmp/sandbox1666713405/prog.go:84 +0x6a6

Is there a better way to do what I need?

Example code:
https://go.dev/play/p/ul4rPk-eh6r

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant