apoorva-01 opened a new pull request, #1545:
URL: https://github.com/apache/couchdb-fauxton/pull/1545

   ## Overview
   
   Open a document that has an integer larger than `Number.MAX_SAFE_INTEGER`, 
save it from the editor, and the value gets rounded: `9223372036854775807` 
comes back as `9223372036854776000`. CouchDB stores the number fine, but the 
editor runs the document through the browser's native 
`JSON.parse`/`JSON.stringify`, which can't hold integers that big.
   
   The document round-trips through native JSON in a few places: the Doc model 
fetches with jQuery (`dataType: 'json'`, so it's already rounded on load), the 
editor renders it with `JSON.stringify`, re-parses the edited text on save, and 
the model serializes the request body. Any one of those drops the precision.
   
   This adds a small helper (`app/core/lossless-json.js`) that wraps 
[lossless-json](https://github.com/josdejong/lossless-json) (MIT). It parses 
oversized integers into `LosslessNumber` and leaves everything that fits as a 
plain JS number, then gets used at those points in the Doc model and the doc 
editor so the value survives a read, an edit and a save without changing.
   
   One thing I'd like your call on: I kept this scoped to the document editor 
and its Doc model. The same rounding can happen anywhere the app parses a 
response with native JSON, so the broader option is to do the lossless parsing 
down in the core ajax layer (`app/core/ajax.js`) and cover the whole app at 
once. That has a much wider blast radius, so I left it out on purpose. Glad to 
go that way instead if you'd prefer. rnewson mentioned on the issue that this 
isn't easy in the current framework, which is fair, so treat this as one 
approach rather than the final word.
   
   Credit where it's due: M-Tesla sketched a very similar lossless-json 
approach in the issue thread but hadn't opened a PR.
   
   ## Testing recommendations
   
   Insert a doc with a big integer, something like `{"_id": "big", 
"hugeNumber": 9223372036854775807}`, open it in Fauxton, save it, then fetch it 
again. The value should be unchanged.
   
   On the code side, `npm run jest` and `npm run stylecheck`. There's a unit 
test for the helper and a round-trip test on the Doc model that stubs the ajax 
layer and checks the real save body keeps the full number (it fails without the 
fix). The full suite passed for me, 744 tests.
   
   ## GitHub issue number
   
   Fixes apache/couchdb#6008
   
   ## Checklist
   
   - [x] Code is written and works correctly;
   - [x] Changes are covered by tests;
   - [ ] Documentation reflects the changes (no docs affected);
   - [ ] Update 
[rebar.config.script](https://github.com/apache/couchdb/blob/main/rebar.config.script)
 with the correct tag once a new Fauxton release is made (release step, not 
part of this PR).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to