Skip to content

fix: Optional[UploadFile] should be None when no file submitted in multipart form#4659

Merged
provinzkraut merged 4 commits intolitestar-org:mainfrom
infraAnchor:fix/optional-upload-file-multipart-empty-string
Apr 2, 2026
Merged

fix: Optional[UploadFile] should be None when no file submitted in multipart form#4659
provinzkraut merged 4 commits intolitestar-org:mainfrom
infraAnchor:fix/optional-upload-file-multipart-empty-string

Conversation

@infraAnchor
Copy link
Copy Markdown
Contributor

@infraAnchor infraAnchor commented Apr 1, 2026

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 a
    400 validation error for Optional[UploadFile] fields.

  • Fix: in _extract_multipart (litestar/_kwargs/extractors.py), inside the
    type-hints loop, detect when a field value is "" and the annotated type is
    Optional[UploadFile] (or a subclass). Replace the empty string with None
    before it reaches the conversion layer — an empty file input means "no file
    selected", which matches the semantics of an Optional upload 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

…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
@infraAnchor infraAnchor requested review from a team as code owners April 1, 2026 14:41
@github-actions github-actions bot added area/kwargs area/private-api This PR involves changes to the privatized API size: small type/bug pr/external Triage Required 🏥 This requires triage labels Apr 1, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 1, 2026

Codecov Report

❌ Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 67.33%. Comparing base (1d58647) to head (c0891f4).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
litestar/_kwargs/extractors.py 80.00% 0 Missing and 1 partial ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Member

@provinzkraut provinzkraut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@provinzkraut provinzkraut enabled auto-merge (squash) April 2, 2026 15:17
@provinzkraut provinzkraut merged commit 80dace4 into litestar-org:main Apr 2, 2026
24 checks passed
@brakhane
Copy link
Copy Markdown
Contributor

brakhane commented Apr 2, 2026

detect when a field value is "" and the annotated type is Optional[UploadFile] (or a subclass).

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Optional[UploadFile] with multipart/form-data broken since 2.18.0 Bug: Empty strings in multipart/form-data requests are converted to None

3 participants