Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-3962): correct type for ObjectiId._bsontype #480

Merged
merged 1 commit into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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)