swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

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

Open MaxDev98 opened this issue 2 years ago • 1 comments

Q&A (please complete the following information)

  • OS: Debian 12
  • Browser: Firefox
  • Version: 121
  • Method of installation: pip
  • Swagger-UI version: 5.x (provided by fastapi)
  • Swagger/OpenAPI version: OpenAPI 3.1 (provided by fastapi)

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.

MaxDev98 avatar Dec 22 '23 14:12 MaxDev98

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

hkosova avatar May 15 '24 14:05 hkosova