🐛 Don't pre-fill form parameters with default values in _get_multidict_value#14283
Draft
YuriiMotov wants to merge 10 commits into
Draft
🐛 Don't pre-fill form parameters with default values in _get_multidict_value#14283YuriiMotov wants to merge 10 commits into
_get_multidict_value#14283YuriiMotov wants to merge 10 commits into
Conversation
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
This comment was marked as resolved.
This comment was marked as resolved.
Contributor
|
This pull request has a merge conflict that needs to be resolved. |
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.
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
Related discussions and PRs
Some clarifications
Why
isinstance(field.field_info, (params.Form, temp_pydantic_v1_params.Form))was removed?This was the way to distinguish
Formparameters from others. But now we haveform_inputparameter that effectively does it.Also, as we previously only checked every field to be annotated with
Form, the logic of treating emptystras None didn't work for form fields declared as model.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.