fix: Optional[UploadFile] should be None when no file submitted in multipart form#4659
Merged
provinzkraut merged 4 commits intolitestar-org:mainfrom Apr 2, 2026
Conversation
…n no file submitted
When a browser submits a multipart form without selecting a file, the field
is sent as an empty string (""). Since 2.18.0 (PR litestar-org#4271 which fixed litestar-org#4204),
this empty string is no longer converted to None, causing validation to fail
for Optional[UploadFile] fields with a 400 error.
Fix: in _extract_multipart, before the type-hints loop, detect when a field
value is "" and the expected type is Optional[UploadFile] (or a subclass),
and replace the empty string with None before it reaches the conversion layer.
Fixes litestar-org#4647
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4659 +/- ##
=======================================
Coverage 67.32% 67.33%
=======================================
Files 292 292
Lines 14933 14938 +5
Branches 1673 1675 +2
=======================================
+ Hits 10054 10058 +4
Misses 4743 4743
- Partials 136 137 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
Wouldn't this result in a weird exception if the field is annotated as (required) UploadFile and no file is uploaded nonetheless? I think getting a None in those cases is better than an empty string for consistency reasons |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When a browser submits a multipart form without selecting a file, the file field
arrives as an empty string (
""). Since 2.18.0 (PR fix: Preserve empty strings in multipart/form-data requests (#4204) #4271 which fixed Bug: Empty strings in multipart/form-data requests are converted to None #4204),this empty string is preserved instead of being converted to
None, causing a400validation error forOptional[UploadFile]fields.Fix: in
_extract_multipart(litestar/_kwargs/extractors.py), inside thetype-hints loop, detect when a field value is
""and the annotated type isOptional[UploadFile](or a subclass). Replace the empty string withNonebefore it reaches the conversion layer — an empty file input means "no file
selected", which matches the semantics of an
Optionalupload field.Regression test added in
tests/unit/test_kwargs/test_multipart_data.py.Closes
Closes #4647
📚 Documentation preview 📚: https://litestar-org.github.io/litestar-docs-preview/4659