Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI incorrect display of type: integer format: int64 as rounded int values in response body #2030

Closed
rgunn opened this issue Mar 15, 2016 · 17 comments

Comments

@rgunn
Copy link

rgunn commented Mar 15, 2016

swagger.json correctly shows the data types and format but the values displayed in the response body are rounded int values.

@webron
Copy link
Contributor

webron commented Mar 16, 2016

Rounded int values?

@rgunn
Copy link
Author

rgunn commented Mar 16, 2016

swagger.json properties are correctly typed

"someKey":{"type":"integer","format":"int64"},
"anotherKey":{"type":"integer","format":"int64"},

The raw JSON returned from my REST service is correct but the values displayed in the swagger response body:

"someKey": 1253261109769874400,
"anotherKey": 212081872531357440,

Should be:

"someKey": 1253261109769874438,
"anotherKey": 212081872531357452,

Which is exactly the same result as running JavaScript's
parseInt("1253261109769874438")
/*
1253261109769874400
*/

I am new to swagger and have produced swagger 2 spec for approx 70 REST API endpoints and this is the only issue I have. Javascript only supports 53 bit integers and the UI does not process these values properly although I assumed it would according to the "format" type ?

@scott-orleman
Copy link

What news of this issue? Having the same problem.

@webron
Copy link
Contributor

webron commented Sep 14, 2016

I believe this is a javascript limitation. Not sure there's much we can do about it, but ideas are welcome.

@code-ape
Copy link

code-ape commented Mar 2, 2017

I personally would like to see this resolved if possible. A project I've worked on has had to be rearchitectured because of this limitation of the values presented in the UI being rounded. We had been using 64 bit hashes.

@bobvanluijt
Copy link

Not sure if this is still an issue, but for other projects I've used this package to deal with large integers: https://www.npmjs.com/package/big-integer

@webron
Copy link
Contributor

webron commented Jun 12, 2017

@bobvanluijt thanks for that, it's still an issue, yes. Would you consider submitting a PR that would add support for it?

@heldersepu
Copy link
Contributor

This is caused by JSON.parse you can see it in response-body.jsx#L25

Try this on the browser console:

JSON.parse("{\"MyInt64\":1253261109769874438,\"MyData\":\"1253261109769874438\"}")
{MyInt64: 1253261109769874400, MyData: "1253261109769874438"}

@heldersepu
Copy link
Contributor

heldersepu commented Dec 9, 2017

Perhaps we should have a config option to bypass the prettification, and always render response as PlainText. Or a way to alternate between the RAW & Parsed views

Found this project: https://github.com/josdejong/lossless-json
looks like a nice candidate but the owner is very clear about the performance:
(Spoiler: lossless-json is much slower than native)

@shockey
Copy link
Contributor

shockey commented Dec 11, 2017

I think a Pretty/Raw switcher is the right answer here, as it offers a more generalized approach

@heldersepu
Copy link
Contributor

@shockey 100% agree, you can merge this issue with #4018

@webron
Copy link
Contributor

webron commented Jun 5, 2018

Closing this as a JavaScript limitation, with the suggested alternative of #4018. Not sure there's more we can do about this one beyond it.

@shockey
Copy link
Contributor

shockey commented Sep 28, 2019

I'm reopening this — the ECMAScript BigInt proposal is in stage 3 with support in latest Chrome, Firefox, and Opera, so JavaScript itself may not be a limitation here going forward.

@breun
Copy link

breun commented Nov 12, 2019

It seems not all browsers support BigInt yet, like for instance Safari: https://caniuse.com/#feat=bigint

@RSully
Copy link

RSully commented May 29, 2020

There is some discussion about Safari's BigInt support here: mdn/browser-compat-data#4848

There is a Webkit bug being tracked for this feature, and the latest dependency there has updates recently, so it sounds like they're working on it. Fingers crossed!

@zapek
Copy link

zapek commented Aug 10, 2020

This problem is really nasty. It's not obvious for someone using Swagger UI for checking the output of the REST API that Javascript uses float for everything and the number showing up there might be wrong.

@tim-lai
Copy link
Contributor

tim-lai commented Mar 24, 2021

Closing this issue again. On the subject of BigInt, JSON specification, section 6 (Numbers) is quite clear about supported number ranges. Going beyond them creates issues of correctly interpreting numbers.

In addition, exposing integers via API in JSON format that may be really big is not a good idea. One can accidentally generate a number that is outside of the range, and different platforms consuming the JSON will interpret the number differently. Even JSON:API has a convention of sending numbers as strings to avoid that problem.

Some time ago JavaScript landed a new type BigInt that can support ranges outside of 253 - 1. Unfortunately this type is not JSON serializable and the only recommended way of transferring it via JSON is: transfer it as a string. That way, every platform can deal with it as it likes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.