fix(python-fastapi): relax strict typing for path/query/header params (#21905)
Pydantic strict types (StrictInt/StrictStr/StrictFloat) and Field(strict=True) disable automatic string coercion. Since path/query/header values always arrive on the wire as strings, FastAPI rejected otherwise-valid requests with a 422 (int_type: Input should be a valid integer).
This adds a relaxStrict flag to PydanticType that emits coercible types (int/str/float) and omits strict=True, gated behind a new shouldRelaxStrictParameterTyping() hook that defaults to false. PythonFastAPIServerCodegen overrides it for path/query/header params. Body params and the Python client generator are unchanged — strict typing is correct for JSON bodies.
Regenerated the python-fastapi petstore sample. Verified the python client, flask, aiohttp, blueplanet, and httpx samples regenerate with zero diff, confirming the shared-base change is inert for non-fastapi generators.
Fixes #21905
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
Commit all changed files../mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
Python technical committee — @cbornet @tomplus @krjakbrjak @fa0311 @multani (@krjakbrjak as the python-fastapi generator author).
https://claude.ai/code/session_019g7iwAyg7ErX1WrhqHyTn6
Summary by cubic
Relax strict typing for path, query, and header parameters in the python-fastapi server generator so FastAPI can coerce wire-string values and avoid 422 errors. Body params and models stay strict. Fixes #21905.
-
Bug Fixes
- Emit
int/str/float(nostrict=True) for path/query/header params to allow coercion. - Regenerated the
python-fastapipetstore sample; other generators are unchanged.
- Emit
-
Refactors
- Replaced the relaxStrict flag and
shouldRelaxStrictParameterTyping()withPydanticCoercibleTypeand agetPydanticParameterType()factory;PythonFastAPIServerCodegenselects the coercible type for path/query/header and the strict base type for others. - Aligned with upstream
ConstraintApplier/ConstraintTypeAPI and made shared types/import helpers inAbstractPythonCodegenprotectedfor extension.
- Replaced the relaxStrict flag and