-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
🐛 Don't pre-fill form parameters with default values in _get_multidict_value
#14283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
YuriiMotov
wants to merge
8
commits into
fastapi:master
Choose a base branch
from
YuriiMotov:issue-13399_dont-prefill-form
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
🐛 Don't pre-fill form parameters with default values in _get_multidict_value
#14283
YuriiMotov
wants to merge
8
commits into
fastapi:master
from
YuriiMotov:issue-13399_dont-prefill-form
+224
−18
Conversation
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
commit 1b8d0d7 Author: sneakers-the-rat <sneakers-the-rat@protonmail.com> Date: Wed Aug 20 03:24:25 2025 -0700 ok but seriously commit d3ccab4 Author: sneakers-the-rat <sneakers-the-rat@protonmail.com> Date: Tue Aug 19 23:38:25 2025 -0700 rm being able to determine the input format of a model commit fec0a06 Merge: 3f2e0f5 cad08bb Author: sneakers-the-rat <sneakers-the-rat@protonmail.com> Date: Mon Apr 14 20:03:14 2025 -0700 Merge branch 'form-defaults' of https://github.com/sneakers-the-rat/fastapi into form-defaults commit 3f2e0f5 Author: sneakers-the-rat <sneakers-the-rat@protonmail.com> Date: Mon Apr 14 20:01:50 2025 -0700 lint commit cad08bb Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue Apr 15 02:47:42 2025 +0000 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks commit f63e983 Author: sneakers-the-rat <sneakers-the-rat@protonmail.com> Date: Mon Apr 14 19:46:34 2025 -0700 docs for handling default values, pass field to validation context commit 529d486 Merge: a9acab8 159824e Author: sneakers-the-rat <sneakers-the-rat@protonmail.com> Date: Sat Mar 8 17:45:40 2025 -0800 Merge branch 'form-defaults' of https://github.com/sneakers-the-rat/fastapi into form-defaults commit a9acab8 Author: sneakers-the-rat <sneakers-the-rat@protonmail.com> Date: Sat Mar 8 17:42:38 2025 -0800 lint commit 159824e Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun Mar 9 01:42:31 2025 +0000 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks commit e761843 Author: sneakers-the-rat <sneakers-the-rat@protonmail.com> Date: Sat Mar 8 17:42:21 2025 -0800 pydantic v1 compat commit 64f2528 Author: sneakers-the-rat <sneakers-the-rat@protonmail.com> Date: Sat Mar 8 17:38:03 2025 -0800 fix handling form data with fields that are not annotated as Form() commit 7fade13 Author: sneakers-the-rat <sneakers-the-rat@protonmail.com> Date: Sat Mar 8 17:37:32 2025 -0800 fix just the extra values problem (again, purposefully with failing tests to demonstrate the problem, fixing in next commit) commit 49f6b83 Author: sneakers-the-rat <sneakers-the-rat@protonmail.com> Date: Sat Mar 8 17:36:03 2025 -0800 add failing tests for empty input values to get a CI run baseline for them commit 15eb678 Author: sneakers-the-rat <sneakers-the-rat@protonmail.com> Date: Thu Mar 6 19:35:53 2025 -0800 mypy lint commit 1a58af4 Author: sneakers-the-rat <sneakers-the-rat@protonmail.com> Date: Thu Mar 6 19:31:28 2025 -0800 finish pydantic 1 compat commit a2ad8b1 Author: sneakers-the-rat <sneakers-the-rat@protonmail.com> Date: Thu Mar 6 19:22:11 2025 -0800 python 3.8 and pydantic 1 compat commit 76c4d31 Author: sneakers-the-rat <sneakers-the-rat@protonmail.com> Date: Thu Mar 6 19:06:44 2025 -0800 don't prefill defaults in form input
5a0ee8d to
4c68df1
Compare
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
sneakers-the-rat
approved these changes
Nov 3, 2025
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.
Credits
The author of solution is @sneakers-the-rat. Initial PR: #13464.
I just separated these changes from (IMO) unnecessary\unrelated changes, refactored the code a bit and simplified tests to make it easier to review.
Problem description
Current implementation of
_get_multidict_valueputs the default value if the parameter value is empty string or is missing.This makes it impossible to determine if this value was passed explicitly or not.
See code example in the details
Also, there is a bug that treating empty strings in input as
Nonedoesn't work if the default value isNone:See test in the details
Current ststus
Waiting for the #13537 to be merged. It will simplify this PR a bit
Related discussions and PRs
Some clarifications
Why
if key not in values:changed toif key not in field_aliases:?In
_extract_form_bodywe now passform_input=Trueto_get_multidict_valuethat disables the pre-filling with default values the form parameters with missing or empty str values.Later in
_extract_form_bodythere were lines that added extra parameters tovalues.It worked previously because we had values for all explicitly declared parameters (we used defaults if parameter was not passed or passed as
"").But now it's not true, so we need to update this condition to
if key not in field_aliases:. Otherwise it would break the logic of treating emptystras absence of the value for Form parameters.Why
isinstance(field.field_info, (params.Form, temp_pydantic_v1_params.Form))was removed?This was the way to distinguish Form parameters from others. But now we have
form_inputparameter that effectively does it.Also, as we previously only checked
Formclass, the logic of treating emptystras None didn't work forFileparameters. So, "Send empty value" option in Swagger didn't work forFileparameters.Now it will work.
We can consider moving this to separate PR which should be merged before this one.
Tests
Part of tests are added just for completeness to cover different ways of Form field declaration.
On
masterthe following tests fail:test_defaults_form_param[empty_strings_sent]- empty string toFileparameter is not treated asNonetest_defaults_form_model[empty_strings_sent]- same as previous but with parameter declared as form modeltest_form_model_fields_set- currently FastAPI pre-fills values with defaults, somodel_fields_setdoesn't work as expected