From a469e919bbee8704d0bbc18d435d7a4d27f65027 Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Fri, 2 Dec 2022 09:58:23 -0500 Subject: [PATCH] fix(NODE-4464):stringify and parse negative zero to and from $numberDouble: -0.0 (#531) --- src/extended_json.ts | 31 +++++++++++-------- test/node/bson_corpus.spec.test.js | 2 ++ test/node/{double_tests.js => double.test.ts} | 30 ++++++++++++++++-- test/node/extended_json_tests.js | 3 +- 4 files changed, 49 insertions(+), 17 deletions(-) rename test/node/{double_tests.js => double.test.ts} (77%) diff --git a/src/extended_json.ts b/src/extended_json.ts index 90269623..c2a9671b 100644 --- a/src/extended_json.ts +++ b/src/extended_json.ts @@ -73,11 +73,15 @@ function deserializeValue(value: any, options: EJSON.Options = {}) { return value; } - // if it's an integer, should interpret as smallest BSON integer - // that can represent it exactly. (if out of range, interpret as double.) - if (Math.floor(value) === value) { - if (value >= BSON_INT32_MIN && value <= BSON_INT32_MAX) return new Int32(value); - if (value >= BSON_INT64_MIN && value <= BSON_INT64_MAX) return Long.fromNumber(value); + if (Number.isInteger(value) && !Object.is(value, -0)) { + // interpret as being of the smallest BSON integer type that can represent the number exactly + if (value >= BSON_INT32_MIN && value <= BSON_INT32_MAX) { + return new Int32(value); + } + if (value >= BSON_INT64_MIN && value <= BSON_INT64_MAX) { + // TODO(NODE-4377): EJSON js number handling diverges from BSON + return Long.fromNumber(value); + } } // If the number is a non-integer or out of integer range, should interpret as BSON Double. @@ -216,16 +220,17 @@ function serializeValue(value: any, options: EJSONSerializeOptions): any { } if (typeof value === 'number' && (!options.relaxed || !isFinite(value))) { - // it's an integer - if (Math.floor(value) === value) { - const int32Range = value >= BSON_INT32_MIN && value <= BSON_INT32_MAX, - int64Range = value >= BSON_INT64_MIN && value <= BSON_INT64_MAX; - + if (Number.isInteger(value) && !Object.is(value, -0)) { // interpret as being of the smallest BSON integer type that can represent the number exactly - if (int32Range) return { $numberInt: value.toString() }; - if (int64Range) return { $numberLong: value.toString() }; + if (value >= BSON_INT32_MIN && value <= BSON_INT32_MAX) { + return { $numberInt: value.toString() }; + } + if (value >= BSON_INT64_MIN && value <= BSON_INT64_MAX) { + // TODO(NODE-4377): EJSON js number handling diverges from BSON + return { $numberLong: value.toString() }; + } } - return { $numberDouble: value.toString() }; + return { $numberDouble: Object.is(value, -0) ? '-0.0' : value.toString() }; } if (value instanceof RegExp || isRegExp(value)) { diff --git a/test/node/bson_corpus.spec.test.js b/test/node/bson_corpus.spec.test.js index 62dbf94b..730a6cb3 100644 --- a/test/node/bson_corpus.spec.test.js +++ b/test/node/bson_corpus.spec.test.js @@ -47,6 +47,8 @@ function nativeToREJSON(native) { } function normalize(cEJ) { + // TODO(NODE-3396): loses information about the original input + // ex. parse will preserve -0 but stringify will output +0 return JSON.stringify(JSON.parse(cEJ)); } diff --git a/test/node/double_tests.js b/test/node/double.test.ts similarity index 77% rename from test/node/double_tests.js rename to test/node/double.test.ts index 5346f6cf..b6fcb37b 100644 --- a/test/node/double_tests.js +++ b/test/node/double.test.ts @@ -1,6 +1,4 @@ -'use strict'; - -const BSON = require('../register-bson'); +import * as BSON from '../register-bson'; const Double = BSON.Double; describe('BSON Double Precision', function () { @@ -89,4 +87,30 @@ describe('BSON Double Precision', function () { expect(type).to.not.equal(BSON.BSON_DATA_NUMBER); expect(type).to.equal(BSON.BSON_DATA_INT); }); + + describe('extended JSON', () => { + describe('stringify()', () => { + it('preserves negative zero in canonical format', () => { + const result = BSON.EJSON.stringify({ a: -0.0 }, { relaxed: false }); + expect(result).to.equal(`{"a":{"$numberDouble":"-0.0"}}`); + }); + + it('loses negative zero in relaxed format', () => { + const result = BSON.EJSON.stringify({ a: -0.0 }, { relaxed: true }); + expect(result).to.equal(`{"a":0}`); + }); + }); + + describe('parse()', () => { + it('preserves negative zero in deserialization with relaxed false', () => { + const result = BSON.EJSON.parse(`{ "a": -0.0 }`, { relaxed: false }); + expect(result.a).to.have.property('_bsontype', 'Double'); + }); + + it('preserves negative zero in deserialization with relaxed true', () => { + const result = BSON.EJSON.parse(`{ "a": -0.0 }`, { relaxed: true }); + expect(Object.is(result.a, -0), 'expected prop a to be negative zero').to.be.true; + }); + }); + }); }); diff --git a/test/node/extended_json_tests.js b/test/node/extended_json_tests.js index 551b0737..fd5bae48 100644 --- a/test/node/extended_json_tests.js +++ b/test/node/extended_json_tests.js @@ -79,7 +79,7 @@ describe('Extended JSON', function () { }); it('should correctly extend an existing mongodb module', function () { - // Serialize the document + // TODO(NODE-4377): doubleNumberIntFit should be a double not a $numberLong var json = '{"_id":{"$numberInt":"100"},"gh":{"$numberInt":"1"},"binary":{"$binary":{"base64":"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+Pw==","subType":"00"}},"date":{"$date":{"$numberLong":"1488372056737"}},"code":{"$code":"function() {}","$scope":{"a":{"$numberInt":"1"}}},"dbRef":{"$ref":"tests","$id":{"$numberInt":"1"},"$db":"test"},"decimal":{"$numberDecimal":"100"},"double":{"$numberDouble":"10.1"},"int32":{"$numberInt":"10"},"long":{"$numberLong":"200"},"maxKey":{"$maxKey":1},"minKey":{"$minKey":1},"objectId":{"$oid":"111111111111111111111111"},"objectID":{"$oid":"111111111111111111111111"},"oldObjectID":{"$oid":"111111111111111111111111"},"regexp":{"$regularExpression":{"pattern":"hello world","options":"i"}},"symbol":{"$symbol":"symbol"},"timestamp":{"$timestamp":{"t":0,"i":1000}},"int32Number":{"$numberInt":"300"},"doubleNumber":{"$numberDouble":"200.2"},"longNumberIntFit":{"$numberLong":"7036874417766400"},"doubleNumberIntFit":{"$numberLong":"19007199250000000"}}'; @@ -103,6 +103,7 @@ describe('Extended JSON', function () { expect(doc1.int32Number._bsontype).to.equal('Int32'); expect(doc1.doubleNumber._bsontype).to.equal('Double'); expect(doc1.longNumberIntFit._bsontype).to.equal('Long'); + // TODO(NODE-4377): EJSON should not try to make Longs from large ints expect(doc1.doubleNumberIntFit._bsontype).to.equal('Long'); });