Skip to content

Commit

Permalink
fix(NODE-3962): correct type for ObjectiId._bsontype (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Feb 7, 2022
1 parent 8305bdf commit 9671773
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/objectid.ts
Expand Up @@ -28,7 +28,7 @@ const kId = Symbol('id');
* @public
*/
export class ObjectId {
_bsontype!: 'ObjectId';
_bsontype!: 'ObjectID';

/** @internal */
static index = Math.floor(Math.random() * 0xffffff);
Expand Down
70 changes: 70 additions & 0 deletions test/node/type_identifier_tests.js
@@ -0,0 +1,70 @@
'use strict';

const {
Binary,
Code,
DBRef,
Decimal128,
Double,
Int32,
Long,
MaxKey,
MinKey,
ObjectId,
BSONRegExp,
BSONSymbol,
Timestamp,
UUID
} = require('../register-bson');

describe('_bsontype identifier', () => {
// The two out of the norm types:
it('should be equal to ObjectID for ObjectId', () => {
expect(ObjectId.prototype._bsontype).to.equal('ObjectID');
});
it('should be equal to Symbol for BSONSymbol', () => {
expect(BSONSymbol.prototype._bsontype).to.equal('Symbol');
});
it('should be equal to Timestamp for Timestamp', () => {
// TODO(NODE-2624): Make Timestamp hold its long value on a property rather than be a subclass
// Timestamp overrides the value in its constructor
const timestamp = new Timestamp({ i: 0, t: 0 });
expect(timestamp._bsontype).to.equal('Timestamp');
expect(Object.getPrototypeOf(timestamp)._bsontype).to.equal('Long');
});

// All equal to their constructor names
it('should be equal to Binary for Binary', () => {
expect(Binary.prototype._bsontype).to.equal('Binary');
});
it('should be equal to Code for Code', () => {
expect(Code.prototype._bsontype).to.equal('Code');
});
it('should be equal to DBRef for DBRef', () => {
expect(DBRef.prototype._bsontype).to.equal('DBRef');
});
it('should be equal to Decimal128 for Decimal128', () => {
expect(Decimal128.prototype._bsontype).to.equal('Decimal128');
});
it('should be equal to Double for Double', () => {
expect(Double.prototype._bsontype).to.equal('Double');
});
it('should be equal to Int32 for Int32', () => {
expect(Int32.prototype._bsontype).to.equal('Int32');
});
it('should be equal to Long for Long', () => {
expect(Long.prototype._bsontype).to.equal('Long');
});
it('should be equal to MaxKey for MaxKey', () => {
expect(MaxKey.prototype._bsontype).to.equal('MaxKey');
});
it('should be equal to MinKey for MinKey', () => {
expect(MinKey.prototype._bsontype).to.equal('MinKey');
});
it('should be equal to BSONRegExp for BSONRegExp', () => {
expect(BSONRegExp.prototype._bsontype).to.equal('BSONRegExp');
});
it('should be equal to UUID for UUID', () => {
expect(UUID.prototype._bsontype).to.equal('UUID');
});
});
24 changes: 24 additions & 0 deletions test/types/bson.test-d.ts
Expand Up @@ -50,3 +50,27 @@ expectError(MaxKey.prototype.toJSON);
expectError(MinKey.prototype.toJSON);
expectError(Long.prototype.toJSON);
expectError(BSONRegExp.prototype.toJSON);

// ObjectID uses a capital for backwards compatibility
expectType<'ObjectID'>(ObjectId.prototype._bsontype)
// BSONSymbol was renamed to not conflict with the global JS Symbol
// but its _bsontype is still 'Symbol'
expectType<'Symbol'>(BSONSymbol.prototype._bsontype)

// We hack TS to say that the prototype has _bsontype='Timestamp'
// but it actually is _bsontype='Long', inside the Timestamp constructor
// we override the property on the instance
// TODO(NODE-2624): Make Timestamp hold its long value on a property rather than be a subclass
expectType<'Timestamp'>(Timestamp.prototype._bsontype)

expectType<'Binary'>(Binary.prototype._bsontype)
expectType<'Code'>(Code.prototype._bsontype)
expectType<'DBRef'>(DBRef.prototype._bsontype)
expectType<'Decimal128'>(Decimal128.prototype._bsontype)
expectType<'Double'>(Double.prototype._bsontype)
expectType<'Int32'>(Int32.prototype._bsontype)
expectType<'Long'>(Long.prototype._bsontype)
expectType<'MaxKey'>(MaxKey.prototype._bsontype)
expectType<'MinKey'>(MinKey.prototype._bsontype)
expectType<'BSONRegExp'>(BSONRegExp.prototype._bsontype)
expectType<'UUID'>(UUID.prototype._bsontype)

0 comments on commit 9671773

Please sign in to comment.