fix: prevent panic when text value sent to FormFile field#932
fix: prevent panic when text value sent to FormFile field#932wolveix merged 4 commits intodanielgtaylor:mainfrom
Conversation
Skip FormFile and []FormFile fields in formValueParser to prevent panic when multipart form data contains text values for fields expecting file uploads. Previously, sending a text value to a FormFile field would cause parseInto() to panic with 'unsupported param type huma.FormFile'. Now it properly returns a validation error 'File required'. Partially fixes danielgtaylor#912
There was a problem hiding this comment.
Pull request overview
This PR fixes a panic that occurred when multipart form data contains text values (without filename) for fields expecting file uploads. The fix prevents parseInto() from attempting to parse FormFile types by skipping them in the formValueParser, allowing the proper file handling code to return a validation error instead.
- Skip FormFile and []FormFile fields in formValueParser to prevent panic
- Add comprehensive test case for text value sent to FormFile field
- Ensures proper "File required" validation error is returned
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| huma.go | Adds early return in formValueParser to skip FormFile and []FormFile fields, preventing panic in parseInto() |
| huma_test.go | Adds test case validating that text values sent to FormFile fields return proper validation error instead of panicking |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…rison This change improves the type checking for FormFile fields by using reflect.TypeOf() for direct type comparison instead of string comparison. This is more reliable and performant.
Revert unnecessary comment change
FormFiles in the body are now correctly required by default, so the "optional" test needed to be explicitly defined
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #932 +/- ##
==========================================
+ Coverage 92.53% 92.93% +0.40%
==========================================
Files 23 23
Lines 4472 4644 +172
==========================================
+ Hits 4138 4316 +178
+ Misses 275 271 -4
+ Partials 59 57 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Awesome, thanks so much! |
Skip FormFile and []FormFile fields in formValueParser to prevent panic when multipart form data contains text values for fields expecting file uploads.
Previously, sending a text value to a FormFile field would cause parseInto() to panic with 'unsupported param type huma.FormFile'. Now it properly returns a validation error 'File required'.
Partially fixes #912