openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[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

Open louissullivan4 opened this issue 1 year ago • 4 comments

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
  1. Run openapi-generator generate -g python-fastapi -i in.json
  2. Open src/openapi_server/apis/default_api.py
  3. 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

louissullivan4 avatar Dec 13 '24 11:12 louissullivan4

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?

wing328 avatar Dec 21 '24 08:12 wing328

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.

Derposoft avatar Jan 25 '25 22:01 Derposoft

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.

mhueck avatar Feb 07 '25 22:02 mhueck

I checked the

[--strict-spec <true/false strict behavior>]

But it seems not to work.

JaxDoge avatar Jun 05 '25 21:06 JaxDoge