danielgtaylor / huma

Huma REST/HTTP API Framework for Golang with OpenAPI 3.1
https://huma.rocks/
MIT License
1.71k stars 134 forks source link

Support multiple query parameters with the same name #452

Closed csmarchbanks closed 1 week ago

csmarchbanks commented 2 months ago

I am looking at migrating to use huma in a project but one thing that is blocking me is support for requests such as /path?foo=bar&foo=baz. This is especially useful when a value may include a comma which would cause the current comma based approach to fail.

I am happy to implement this feature, and made a start, however I am not sure the best way to get the multiple values out of huma.Context. I could add a new method such as QueryMultiple(name string) []string to the interface, but would that be a breaking change for anyone implementing their own adapter? Happy to hear any suggestions. Worst case I suppose we could cast to a second interface and throw a runtime error?

danielgtaylor commented 1 month ago

@csmarchbanks I'm open to trying to support this. I didn't originally implement it because of performance reasons, i.e. you can avoid all dynamic allocations in the request handling hot path by only having one value per query param name. I think returning []string will require allocations, but I'm open to ideas. The benchmarks in adapters/humachi can be helpful to see how your changes impact memory and garbage collection.

Edit: another option is to support escaping of commas.

turtledev1 commented 1 month ago

I'm trying to support the same thing. Currently it is supported by OpenAPI with the parameters "explode: true" and "style: form" (https://spec.openapis.org/oas/v3.1.0#parameter-object). I tried to add these annotations but the resulting json always have the explode property to false, and was confused until I stumbled on this thread.

EDIT: Found the exact line https://github.com/danielgtaylor/huma/blob/158b0769e566a19d848af5820d30727ecfb54b6d/huma.go#L140

ARolek commented 1 month ago

I'm also encountering this block. I'm happy to help with code review / testing / etc.

csmarchbanks commented 1 week ago

I finally had some time to implement this and have a PR up at https://github.com/danielgtaylor/huma/pull/498, let me know if you run into any issues with it!