Skip to content

Commit

Permalink
fix: parse value of Int32 in constructor
Browse files Browse the repository at this point in the history
Parses the value passed into the Int32 constructor using + prefixed variable. The value should always be a number. Fixes issues whereby EJSON parse would return string value with relaxed option. Verified no breaking changes to hex strings.

NODE-2613
  • Loading branch information
Thomas Reggi authored May 29, 2020
1 parent c307ca8 commit 5cda40f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/int_32.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ class Int32 {
/**
* Create an Int32 type
*
* @param {number|Number} value the number we want to represent as an int32.
* @param {*} value the number we want to represent as an int32.
* @return {Int32}
*/
constructor(value) {
if (value instanceof Number) {
value = value.valueOf();
}

this.value = value;
this.value = +value;
}

/**
Expand Down
20 changes: 19 additions & 1 deletion test/node/extended_json_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ describe('Extended JSON', function() {
expect(result.double.value).to.equal(10.1);
// int32
expect(result.int32).to.be.an.instanceOf(BSON.Int32);
expect(result.int32.value).to.equal('10');
expect(result.int32.value).to.equal(10);
//long
expect(result.long).to.be.an.instanceOf(BSON.Long);
// maxKey
Expand Down Expand Up @@ -642,6 +642,24 @@ describe('Extended JSON', function() {
const bson = EJSON.parse('{"field":1}', { legacy: true });
expect(bson).to.deep.equal(doc);
});

it('parses the numberInt without doc', function() {
const value = 1;
const bson = EJSON.parse('{ "$numberInt": "1" }');
expect(bson).to.deep.equal(value);
});

it('parses the numberInt', function() {
const doc = { field: 1 };
const bson = EJSON.parse('{"field": {"$numberInt": "1"}}');
expect(bson).to.deep.equal(doc);
});

it('parses the numberInt and stringify', function() {
const doc = { field: 1 };
const bson = EJSON.parse('{"field": {"$numberInt": "1"}}');
expect(EJSON.stringify(bson)).to.deep.equal(JSON.stringify(doc));
});
});

context('when deserializing double', function() {
Expand Down
42 changes: 40 additions & 2 deletions test/node/int_32_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ const Int32 = BSON.Int32;
const expect = require('chai').expect;

describe('Int32', function() {
describe('Constructor', function() {
var value = 42;
context('Constructor', function() {
const strHexValue = '0x2a';
const hexValue = 0x2a;
const octalValue = 0o52;
const value = 42;

it('Primitive number', function(done) {
expect(new Int32(value).valueOf()).to.equal(value);
Expand All @@ -17,5 +20,40 @@ describe('Int32', function() {
expect(new Int32(new Number(value)).valueOf()).to.equal(value);
done();
});

it('String Hex', function(done) {
expect(new Int32(strHexValue).valueOf()).to.equal(value);
done();
});

it('Hex', function(done) {
expect(new Int32(hexValue).valueOf()).to.equal(value);
done();
});

it('Octal', function(done) {
expect(new Int32(octalValue).valueOf()).to.equal(value);
done();
});

it('should equal zero', function() {
const prop = 'key';
const zero = BSON.serialize({ [prop]: new Int32(0) }).toString();
// should equal zero
['fortyTwo', '42fortyTwo', '0', 0, Infinity, 'Infinity'].forEach(value => {
expect(BSON.serialize({ [prop]: new Int32(value) }).toString()).to.equal(zero);
expect(BSON.serialize({ [prop]: new Int32(+value) }).toString()).to.equal(zero);
});
});

it('should equal fortyTwo', function() {
const prop = 'key';
const fortyTwo = BSON.serialize({ [prop]: new Int32(value) }).toString();
// should equal fortyTwo
[strHexValue, hexValue, octalValue].forEach(value => {
expect(BSON.serialize({ [prop]: new Int32(value) }).toString()).to.equal(fortyTwo);
expect(BSON.serialize({ [prop]: new Int32(+value) }).toString()).to.equal(fortyTwo);
});
});
});
});

0 comments on commit 5cda40f

Please sign in to comment.