danielgtaylor/huma

Allow custom query param types.

Opened this issue · 0 comments

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.