fix(python-fastapi): relax strict typing for path/query/header params (#21905)

Closed
GO
2mo ago
+210-65

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:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    Commit all changed files.
  • 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 (no strict=True) for path/query/header params to allow coercion.
    • Regenerated the python-fastapi petstore sample; other generators are unchanged.
  • Refactors

    • Replaced the relaxStrict flag and shouldRelaxStrictParameterTyping() with PydanticCoercibleType and a getPydanticParameterType() factory; PythonFastAPIServerCodegen selects the coercible type for path/query/header and the strict base type for others.
    • Aligned with upstream ConstraintApplier/ConstraintType API and made shared types/import helpers in AbstractPythonCodegen protected for extension.