[BUG][Python-Fastapi] Path parameters when schema type is number causes the url to not be callable as it generates the type as strict float which can't be called by curl/browser
Bug Report Checklist
- [x] Have you provided a full/minimal spec to reproduce the issue?
- [x] Have you validated the input using an OpenAPI validator (example)?
- [x] Have you tested with the latest master to confirm the issue still exists?
- [x] Have you searched for related issues/PRs?
- [ ] What's the actual output vs expected output? Current Output:
async def order_order_id_get(
order_id: Union[StrictFloat, StrictInt] = Path(..., description="")) -> str:
Expected Output
async def order_order_id_get(
order_id: float = Path(..., description="")) -> str:
- [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
For Python Fastapi path parameter, when type is number it set it to StrictFloat which isn't good for making calls to the path parameter as when you send a url it gives you an error back as it can't convert the string value to a float. I think it would be more suitable for this value to be a float as it would allow type conversion from a curl or browser call to the server.
openapi-generator version
openapi-generator v7.10.0 and master
OpenAPI declaration file content or url
openapi: 3.0.0
info:
title: Order API
version: 1.0.0
paths:
/order/{order_id}:
parameters:
- name: order_id
in: path
required: true
schema:
type: number
get:
responses:
'200':
description: Successfully retrieved
content:
application/json:
schema:
type: string
servers:
- url: http://localhost:3020
Generation Details
openapi-generator generate -g python-fastapi -i in.json
Steps to reproduce
- Run
openapi-generator generate -g python-fastapi -i in.json - Open src/openapi_server/apis/default_api.py
- See function with path param order_id
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/pull/20316
Suggest a fix
Set the number value to generate as float and not strict float
which isn't good for making calls to the path parameter as when you send a url it gives you an error back as it can't convert the string value to a float
can you please share the exact errors, stack trace, etc?
which isn't good for making calls to the path parameter as when you send a url it gives you an error back as it can't convert the string value to a float
can you please share the exact errors, stack trace, etc?
The api in this case returns 422 Unprocessable Entity and there doesn't seem to be any stack trace, I suppose because number can't be parsed as a Strict* class. Changing the type from Strict* to fixes it (e.g., StrictInt -> int) for me.
I am facing the same problem. This is giving a problem:
user_count: Optional[StrictInt] = Query(None, description="", alias="userCount"),
This gives status code 422 with message:
{
"detail": [
{
"type": "int_type",
"loc": [
"query",
"userCount"
],
"msg": "Input should be a valid integer",
"input": "121"
},
If I change the generated code manually to (make it not optional):
user_count: StrictInt = Query(None, description="", alias="userCount"),
or (use int instead of StrictInt):
user_count: Optional[int] = Query(None, description="", alias="userCount"),
I think using an int instead of StrictInt should be the way forward. Not sure why this is a StrictInt. Same goes for StrictBool and StrictFloat.
I checked the
[--strict-spec <true/false strict behavior>]
But it seems not to work.