Skip to content

Commit 4bed500

Browse files
authored
fix(shell-api): include bson constructor static methods MONGOSH-880 (#998)
1 parent 7edb5c2 commit 4bed500

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

packages/shell-api/src/shell-bson.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ describe('Shell BSON', () => {
116116
expect(s._bsontype).to.equal('ObjectID');
117117
expect(s.toHexString().slice(0, 8)).to.equal('12345678');
118118
});
119+
it('can be created through createFromTime', () => {
120+
const s = (shellBson.ObjectId as any).createFromTime(0x12345678);
121+
expect(s._bsontype).to.equal('ObjectID');
122+
expect(s.toHexString().slice(0, 8)).to.equal('12345678');
123+
});
119124
it('has help and other metadata', async() => {
120125
const s = shellBson.ObjectId();
121126
expect((await toShellResult(s.help)).type).to.equal('Help');
@@ -584,4 +589,29 @@ describe('Shell BSON', () => {
584589
expect(input).to.deep.equal(output);
585590
});
586591
});
592+
593+
describe('BSON constructor properties', () => {
594+
it('matches original BSON constructor properties', () => {
595+
for (const key of Object.keys(bson)) {
596+
if (!(key in shellBson) || bson[key] === shellBson[key]) {
597+
continue;
598+
}
599+
600+
const bsonProperties = Object.getOwnPropertyDescriptors(bson[key]);
601+
const shellProperties = Object.getOwnPropertyDescriptors(shellBson[key]);
602+
delete shellProperties.help; // Not expected from the original BSON.
603+
delete bsonProperties.get_inc; // Deprecated.
604+
delete shellProperties.length; // Function length can vary depending on the specific arguments in TS.
605+
delete bsonProperties.length;
606+
delete shellProperties.index; // ObjectId.index is a random number
607+
delete bsonProperties.index; // ObjectId.index is a random number
608+
try {
609+
expect(shellProperties).to.deep.equal(bsonProperties);
610+
} catch (err) {
611+
err.message += ` (${key})`;
612+
throw err;
613+
}
614+
}
615+
});
616+
});
587617
});

packages/shell-api/src/shell-bson.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ALL_PLATFORMS, ALL_SERVER_VERSIONS, ALL_TOPOLOGIES, ServerVersions } from './enums';
22
import Help from './help';
3-
import { BinaryType, bson as BSON } from '@mongosh/service-provider-core';
3+
import { BinaryType, Document, bson as BSON } from '@mongosh/service-provider-core';
44
import { CommonErrors, MongoshInternalError, MongoshInvalidInputError } from '@mongosh/errors';
55
import { assertArgsDefinedType } from './helpers';
66
import { randomBytes } from 'crypto';
@@ -51,34 +51,34 @@ export default function constructShellBson(bson: typeof BSON, printWarning: (msg
5151
(bson.BSONSymbol as any).prototype.deprecated = true;
5252

5353
const bsonPkg = {
54-
DBRef: Object.assign(function(namespace: string, oid: any, db?: string): typeof bson.DBRef.prototype {
55-
assertArgsDefinedType([namespace, oid, db], ['string', true, [undefined, 'string']], 'DBRef');
56-
return new bson.DBRef(namespace, oid, db);
57-
}, { prototype: bson.DBRef.prototype }),
54+
DBRef: Object.assign(function DBRef(namespace: string, oid: any, db?: string, fields?: Document): typeof bson.DBRef.prototype {
55+
assertArgsDefinedType([namespace, oid, db], ['string', true, [undefined, 'string'], [undefined, 'object']], 'DBRef');
56+
return new bson.DBRef(namespace, oid, db, fields);
57+
}, { ...bson.DBRef, prototype: bson.DBRef.prototype }),
5858
// DBPointer not available in the bson 1.x library, but depreciated since 1.6
5959
Map: bson.Map,
60-
bsonsize: function(object: any): number {
60+
bsonsize: function bsonsize(object: any): number {
6161
assertArgsDefinedType([object], ['object'], 'bsonsize');
6262
return bson.calculateObjectSize(object);
6363
},
64-
MaxKey: Object.assign(function(): typeof bson.MaxKey.prototype {
64+
MaxKey: Object.assign(function MaxKey(): typeof bson.MaxKey.prototype {
6565
return new bson.MaxKey();
66-
}, { prototype: bson.MaxKey.prototype }),
67-
MinKey: Object.assign(function(): typeof bson.MinKey.prototype {
66+
}, { ...bson.MaxKey, prototype: bson.MaxKey.prototype }),
67+
MinKey: Object.assign(function MinKey(): typeof bson.MinKey.prototype {
6868
return new bson.MinKey();
69-
}, { prototype: bson.MinKey.prototype }),
70-
ObjectId: Object.assign(function(id?: string | number | typeof bson.ObjectId.prototype | Buffer): typeof bson.ObjectId.prototype {
69+
}, { ...bson.MinKey, prototype: bson.MinKey.prototype }),
70+
ObjectId: Object.assign(function ObjectId(id?: string | number | typeof bson.ObjectId.prototype | Buffer): typeof bson.ObjectId.prototype {
7171
assertArgsDefinedType([id], [[undefined, 'string', 'number', 'object']], 'ObjectId');
7272
return new bson.ObjectId(id);
73-
}, { prototype: bson.ObjectId.prototype }),
74-
Timestamp: Object.assign(function(low?: number | typeof bson.Long.prototype, high?: number): typeof bson.Timestamp.prototype {
73+
}, { ...bson.ObjectId, prototype: bson.ObjectId.prototype }),
74+
Timestamp: Object.assign(function Timestamp(low?: number | typeof bson.Long.prototype, high?: number): typeof bson.Timestamp.prototype {
7575
assertArgsDefinedType([low, high], [['number', 'object', undefined], [undefined, 'number']], 'Timestamp');
7676
return new bson.Timestamp(low as number, high as number);
77-
}, { prototype: bson.Timestamp.prototype }),
78-
Code: Object.assign(function(c: string | Function = '', s?: any): typeof bson.Code.prototype {
77+
}, { ...bson.Timestamp, prototype: bson.Timestamp.prototype }),
78+
Code: Object.assign(function Code(c: string | Function = '', s?: any): typeof bson.Code.prototype {
7979
assertArgsDefinedType([c, s], [[undefined, 'string', 'function'], [undefined, 'object']], 'Code');
8080
return new bson.Code(c, s);
81-
}, { prototype: bson.Code.prototype }),
81+
}, { ...bson.Code, prototype: bson.Code.prototype }),
8282
NumberDecimal: Object.assign(function(s = '0'): any {
8383
assertArgsDefinedType([s], [['string', 'number']], 'NumberDecimal');
8484
if (typeof s === 'string') {

0 commit comments

Comments
 (0)