Skip to content

Fix BaseModel parameter defaults#4

Open
yashwant86 wants to merge 4 commits intomasterfrom
pr-15269
Open

Fix BaseModel parameter defaults#4
yashwant86 wants to merge 4 commits intomasterfrom
pr-15269

Conversation

@yashwant86
Copy link
Copy Markdown

@yashwant86 yashwant86 commented Apr 7, 2026

Mirror of fastapi#15269


Summary by MergeMonkey

  • Fixes & Patches:
    • BaseModel parameter defaults are no longer incorrectly marked as explicitly set when omitted from requests
    • Omitted fields with invalid default types no longer trigger validation errors

@mergemonkeyhq
Copy link
Copy Markdown

mergemonkeyhq Bot commented Apr 14, 2026

Walkthrough

When a user defines a FastAPI endpoint with a BaseModel parameter (Query, Header, Cookie, or Form) containing fields with defaults, and the client omits those fields from the request, the framework now leaves them absent from the raw input dict. Pydantic then applies its own defaults, correctly keeping model_fields_set empty for those fields. Previously, FastAPI pre-filled defaults into the input, causing Pydantic to treat them as explicitly provided.

Changes

Files Summary
BaseModel parameter default handling
fastapi/dependencies/utils.py
Introduces `use_default_when_missing` flag to `_get_multidict_value` and `_extract_form_body` so that when a BaseModel wraps query/header/cookie/form parameters, omitted fields are left absent rather than pre-filled with defaults. This lets Pydantic apply its own defaults without marking them as explicitly provided in `model_fields_set`.
New tests for model parameter defaults
tests/test_model_param_defaults.py
Adds tests verifying that omitted BaseModel fields are not marked as set, explicitly provided values are still tracked, and models with invalid default types do not trigger validation when the field is omitted.
Test expectation corrections
tests/test_forms_single_model.py
tests/test_tutorial/test_header_param_models/test_tutorial001.py
tests/test_tutorial/test_header_param_models/test_tutorial002.py
tests/test_tutorial/test_header_param_models/test_tutorial003.py
Updates expected validation error inputs to reflect that default-valued fields are no longer pre-populated in the input dict when omitted from requests.

Sequence Diagram

sequenceDiagram
    participant Client
    participant FastAPI
    participant RequestParamsToArgs as request_params_to_args
    participant GetMultidictValue as _get_multidict_value
    participant PydanticModel as Pydantic BaseModel

    Client->>FastAPI: GET /query (no field_1 param)
    FastAPI->>RequestParamsToArgs: Extract fields from request
    RequestParamsToArgs->>RequestParamsToArgs: Detect BaseModel param, set is_model_param=true
    loop For each model field
        RequestParamsToArgs->>GetMultidictValue: field, params, use_default_when_missing=false
        alt Field present in request
            GetMultidictValue-->>RequestParamsToArgs: actual value
        else Field missing
            GetMultidictValue-->>RequestParamsToArgs: None (no default injected)
        end
    end
    RequestParamsToArgs->>PydanticModel: Construct with only explicitly provided fields
    PydanticModel-->>RequestParamsToArgs: model instance (fields_set excludes defaults)
    RequestParamsToArgs-->>FastAPI: Validated model
    FastAPI-->>Client: 200 {fields_set: [], model: {field_1: true}}
Loading

Dig Deeper With Commands

  • /review <file-path> <function-optional>
  • /chat <file-path> "<question>"
  • /roast <file-path>

Runs only when explicitly triggered.

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.

2 participants