swagger-api / swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
https://swagger.io
Apache License 2.0
26.64k stars 8.96k forks source link

Rendering of double as 0 instead of 0.0 under example tab #7337

Open mg1308 opened 3 years ago

mg1308 commented 3 years ago

This should no longer be an issue with 3.X. Please reopen otherwise.

Originally posted by @webron in https://github.com/swagger-api/swagger-ui/issues/2088#issuecomment-288948198

I still see this issue of double not rendered correctly under "example" tab

andreatth commented 3 years ago

We are experiencing the same problem, and would like to have a solution.

fredebk commented 3 years ago

I have the same issue that responses containing decimals are rendered without them, e.g. -1.0 is rendered as -1

mathis-m commented 3 years ago

This is a javascript limitation...

const double = 1.0 // will be 1
char0n commented 3 years ago

Hi all,

Yes as @mathis-m mentioned, it's a JavaScript thing.

Number 0.0 is not decimal or integer. It's a float, specifically double-precision 64-bit binary format IEEE 754. The only difference between 0.0 and 0 is the notation that was used to create aforementioned floats. There is also a special case of -0 and +0 which is even more confusing ;]

-0 === +0; // true
Object.is(-0, +0); // true

Basically if numbers are defined in JSON Schema or transfered via API, there no way to get the information for the JavaScript, what notation was used to create the number.

Related problem https://github.com/swagger-api/swagger-ui/issues/7478#issuecomment-916190335

fredebk commented 3 years ago

I understand the reasoning that it is JavaScript's way of formatting numbers. But this is JSON returned as a string by an API. Why does it need to be parsed as a number?

mathis-m commented 3 years ago

@fredebk this is because swagger-ui is formatting the response.

char0n commented 3 years ago

Yes we need to parse the response to display it - and that's where the original notation is lost.

fredebk commented 3 years ago

I understand that. However, after identifying that this is a number, and adding syntax highlighting, the original string should be written, rather than the parsed number.

char0n commented 3 years ago

I understand that. However, after identifying that this is a number, and adding syntax highlighting, the original string should be written, rather than the parsed number.

That would include significant effort, probably involving creating custom JSON parser and mechanism to map resulting highlighted code to parsed CST nodes from custom parser.

This is unfortunate issue of JavaScript that we'll not be mitigating currently. It's possible that I'm wrong and the problem is somehow fixable with minimal effort solution that I don't currently see. In that case please issue a PR, and we'll collaborate further there.