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

Allow custom query param types. #433

Open
ross96D opened this issue May 9, 2024 · 0 comments · May be fixed by #438
Open

Allow custom query param types. #433

ross96D opened this issue May 9, 2024 · 0 comments · May be fixed by #438
Labels
enhancement New feature or request

Comments

@ross96D
Copy link
Contributor

ross96D commented May 9, 2024

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.

ross96D added a commit to ross96D/huma that referenced this issue May 11, 2024
@ross96D ross96D linked a pull request May 13, 2024 that will close this issue
@danielgtaylor danielgtaylor added the enhancement New feature or request label May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants