fix(python-fastapi): rebase #24098 onto master (relax path/query/header strict typing, #21905)
This PR rebases @goopher's #24098 onto current master. It does not change the original fix; it only resolves merge conflicts and keeps the behavior working against upstream refactors that landed after #24098.
Original problem (#21905): Pydantic strict types (StrictInt/StrictStr/StrictFloat) and Field(strict=True) disable string coercion. Path/query/header values always arrive as strings, so FastAPI rejected valid requests with 422 (int_type: Input should be a valid integer).
Original approach (from #24098):
- Path/query/header params emit coercible types (
int/str/float) and omitstrict=True. - Body params and the Python client stay strict (JSON bodies carry real types).
- Strictness is a type (
PydanticCoercibleType), selected bygetPydanticParameterType(), not a boolean flag.
What this rebase had to reconcile with master:
- Keep #24141's centralized
ConstraintApplier(do not restore inlined min/max/length constraints). - Keep #24098's
PydanticCoercibleTypeand FastAPIgetPydanticParameterType()for path/query/header. - Regenerated
python-fastapipetstore samples with HEAD's current signature shape (Query(...)/alias=/ trailing commas) and the relaxed types.
Fixes #21905
Supersedes / continues #24098 (credit: @goopher)
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./bin/generate-samples.sh bin/configs/java*.
IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed. - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
Summary by cubic
Fixes #21905 by relaxing strict Pydantic typing for python-fastapi path/query/header/cookie params, which previously rejected valid requests with 422 because wire-string values couldn't be coerced to StrictInt/StrictStr/StrictFloat.
Bug Fixes
- Path/query/header/cookie params now emit coercible
int/str/floattypes withoutstrict=Truevia a newPydanticCoercibleType; body params and the Python client keep strict types. - Endpoint argument commas now stay on the parameter line; a trailing newline in the
endpoint_argument_definition.mustachepartial was making them wrap onto their own line. - Rebased onto master while preserving the
ConstraintApplierfrom #24141, with regenerated petstore samples and regression tests.