Skip to content

Commit 616665f

Browse files
authored
fix: make inspection result of BSON types evaluable (#416)
Make sure that evaluating the inspection result of a BSON type returns a result that can be evaluated to give an object equivalent to the original. NODE-2947
1 parent b707f65 commit 616665f

File tree

14 files changed

+30
-24
lines changed

14 files changed

+30
-24
lines changed

src/binary.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ export class Binary {
263263

264264
inspect(): string {
265265
const asBuffer = this.value(true);
266-
return `Binary("${asBuffer.toString('hex')}", ${this.sub_type})`;
266+
return `new Binary(Buffer.from("${asBuffer.toString('hex')}", "hex"), ${this.sub_type})`;
267267
}
268268
}
269269

src/code.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export class Code {
5050

5151
inspect(): string {
5252
const codeJson = this.toJSON();
53-
return `Code("${codeJson.code}"${codeJson.scope ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
53+
return `new Code("${codeJson.code}"${
54+
codeJson.scope ? `, ${JSON.stringify(codeJson.scope)}` : ''
55+
})`;
5456
}
5557
}
5658

src/db_ref.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ export class DBRef {
108108
// NOTE: if OID is an ObjectId class it will just print the oid string.
109109
const oid =
110110
this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
111-
return `DBRef("${this.namespace}", "${oid}"${this.db ? `, "${this.db}"` : ''})`;
111+
return `new DBRef("${this.namespace}", new ObjectId("${oid}")${
112+
this.db ? `, "${this.db}"` : ''
113+
})`;
112114
}
113115
}
114116

src/decimal128.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ export class Decimal128 {
794794
}
795795

796796
inspect(): string {
797-
return `Decimal128("${this.toString()}")`;
797+
return `Decimal128.fromString("${this.toString()}")`;
798798
}
799799
}
800800

src/double.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class Double {
7878

7979
inspect(): string {
8080
const eJSON = this.toExtendedJSON() as DoubleExtended;
81-
return `Double(${eJSON.$numberDouble})`;
81+
return `new Double(${eJSON.$numberDouble})`;
8282
}
8383
}
8484

src/int_32.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class Int32 {
5757
}
5858

5959
inspect(): string {
60-
return `Int32(${this.valueOf()})`;
60+
return `new Int32(${this.valueOf()})`;
6161
}
6262
}
6363

src/long.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ export class Long {
992992
}
993993

994994
inspect(): string {
995-
return `Long("${this.toString()}")`;
995+
return `new Long("${this.toString()}")`;
996996
}
997997
}
998998

src/max_key.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class MaxKey {
2626
}
2727

2828
inspect(): string {
29-
return 'MaxKey()';
29+
return 'new MaxKey()';
3030
}
3131
}
3232

src/min_key.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class MinKey {
2626
}
2727

2828
inspect(): string {
29-
return 'MinKey()';
29+
return 'new MinKey()';
3030
}
3131
}
3232

src/objectid.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ export class ObjectId {
350350
}
351351

352352
inspect(): string {
353-
return `ObjectId("${this.toHexString()}")`;
353+
return `new ObjectId("${this.toHexString()}")`;
354354
}
355355
}
356356

src/symbol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class BSONSymbol {
3030

3131
/** @internal */
3232
inspect(): string {
33-
return `BSONSymbol("${this.value}")`;
33+
return `new BSONSymbol("${this.value}")`;
3434
}
3535

3636
/** @internal */

src/timestamp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,6 @@ export class Timestamp extends LongWithoutOverridesClass {
9898
}
9999

100100
inspect(): string {
101-
return `Timestamp(${this.getLowBits().toString()}, ${this.getHighBits().toString()})`;
101+
return `new Timestamp(${this.getLowBits().toString()}, ${this.getHighBits().toString()})`;
102102
}
103103
}

test/node/bson_test.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,23 +2276,25 @@ describe('BSON', function () {
22762276
*/
22772277
it('Binary', function () {
22782278
const binary = new Binary(Buffer.from('0123456789abcdef0123456789abcdef', 'hex'), 4);
2279-
expect(inspect(binary)).to.equal('Binary("0123456789abcdef0123456789abcdef", 4)');
2279+
expect(inspect(binary)).to.equal(
2280+
'new Binary(Buffer.from("0123456789abcdef0123456789abcdef", "hex"), 4)'
2281+
);
22802282
});
22812283

22822284
/**
22832285
* @ignore
22842286
*/
22852287
it('BSONSymbol', function () {
22862288
const symbol = new BSONSymbol('sym');
2287-
expect(inspect(symbol)).to.equal('BSONSymbol("sym")');
2289+
expect(inspect(symbol)).to.equal('new BSONSymbol("sym")');
22882290
});
22892291

22902292
/**
22912293
* @ignore
22922294
*/
22932295
it('Code', function () {
22942296
const code = new Code('this.a > i', { i: 1 });
2295-
expect(inspect(code)).to.equal('Code("this.a > i", {"i":1})');
2297+
expect(inspect(code)).to.equal('new Code("this.a > i", {"i":1})');
22962298
});
22972299

22982300
/**
@@ -2302,7 +2304,7 @@ describe('BSON', function () {
23022304
const oid = new ObjectId('deadbeefdeadbeefdeadbeef');
23032305
const dbref = new DBRef('namespace', oid, 'integration_tests_');
23042306
expect(inspect(dbref)).to.equal(
2305-
'DBRef("namespace", "deadbeefdeadbeefdeadbeef", "integration_tests_")'
2307+
'new DBRef("namespace", new ObjectId("deadbeefdeadbeefdeadbeef"), "integration_tests_")'
23062308
);
23072309
});
23082310

@@ -2311,55 +2313,55 @@ describe('BSON', function () {
23112313
*/
23122314
it('Decimal128', function () {
23132315
const dec = Decimal128.fromString('1.42');
2314-
expect(inspect(dec)).to.equal('Decimal128("1.42")');
2316+
expect(inspect(dec)).to.equal('Decimal128.fromString("1.42")');
23152317
});
23162318

23172319
/**
23182320
* @ignore
23192321
*/
23202322
it('Double', function () {
23212323
const double = new Double(-42.42);
2322-
expect(inspect(double)).to.equal('Double(-42.42)');
2324+
expect(inspect(double)).to.equal('new Double(-42.42)');
23232325
});
23242326

23252327
/**
23262328
* @ignore
23272329
*/
23282330
it('Int32', function () {
23292331
const int = new Int32(42);
2330-
expect(inspect(int)).to.equal('Int32(42)');
2332+
expect(inspect(int)).to.equal('new Int32(42)');
23312333
});
23322334

23332335
/**
23342336
* @ignore
23352337
*/
23362338
it('Long', function () {
23372339
const long = Long.fromString('42');
2338-
expect(inspect(long)).to.equal('Long("42")');
2340+
expect(inspect(long)).to.equal('new Long("42")');
23392341
});
23402342

23412343
/**
23422344
* @ignore
23432345
*/
23442346
it('MaxKey', function () {
23452347
const maxKey = new MaxKey();
2346-
expect(inspect(maxKey)).to.equal('MaxKey()');
2348+
expect(inspect(maxKey)).to.equal('new MaxKey()');
23472349
});
23482350

23492351
/**
23502352
* @ignore
23512353
*/
23522354
it('MinKey', function () {
23532355
const minKey = new MinKey();
2354-
expect(inspect(minKey)).to.equal('MinKey()');
2356+
expect(inspect(minKey)).to.equal('new MinKey()');
23552357
});
23562358

23572359
/**
23582360
* @ignore
23592361
*/
23602362
it('Timestamp', function () {
23612363
const timestamp = new Timestamp(1, 100);
2362-
expect(inspect(timestamp)).to.equal('Timestamp(1, 100)');
2364+
expect(inspect(timestamp)).to.equal('new Timestamp(1, 100)');
23632365
});
23642366
});
23652367
});

test/node/object_id_tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('ObjectId', function () {
6666
it('should correctly allow for node.js inspect to work with ObjectId', function (done) {
6767
var a = 'AAAAAAAAAAAAAAAAAAAAAAAA';
6868
var b = new ObjectId(a);
69-
expect(util.inspect(b)).to.equal('ObjectId("aaaaaaaaaaaaaaaaaaaaaaaa")');
69+
expect(util.inspect(b)).to.equal('new ObjectId("aaaaaaaaaaaaaaaaaaaaaaaa")');
7070

7171
done();
7272
});

0 commit comments

Comments
 (0)