v2.18.0
Overview
Better Multipart Files Uploads
You can now use huma.MultipartFormFiles[YourType]
as the request's RawBody
in order to handle multipart file uploads. Files can be limited to specific content types and required or optional. Example:
type FileData struct {
// This is an example, any number of `multipart.File` fields can be defined.
// Nested structs are not supported.
SomeFile multipart.File `form-data:"some_file" content-type:"image/png" required:"true"`
SeveralFiles []multipart.File `form-data:"several_files" content-type:"image/png,image/jpeg" required:"true"`
}
type FileHandlerInput struct {
RawBody huma.MultipartFormFiles[FileData]
}
func FileHandler(ctx context.Context, input *FileHandlerInput) (*struct{}, error) {
fileData := input.RawBody.Data()
DoSomeThingWith(fileData.SomeFile)
OrSomethingElseWith(fileData.SeveralFiles)
}
huma.Register(api,
huma.Operation{
Path: "/handle-files",
Method: http.MethodPost,
OperationID: "Handle files",
}, FileHandler)
Schema Transformers
It's now possible to have types implement a schema transformer, which lets them modify the generated schema for the type. This option lives in between using the generated types and providing your own schema, and makes it a bit easier to modify the generated schema by not needing you to call into the registry manually. This is the interface:
type SchemaTransformer interface {
TransformSchema(r Registry, s *Schema) *Schema
}
Simple example:
type MyInput struct {
Field string `json:"field"`
}
func (i *MyInput) TransformSchema(r huma.Registry, s *huma.Schema) *huma.Schema {
s.Description = "I am an override"
return s
}
What's Changed
- Fix typo in SSE doc by @op-tmplt in #448
- Feature: handling files from
multipart/form-data
request by @lsdch in #415 - feat: allow modifying generated schema by implementing SchemaTransformer interface by @lsdch in #456
- fix: type schema description not work by @fourcels in #462
New Contributors
Full Changelog: v2.17.0...v2.18.0