Skip to content

Commit 5cda40f

Browse files
author
Thomas Reggi
authored
fix: parse value of Int32 in constructor
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
1 parent c307ca8 commit 5cda40f

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

Diff for: lib/int_32.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ class Int32 {
66
/**
77
* Create an Int32 type
88
*
9-
* @param {number|Number} value the number we want to represent as an int32.
9+
* @param {*} value the number we want to represent as an int32.
1010
* @return {Int32}
1111
*/
1212
constructor(value) {
1313
if (value instanceof Number) {
1414
value = value.valueOf();
1515
}
1616

17-
this.value = value;
17+
this.value = +value;
1818
}
1919

2020
/**

Diff for: test/node/extended_json_tests.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ describe('Extended JSON', function() {
280280
expect(result.double.value).to.equal(10.1);
281281
// int32
282282
expect(result.int32).to.be.an.instanceOf(BSON.Int32);
283-
expect(result.int32.value).to.equal('10');
283+
expect(result.int32.value).to.equal(10);
284284
//long
285285
expect(result.long).to.be.an.instanceOf(BSON.Long);
286286
// maxKey
@@ -642,6 +642,24 @@ describe('Extended JSON', function() {
642642
const bson = EJSON.parse('{"field":1}', { legacy: true });
643643
expect(bson).to.deep.equal(doc);
644644
});
645+
646+
it('parses the numberInt without doc', function() {
647+
const value = 1;
648+
const bson = EJSON.parse('{ "$numberInt": "1" }');
649+
expect(bson).to.deep.equal(value);
650+
});
651+
652+
it('parses the numberInt', function() {
653+
const doc = { field: 1 };
654+
const bson = EJSON.parse('{"field": {"$numberInt": "1"}}');
655+
expect(bson).to.deep.equal(doc);
656+
});
657+
658+
it('parses the numberInt and stringify', function() {
659+
const doc = { field: 1 };
660+
const bson = EJSON.parse('{"field": {"$numberInt": "1"}}');
661+
expect(EJSON.stringify(bson)).to.deep.equal(JSON.stringify(doc));
662+
});
645663
});
646664

647665
context('when deserializing double', function() {

Diff for: test/node/int_32_tests.js

+40-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ const Int32 = BSON.Int32;
55
const expect = require('chai').expect;
66

77
describe('Int32', function() {
8-
describe('Constructor', function() {
9-
var value = 42;
8+
context('Constructor', function() {
9+
const strHexValue = '0x2a';
10+
const hexValue = 0x2a;
11+
const octalValue = 0o52;
12+
const value = 42;
1013

1114
it('Primitive number', function(done) {
1215
expect(new Int32(value).valueOf()).to.equal(value);
@@ -17,5 +20,40 @@ describe('Int32', function() {
1720
expect(new Int32(new Number(value)).valueOf()).to.equal(value);
1821
done();
1922
});
23+
24+
it('String Hex', function(done) {
25+
expect(new Int32(strHexValue).valueOf()).to.equal(value);
26+
done();
27+
});
28+
29+
it('Hex', function(done) {
30+
expect(new Int32(hexValue).valueOf()).to.equal(value);
31+
done();
32+
});
33+
34+
it('Octal', function(done) {
35+
expect(new Int32(octalValue).valueOf()).to.equal(value);
36+
done();
37+
});
38+
39+
it('should equal zero', function() {
40+
const prop = 'key';
41+
const zero = BSON.serialize({ [prop]: new Int32(0) }).toString();
42+
// should equal zero
43+
['fortyTwo', '42fortyTwo', '0', 0, Infinity, 'Infinity'].forEach(value => {
44+
expect(BSON.serialize({ [prop]: new Int32(value) }).toString()).to.equal(zero);
45+
expect(BSON.serialize({ [prop]: new Int32(+value) }).toString()).to.equal(zero);
46+
});
47+
});
48+
49+
it('should equal fortyTwo', function() {
50+
const prop = 'key';
51+
const fortyTwo = BSON.serialize({ [prop]: new Int32(value) }).toString();
52+
// should equal fortyTwo
53+
[strHexValue, hexValue, octalValue].forEach(value => {
54+
expect(BSON.serialize({ [prop]: new Int32(value) }).toString()).to.equal(fortyTwo);
55+
expect(BSON.serialize({ [prop]: new Int32(+value) }).toString()).to.equal(fortyTwo);
56+
});
57+
});
2058
});
2159
});

0 commit comments

Comments
 (0)