-
-
Notifications
You must be signed in to change notification settings - Fork 232
feat: custom query parameter types #722
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #722 +/- ##
==========================================
- Coverage 92.79% 92.78% -0.02%
==========================================
Files 22 22
Lines 5027 5061 +34
==========================================
+ Hits 4665 4696 +31
- Misses 312 314 +2
- Partials 50 51 +1 ☔ View full report in Codecov by Sentry. |
danielgtaylor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lsdch nice idea! I think this looks good, just left one comment for how we can precompute the underlying type so we don't modify the param definition in an unsafe way.
huma.go
Outdated
| receiver = f.Addr().Interface().(ParamWrapper).Receiver() | ||
| // Feels like a hack, but we need to override the type of the wrapper from the cache | ||
| // There might a be a better way | ||
| p.Type = receiver.Type() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect we want to find the right type in findParams at https://github.com/danielgtaylor/huma/blob/main/huma.go#L102 by calling Receiver() once on a dummy value, then we don't have to do this check/swap on each request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (1)
huma.go:699
- [nitpick] The variable name 'receiver' is ambiguous. It should be renamed to 'wrappedValue' or 'paramValue' for better clarity.
var receiver = f
4bbd9f3 to
297fc17
Compare
|
Thanks for the feedback @danielgtaylor ! And for the pointer to fix the type precomputation, feels way better now 🙂 edit: not sure how we get a test coverage regression here 🤔 |
Hi!
I need a way to handle null values in query parameters (and apparently I am not the only one #438 🙂 ) so here is a PR that gives the possibility to wrap query parameter fields with custom types.
It relies on implementing the
ParamWrapperinterface, which exposes as areflect.Valuethe internal field that we want to use when parsing request parameters.ParamReactor(bad name, I know 🙈 ) is another interface that can be implemented to specify a callback when a request parameter was parsed into our custom type.Example usage:
I believe it resolves validation issues we had with #438, but there might be other caveats I am not aware of. Also I am not 100% convinced with the proposed API, what do you think ?