swagger-api / swagger-api/swagger-ui

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

Open
#9,442 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
29k
Forks
9.3k
Avg merge
2d 23h
Merged PRs (30d)
25

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied FastAPI reproduction with the provided OpenAPI 3.1 schema, then trace Swagger UI's JavaScript handling of the integer example and response. Done means the UI displays 89490200001123456789 exactly, matching the curl response, without requiring the value to be represented as a string.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, openapi
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.