fix(python-fastapi): relax strict typing for path/query/header params (#21905)#24098
fix(python-fastapi): relax strict typing for path/query/header params (#21905)#24098Goopher wants to merge 2 commits into
Conversation
…OpenAPITools#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). Add a relaxStrict flag to PydanticType that emits coercible types (int/str/float) and omits strict=True, gated behind a 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. Claude-Session: https://claude.ai/code/session_019g7iwAyg7ErX1WrhqHyTn6
There was a problem hiding this comment.
1 issue found across 10 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="samples/server/petstore/python-fastapi/src/openapi_server/apis/store_api_base.py">
<violation number="1" location="samples/server/petstore/python-fastapi/src/openapi_server/apis/store_api_base.py:5">
P2: Unused `StrictInt` import remains after strict type relaxation — should be removed to avoid F401 lint failures in generated code.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| from typing import ClassVar, Dict, List, Tuple # noqa: F401 | ||
|
|
||
| from pydantic import Field, StrictInt, StrictStr | ||
| from pydantic import Field, StrictInt |
There was a problem hiding this comment.
P2: Unused StrictInt import remains after strict type relaxation — should be removed to avoid F401 lint failures in generated code.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/server/petstore/python-fastapi/src/openapi_server/apis/store_api_base.py, line 5:
<comment>Unused `StrictInt` import remains after strict type relaxation — should be removed to avoid F401 lint failures in generated code.</comment>
<file context>
@@ -2,7 +2,7 @@
from typing import ClassVar, Dict, List, Tuple # noqa: F401
-from pydantic import Field, StrictInt, StrictStr
+from pydantic import Field, StrictInt
from typing import Any, Dict
from typing_extensions import Annotated
</file context>
| from pydantic import Field, StrictInt | |
| from pydantic import Field |
|
Rather than expanding the responsibilities of Currently we have a whole essay to try to describe the background about the coercion that occurs and when, but I believe it is always better to capture the relations and the rules as explicit code-structures (read, separation-of-responsibilities). My understanding is that the server-bound parameters never should utilize the strictness that |
Replace the relaxStrict boolean + shouldRelaxStrictParameterTyping hook with
an explicit PydanticCoercibleType subclass of PydanticType that never emits
Pydantic strict types for the scalar kinds where strictness blocks coercion.
Selection moves into a getPydanticParameterType() factory: PythonFastAPIServer
returns PydanticCoercibleType for path/query/header params and the strict base
type for everything else. The rule ("wire-string params are never strict") is
now a code structure rather than a flag callers must remember to set, and the
explanatory comment lives in one place on the class it describes.
Behaviour-preserving: the regenerated python-fastapi sample is byte-identical
to the previous commit. Python codegen tests (fastapi, client) pass.
Claude-Session: https://claude.ai/code/session_019g7iwAyg7ErX1WrhqHyTn6
|
@Mattias-Sehlstedt, updated to have a separate class for params. |
Pydantic strict types (
StrictInt/StrictStr/StrictFloat) andField(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
relaxStrictflag toPydanticTypethat emits coercible types (int/str/float) and omitsstrict=True, gated behind a newshouldRelaxStrictParameterTyping()hook that defaults tofalse.PythonFastAPIServerCodegenoverrides 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
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-fastapiserver generator so FastAPI can coerce wire-string values and avoid 422 errors. Body params and models stay strict. Fixes #21905.Bug Fixes
int/str/float(nostrict=True) for path/query/header params to allow coercion.python-fastapipetstore sample; other generators are unchanged.Refactors
shouldRelaxStrictParameterTyping()withPydanticCoercibleTypeand agetPydanticParameterType()factory.PythonFastAPIServerCodegenreturnsPydanticCoercibleTypefor path/query/header params and the strict base type for others; shared types inAbstractPythonCodegenmadeprotectedfor extension.Written for commit 4decf3c. Summary will update on new commits.