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
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.
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.
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
In SwaggerUI, however, only
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:
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:
Swagger-UI configuration options: Default FastAPI swagger configuration
Describe the bug you're encountering
To reproduce...
Steps to reproduce the behavior:
app = FastAPI()
class TestClass(BaseModel): iccid: float = Field(example=89490200001123456789)
@app.get("/", response_model=TestClass) async def root(): test = TestClass(iccid="89490200001123456789")