swagger-api / swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
https://swagger.io
Apache License 2.0
26.57k stars 8.96k forks source link

SwaggerUI fails to display large int (>32bit) provided by python correctly #9442

Open MaxDev98 opened 10 months ago

MaxDev98 commented 10 months ago

Q&A (please complete the following information)

Content & configuration

I use Python with FastAPI to provide a proper API endpoint for my MariaDB database. In this database, among other things, ICCIDs of SIM cards for the public cellular network are stored.

An ICCID consists of 18 to 22 digits and is stored in MariaDB as a "float" type.

The Pydantic class defines the "ICCID" field as "int". When defined as a "float", Python uses exponents for rounding, which leads to data loss. Since an "int" has no limitation in Python and can therefore be greater than 32 bits, the field is defined as int.

Example: ICCID in the database: 89490200001123456789

String: iccid = "89490200001123456789" print(type(iccid)) -> <class 'str'> -> okay

Float: print(float(iccid)) -> 8.949020000112345e+19 -> refers to 89490200001123450000 -> loss of information!

Int: print(int(iccid)) -> 89490200001123456789 -> okay

SwaggerUI with Javascript uses a limit of 32bit for int. The request via the API via curl is executed correctly and in this case returns

{
    "iccid": 89490200001123456789
}

In SwaggerUI, however, only

{
    "iccid": 89490200001123450000
}

is displayed.

The workaround for me at the moment is to treat the field in Pydantic (and in the database) as a string, so that the return statement looks like this:

{
    "iccid": "89490200001123456789"
}

which in my opinion is not optimal in terms of input validation and the performance, sorting algorithm, ... in the database. I would like to continue to save this as a float in my database.

Example Swagger/OpenAPI definition:

{"openapi":"3.1.0","info":{"title":"FastAPI","version":"0.1.0"},"paths":{"/":{"get":{"summary":"Root","operationId":"root__get","responses":{"200":{"description":"Successful Response","content":{"application/json":{"schema":{"$ref":"#/components/schemas/TestClass"}}}}}}}},"components":{"schemas":{"TestClass":{"properties":{"iccid":{"type":"integer","title":"Iccid","example":89490200001123456789}},"type":"object","required":["iccid"],"title":"TestClass"}}}}

Swagger-UI configuration options: Default FastAPI swagger configuration

Describe the bug you're encountering

To reproduce...

Steps to reproduce the behavior:

  1. Run the following code and use SwaggerUI for a GET request:
    
    from fastapi import FastAPI
    from pydantic import BaseModel, ConfigDict, Field

app = FastAPI()

class TestClass(BaseModel): iccid: float = Field(example=89490200001123456789)

@app.get("/", response_model=TestClass) async def root(): test = TestClass(iccid="89490200001123456789")

return test


SwaggerUI will show 89490200001123450000
Raw response via curl is: {"iccid":89490200001123456789}

### Expected behavior
SwaggerUI will show 89490200001123456789

### Additional context or thoughts
In this case, it should be sufficient to change the variable type from int to bigint within SwaggerUI.
I know it's all super stupid and weird, but maybe we can find a solution to solve the issue in another way and not use a string.
hkosova commented 6 months ago

It's the same issue as #2030; see this comment in particular: https://github.com/swagger-api/swagger-ui/issues/2030#issuecomment-805983179