Skip to content

Conversation

@thomasleveil
Copy link
Contributor

This PR updates the fix (6185a92) from @jsg921019 so that tests pass with the latest FastAPI code (0.110.0).
It also adds a test to cover the case with multiple files, and another to ensure the order of parameters in an endpoint definition does not affect the way FastAPI handles the request.

Problem
When declaring an endpoint which accepts a File but also a Form parameter, the File parameter must be declared first or the endpoint would respond with error 422.

Context

@thomasleveil thomasleveil force-pushed the fix-9116-form-and-file-order branch from 1763ed4 to dcd2e69 Compare February 25, 2024 15:11
YuriiMotov

This comment was marked as outdated.

@thomasleveil thomasleveil force-pushed the fix-9116-form-and-file-order branch from 7e97f62 to 0a1e865 Compare February 25, 2024 16:07
@thomasleveil
Copy link
Contributor Author

Thank you for the feed back. I pushed force with your suggested changes. Let me know if anything else can be improved.

@thomasleveil thomasleveil changed the title Fix #9116 form and file order 🐛 Fix #9116 form and file order Feb 25, 2024
@thomasleveil thomasleveil changed the title 🐛 Fix #9116 form and file order 🐛 Fix #9116 Form and File parameters order should not matter Feb 25, 2024
Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

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

The solution works.
Tests cover the issue. Tests fail before changes and pass after

@cfreksen
Copy link

cfreksen commented Nov 22, 2024

I am wondering if this PR has been forgotten?

I am not that familiar with the workflow in this repository, so excuse me if these things just take time or if this has low priority and there have been more important things to spend your time on.

However, I was affected by #9116 which led me to this PR, and since it has some approvals and it hasn't been touched in 9 months, I thought it would be ok to "bump" it.

@carl-sauter
Copy link

Hey what about this pr? At least there should be a better error message. Wasted my time only for finding this.

@YuriiMotov YuriiMotov self-requested a review June 21, 2025 17:05
@YuriiMotov

This comment was marked as resolved.

@thomasleveil thomasleveil force-pushed the fix-9116-form-and-file-order branch from 0a1e865 to 4b6e747 Compare June 22, 2025 17:21
@thomasleveil

This comment was marked as resolved.

@thomasleveil thomasleveil force-pushed the fix-9116-form-and-file-order branch from 577dce8 to 150fb07 Compare June 22, 2025 17:27
@YuriiMotov

This comment was marked as resolved.

@thomasleveil

This comment was marked as resolved.

@YuriiMotov

This comment was marked as resolved.

@thomasleveil thomasleveil force-pushed the fix-9116-form-and-file-order branch 2 times, most recently from 50b9f22 to e34c53c Compare June 23, 2025 12:32
…t of a multipart/form-data request body

The HTTP specification for multipart/form-data, defined in RFC 7578, allows both form data and uploaded files in a single request. It does not require files to be before or after form fields.

As such, no specific care is to be given to the first field.

References:
- [RFC 7578](https://datatracker.ietf.org/doc/html/rfc7578)
- [FastAPI issue 9116](fastapi#9116)

Close fastapi#9116
@thomasleveil thomasleveil force-pushed the fix-9116-form-and-file-order branch from bb07937 to 6e0abe9 Compare June 23, 2025 12:55
@thomasleveil
Copy link
Contributor Author

@YuriiMotov here you go.

Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

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

LGTM!

@YuriiMotov YuriiMotov changed the title 🐛 Fix #9116 Form and File parameters order should not matter 🐛 Fix validation error when File is declared after Form parameter Jun 23, 2025
Copy link
Member

@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

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

Great, thank you! 🚀 🙌

@tiangolo tiangolo merged commit 44fc676 into fastapi:master Sep 20, 2025
29 checks passed
@thomasleveil thomasleveil deleted the fix-9116-form-and-file-order branch September 26, 2025 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working p2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants