Closed lsdch closed 3 months ago
Attention: Patch coverage is 93.93939%
with 12 lines
in your changes are missing coverage. Please review.
Project coverage is 92.80%. Comparing base (
e1b7179
) to head (f1a58f1
).
Files | Patch % | Lines |
---|---|---|
formdata.go | 94.87% | 4 Missing and 4 partials :warning: |
huma.go | 90.47% | 2 Missing and 2 partials :warning: |
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
@lsdch this looks amazing! I will dig in and give it a thorough review soon, just wanted to say thanks for all the work on this and I like the idea of better file handling & documentation.
The changes introduce extensive support for handling multipart form data in HTTP requests within the huma
package. This includes defining structures and methods for reading, validating, and decoding form files, as well as adding comprehensive test cases to ensure robust functionality.
Files | Change Summary |
---|---|
formdata.go |
Added structures and methods for handling multipart form data, including file reading, validation, and decoding. |
huma.go |
Renamed rawBodyMultipart to rawBodyDecodedMultipart in the Register function. |
huma_test.go |
Added multiple test cases for multipart file handling, including validation, optional/required files, and content type checks. |
π° In bytes and streams, we trust,
To handle forms, precise and just.
With MIME types checked and files read right,
Our code now gleams, a coder's delight.
So cheers to tests, both new and bold,
Our multipart forms, a tale well told.
π
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?
@danielgtaylor hey, finally took some time to write tests for this feature. Coverage is almost 100%, except for 3 error checks on file I/O that are hard to trigger.
API has changed a little, I introduced small convenience features to help working with optional files and content-type. Also changed tag names to be more consistent with the naming conventions in Huma -- I don't mind changing them again to whatever you like.
I expect that some of the code could be better optimized, especially the parts using reflect
, which I do not know in depth yet.
New usage example :
type FileData struct {
// Now using huma.FormFile instead of multipart.File
// FormFile is a simple wrapper around multipart.File which provides some convenience features
SomeFile huma.FormFile `form:"some_file" contentType:"image/png" required:"true"`
SomeImage huma.FormFile `form:"some_image" contentType:"image/*"`
SeveralFiles []huma.FormFile `form:"several_files" contentType:"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)
if fileData.SomeImage.IsSet { // We can now check the presence of optional files
fmt.Printf("Content type: %s", fileData.SomeImage.ContentType) // Actual content type (declared in the form or detected as fallback)
}
}
huma.Register(api,
huma.Operation{
Path: "/handle-files",
Method: http.MethodPost,
OperationID: "Handle files",
}, FileHandler)
@lsdch awesome work! I will dig in and do a thorough review ASAP! Thank you.
This PR aims to improve the handling of
multipart/form-data
requests by:RawBody
ismultipart.FormData
. The generated schema is more of an example and does not reflect the actual form data structure, which should be specified by the user.MultipartFormFiles
, which can be used to typeRawBody
in request inputs and provides logic for decoding and validating files from amultipart/form-data
request asmultipart.File
values, as well as generating the associated schema in the spec. Although this could be extended to support data types other than files, it would require significant refactoring to reuse some of the logic from body/query/path/header parameters decoding, and I was not confident enough to engage into it.There is currently no automated tests for this PR, I would like to first get some feedback before spending more time on this (and still have to get familiar with
humatest
).Example usage:
When using
MultipartFormFiles[T]
, only the fields inT
that implementmultipart.File
or[]multipart.File
are considered when generating the spec or resolving a request. The raw form data can be accessed atMultipartFormFiles.Form
which is amultipart.Form
.form-data
tag specifies the lookup key to retrieve the encoded file in themultipart.Form
. In its absence, the name of the field is used instead.content-type
specifies the acceptable mime types for a file. It is used to document the endpoint in the spec, and validate file format usinghttp.DetectContentType
required
makes the field required. In the case of[]multipart.File
, validation checks that at least one file was submitted.Generated operation spec :
Summary by CodeRabbit
New Features
Tests