Skip to content

Fix Form model fields_set for omitted defaults#15027

Open
Sebastianhayashi wants to merge 1 commit intofastapi:masterfrom
Sebastianhayashi:codex/fix-13399
Open

Fix Form model fields_set for omitted defaults#15027
Sebastianhayashi wants to merge 1 commit intofastapi:masterfrom
Sebastianhayashi:codex/fix-13399

Conversation

@Sebastianhayashi
Copy link

Summary

Fix inconsistent model_fields_set behavior between JSON body and Form() model parsing.

For single non-embedded Form() parameters typed as a Pydantic model, omitted default fields were being treated as explicitly provided and showed up in model_fields_set.

What changed

  • Added regression test in tests/test_forms_single_model.py:
    • test_default_field_not_marked_as_set_for_form_model
  • Updated fastapi/dependencies/utils.py in request_body_to_args:
    • Track which form fields are actually present.
    • After successful model validation, set __pydantic_fields_set__ to only those provided fields.

Validation

pytest -q \
  tests/test_forms_single_model.py::test_default_field_not_marked_as_set_for_form_model \
  tests/test_forms_single_model.py::test_no_data \
  tests/test_forms_single_model.py::test_defaults

Result: 3 passed

Related to #13399.

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 28, 2026

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing Sebastianhayashi:codex/fix-13399 (39a5062) with master (48d58ae)

Open in CodSpeed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant