stoplightio / http-spec

Utilities to normalize OpenAPI v2 and v3 objects for the Stoplight ecosystem.
https://stoplight.io
Apache License 2.0
20 stars 12 forks source link

Fix int64 overflow issue to due precision issues in Typescript with large numbers #253

Closed ilanashapiro closed 1 year ago

ilanashapiro commented 1 year ago

Currently, MAX_INT_64 is set to 2 ** 63 - 1, and MIN_INT_64 is set to 0 - 2 ** 63 in src/oas/transformers/schema/keywords/format.ts. The TypeScript compiler evaluates these, respectively, to 9223372036854776000 and -9223372036854776000.

Motivation and Context

These values are outside the range of int64 and result in bugs in clients such as Prism that use this library (e.g. https://github.com/stoplightio/prism/issues/1992).

The reasoning behind this erroneous calculation is explained here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER. Essentially, we are overflowing the capacity of JavaScript's Number type, and beyond Number.MAX_SAFE_INTEGER + 1 (9007199254740992), the IEEE-754 floating-point format can no longer represent every consecutive integer (see: https://stackoverflow.com/questions/1379934/large-numbers-erroneously-rounded-in-javascript).

Description

Thus, instead of setting MAX_INT_64 to 2 ** 63 - 1, and MIN_INT_64 to 0 - 2 ** 63, we set them to TypeScript's built-in Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER instead.

How Has This Been Tested?

Update local tests including screenshot tests.

Types of changes

Checklist

ilanashapiro commented 1 year ago

@rattrayalex @RobertCraigie

ilanashapiro commented 1 year ago

@daniel-white Hi! Could someone from your team please take a look at this PR? Thanks!

P0lip commented 1 year ago

Heyo! I'll take a look at the PR soon

stoplight-bot commented 1 year ago

:tada: This PR is included in version 6.0.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: