Skip to content

Commit 9671773

Browse files
authored
fix(NODE-3962): correct type for ObjectiId._bsontype (#480)
1 parent 8305bdf commit 9671773

File tree

4 files changed

+96
-2
lines changed

4 files changed

+96
-2
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/objectid.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const kId = Symbol('id');
2828
* @public
2929
*/
3030
export class ObjectId {
31-
_bsontype!: 'ObjectId';
31+
_bsontype!: 'ObjectID';
3232

3333
/** @internal */
3434
static index = Math.floor(Math.random() * 0xffffff);

test/node/type_identifier_tests.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'use strict';
2+
3+
const {
4+
Binary,
5+
Code,
6+
DBRef,
7+
Decimal128,
8+
Double,
9+
Int32,
10+
Long,
11+
MaxKey,
12+
MinKey,
13+
ObjectId,
14+
BSONRegExp,
15+
BSONSymbol,
16+
Timestamp,
17+
UUID
18+
} = require('../register-bson');
19+
20+
describe('_bsontype identifier', () => {
21+
// The two out of the norm types:
22+
it('should be equal to ObjectID for ObjectId', () => {
23+
expect(ObjectId.prototype._bsontype).to.equal('ObjectID');
24+
});
25+
it('should be equal to Symbol for BSONSymbol', () => {
26+
expect(BSONSymbol.prototype._bsontype).to.equal('Symbol');
27+
});
28+
it('should be equal to Timestamp for Timestamp', () => {
29+
// TODO(NODE-2624): Make Timestamp hold its long value on a property rather than be a subclass
30+
// Timestamp overrides the value in its constructor
31+
const timestamp = new Timestamp({ i: 0, t: 0 });
32+
expect(timestamp._bsontype).to.equal('Timestamp');
33+
expect(Object.getPrototypeOf(timestamp)._bsontype).to.equal('Long');
34+
});
35+
36+
// All equal to their constructor names
37+
it('should be equal to Binary for Binary', () => {
38+
expect(Binary.prototype._bsontype).to.equal('Binary');
39+
});
40+
it('should be equal to Code for Code', () => {
41+
expect(Code.prototype._bsontype).to.equal('Code');
42+
});
43+
it('should be equal to DBRef for DBRef', () => {
44+
expect(DBRef.prototype._bsontype).to.equal('DBRef');
45+
});
46+
it('should be equal to Decimal128 for Decimal128', () => {
47+
expect(Decimal128.prototype._bsontype).to.equal('Decimal128');
48+
});
49+
it('should be equal to Double for Double', () => {
50+
expect(Double.prototype._bsontype).to.equal('Double');
51+
});
52+
it('should be equal to Int32 for Int32', () => {
53+
expect(Int32.prototype._bsontype).to.equal('Int32');
54+
});
55+
it('should be equal to Long for Long', () => {
56+
expect(Long.prototype._bsontype).to.equal('Long');
57+
});
58+
it('should be equal to MaxKey for MaxKey', () => {
59+
expect(MaxKey.prototype._bsontype).to.equal('MaxKey');
60+
});
61+
it('should be equal to MinKey for MinKey', () => {
62+
expect(MinKey.prototype._bsontype).to.equal('MinKey');
63+
});
64+
it('should be equal to BSONRegExp for BSONRegExp', () => {
65+
expect(BSONRegExp.prototype._bsontype).to.equal('BSONRegExp');
66+
});
67+
it('should be equal to UUID for UUID', () => {
68+
expect(UUID.prototype._bsontype).to.equal('UUID');
69+
});
70+
});

test/types/bson.test-d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,27 @@ expectError(MaxKey.prototype.toJSON);
5050
expectError(MinKey.prototype.toJSON);
5151
expectError(Long.prototype.toJSON);
5252
expectError(BSONRegExp.prototype.toJSON);
53+
54+
// ObjectID uses a capital for backwards compatibility
55+
expectType<'ObjectID'>(ObjectId.prototype._bsontype)
56+
// BSONSymbol was renamed to not conflict with the global JS Symbol
57+
// but its _bsontype is still 'Symbol'
58+
expectType<'Symbol'>(BSONSymbol.prototype._bsontype)
59+
60+
// We hack TS to say that the prototype has _bsontype='Timestamp'
61+
// but it actually is _bsontype='Long', inside the Timestamp constructor
62+
// we override the property on the instance
63+
// TODO(NODE-2624): Make Timestamp hold its long value on a property rather than be a subclass
64+
expectType<'Timestamp'>(Timestamp.prototype._bsontype)
65+
66+
expectType<'Binary'>(Binary.prototype._bsontype)
67+
expectType<'Code'>(Code.prototype._bsontype)
68+
expectType<'DBRef'>(DBRef.prototype._bsontype)
69+
expectType<'Decimal128'>(Decimal128.prototype._bsontype)
70+
expectType<'Double'>(Double.prototype._bsontype)
71+
expectType<'Int32'>(Int32.prototype._bsontype)
72+
expectType<'Long'>(Long.prototype._bsontype)
73+
expectType<'MaxKey'>(MaxKey.prototype._bsontype)
74+
expectType<'MinKey'>(MinKey.prototype._bsontype)
75+
expectType<'BSONRegExp'>(BSONRegExp.prototype._bsontype)
76+
expectType<'UUID'>(UUID.prototype._bsontype)

0 commit comments

Comments
 (0)