Preserve integers larger than MAX_SAFE_INTEGER in the doc editor - #1545
Open
apoorva-01 wants to merge 1 commit into
Open
Preserve integers larger than MAX_SAFE_INTEGER in the doc editor#1545apoorva-01 wants to merge 1 commit into
apoorva-01 wants to merge 1 commit into
Conversation
Saving a document with an integer past Number.MAX_SAFE_INTEGER rounded it (9223372036854775807 became 9223372036854776000) because the editor ran the document through native JSON.parse/stringify. Use lossless-json for the document's parse and serialize so the value round-trips intact. Fixes apache/couchdb#6008
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Save a document with an integer bigger than
Number.MAX_SAFE_INTEGERand it comes back rounded:9223372036854775807turns into9223372036854776000. CouchDB keeps the number fine, but the editor runs the doc through nativeJSON.parse/JSON.stringify, which can't hold ints that big. It rounds on load, on display, on re-parse, and on save.I added a small helper (
app/core/lossless-json.js) around lossless-json (MIT) that keeps oversized ints asLosslessNumberand leaves normal numbers alone, then used it at those spots in the Doc model and the editor.I kept it scoped to the doc editor. The same rounding can happen anywhere the app parses JSON, so the wider option is to do this in the core ajax layer instead. Bigger blast radius, so I left it out for now. Happy to go that way if you'd rather. (M-Tesla sketched basically this approach on the issue but hadn't opened a PR.)
Testing recommendations
Add a doc like
{"_id": "big", "hugeNumber": 9223372036854775807}, open it in Fauxton, save, fetch again. Should be unchanged.npm run jestandnpm run stylecheckare green (744 tests); there's a round-trip test that checks the save body keeps the full number, and it fails without the fix.GitHub issue number
Fixes apache/couchdb#6008
Checklist