Overview
Better Marking of Visited Types
When looking for params, headers, defaults, and resolvers the Huma type traversal code now tracks previously visited types more narrowly, continuing to detect recursive loops while allowing multiple adjacent fields to use the same type. Before this fix it would ignore some fields. For example, this now works propertly to run the resolver on both HomeAddress
and AwayAddress
:
type Address struct {
Line1 string `json:"line1" required:"true" minLength:"1" maxLength:"50"`
Line2 string `json:"line2,omitempty" required:"false" minLength:"0" maxLength:"50" default:""`
City string `json:"city" required:"true" minLength:"1" maxLength:"64"`
State string `json:"state" required:"true" minLength:"1" maxLength:"32"`
Zip string `json:"zip" required:"true" minLength:"1" maxLength:"16"`
CountryCode string `json:"countryCode" required:"false" minLength:"1" maxLength:"2" default:"US"`
}
func (a Address) Resolve(_ huma.Context, prefix *huma.PathBuffer) []error {
/* ... do stuff ... */
}
type TestRequestBody struct {
Name string `json:"name"`
Age int `json:"age"`
HomeAddress Address `json:"home" required:"true"`
AwayAddress Address `json:"away" required:"true"`
}
More Resilient Fast Q Value Selection
Several minor bugs have been fixed in the fast zero-allocation q
value parsing for client-based content negotiation via the Accept
header. Values like a;,
no longer cause a panic. Several new tests were added to ensure robustness.
No Longer Panic From Client Disconnect
When a client disconnects and a write to the socket results in an error, we now check if the context is context.Canceled
and ignore it. This should not result in a panic as that has a negative impact on metrics and perceived service health. An attempt is made to tell the huma.Context
that the response status code should be 499 Client Disconnected to help with metrics/logging middleware.
Others
- Fixed doc bug
- Minor fix when printing response bodies in tests if JSON indenting fails
- Refactored code for sending responses to be more consistent & re-used between the handlers and
WriteErr
and ensureNewErrorWithContext
is used everywhere. This should be a no-op for library users.
What's Changed
- fix: doc comments for TestAPI functions by @alexandear in #644
- fix: marking visited types in findInType by @danielgtaylor in #641
- fix: make content negotiation more resilient to bad input by @danielgtaylor in #645
- fix: dump body directly if json indent fails by @danielgtaylor in #643
- refactor: use WriteErr function for handling error returned by handler by @notjustmoney in #640
- fix: do not panic on client disconnect by @danielgtaylor in #650
New Contributors
- @notjustmoney made their first contribution in #640
Full Changelog: v2.25.0...v2.26.0