danielgtaylor / huma

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

Allow custom query param types. #433

Open ross96D opened 4 months ago

ross96D commented 4 months ago

The idea is to have something like

type Nullable[T any] struct {
    Null  bool
    Value T
}

func (o Nullable[T]) Schema(r huma.Registry) *huma.Schema {
    return r.Schema(reflect.TypeOf(o.Value), true, "")
}

type CustomQueryParamInput struct {
    Query Nullable[int] `query:"query"`
}

And allow the user to implement the field conversion.

Using the SchemaProvider interface it can be set the query param type, but the current implementation does not provide any interface for the conversion part.

The fallback is the TextUnmarshaler interface, but this interface is not enough because then will fail on the validation phase (always set the value as a string).

So my proposal is to add an interface that allow the user to implement this conversion.. something like

type InputConverter interface {
    HumaInputConvert([]byte) (any, error)
}

the relevant code is on the huma.go file, starting at line 825.