Here's a self-contained, minimal, reproducible, example with my use case:
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class TruncTest(BaseModel):
integer: int
string: str
@app.post("/trunctest")
async def trunc_test(a: TruncTest):
print("Int: " + str(a.integer) + "Str: " + a.string)
When I tried testing this with a POST request (using swagger, since thats the only way I know of to do a post request):
{
"integer": 563808552288780322,
"string": "563808552288780322"
}
I do not know whether this is an issue with FastAPI or Swagger or even Mozilla Firefox
OS: [e.g. Linux / Windows / macOS]: Linux (fedora rawhide, kernel version: Linux localhost.localdomain 5.10.0-0.rc4.20201119gitc2e7554e1b85.81.fc34.x86_64 #1 SMP Thu Nov 19 20:45:31 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux)
FastAPI Version [e.g. 0.3.0]: 0.61.2
To know the FastAPI version use:
python -c "import fastapi; print(fastapi.__version__)"
To know the Python version use: Python 3.9.0
python --version

Well, for those who didn't get it, the issue is with the integer field. It's not a FastAPI issue, it's a Swagger one.
Proof: run the below command on your terminal, and you'll see it's not truncated.
curl -X POST "http://127.0.0.1:8000/trunctest" -H "accept: application/json" -d "{\"integer\": 563808552288780322, \"string\": \"563808552288780322\"}"
thanks, that gives the appropriate results
Notice that 563808552288780322 happens to be larger than the javascript constant for Number.MAX_SAFE_INTEGER (since integers in JS are stored as floats, they only get 53 bits of actual precision), so it would make sense that the number would lose precision at very high values.
You get the same kind of precision issue if you try this in Python.
>>> int("563808552288780322")
563808552288780322
>>> float("563808552288780322")
5.6380855228878035e+17
>>> int(float("563808552288780322"))
563808552288780352
For the record, RFC 7159 mentions how integers outside of the 卤2^53 range, while not outright forbidden by the ECMA-404 JSON spec, might run into interoperability issues.
Unfortunately, discord uses such large numbers so I guess I will have to just use strings for large numbers then
Unfortunately, discord uses such large numbers so I guess I will have to just use strings for large numbers then
If memory serves me right, Discord uses Twitter snowflakes for their IDs, which have that caveat documented and advise that consumers of APIs using them use the string representation to avoid that sort of issue.
Most helpful comment
Notice that
563808552288780322happens to be larger than the javascript constant forNumber.MAX_SAFE_INTEGER(since integers in JS are stored as floats, they only get 53 bits of actual precision), so it would make sense that the number would lose precision at very high values.You get the same kind of precision issue if you try this in Python.
For the record, RFC 7159 mentions how integers outside of the 卤2^53 range, while not outright forbidden by the ECMA-404 JSON spec, might run into interoperability issues.