Skip to content

Commit 8511225

Browse files
nbbeekendurran
andauthored
feat!(NODE-4710): remove capital D ObjectID export (#528)
Co-authored-by: Durran Jordan <[email protected]>
1 parent 196f9f8 commit 8511225

File tree

7 files changed

+70
-13
lines changed

7 files changed

+70
-13
lines changed

docs/upgrade-to-v5.md

+4
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,7 @@ Now `-0` can be used directly
130130
BSON.deserialize(BSON.serialize({ d: -0 }))
131131
// type preservation, returns { d: -0 }
132132
```
133+
134+
### Capital "D" ObjectID export removed
135+
136+
For clarity the deprecated and duplicate export `ObjectID` has been removed. `ObjectId` matches the class name and is equal in every way to the capital "D" export.

src/bson.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ export {
4949
MinKey,
5050
MaxKey,
5151
BSONRegExp,
52-
Decimal128,
53-
// In 4.0.0 and 4.0.1, this property name was changed to ObjectId to match the class name.
54-
// This caused interoperability problems with previous versions of the library, so in
55-
// later builds we changed it back to ObjectID (capital D) to match legacy implementations.
56-
ObjectId as ObjectID
52+
Decimal128
5753
};
5854
export { BSONError, BSONTypeError } from './error';
5955
export { BSONType } from './constants';
@@ -273,7 +269,6 @@ const BSON = {
273269
MaxKey,
274270
MinKey,
275271
ObjectId,
276-
ObjectID: ObjectId,
277272
BSONRegExp,
278273
BSONSymbol,
279274
Timestamp,

src/extended_json.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,10 @@ const BSON_TYPE_MAPPINGS = {
273273
MaxKey: () => new MaxKey(),
274274
MinKey: () => new MinKey(),
275275
ObjectID: (o: ObjectId) => new ObjectId(o),
276-
ObjectId: (o: ObjectId) => new ObjectId(o), // support 4.0.0/4.0.1 before _bsontype was reverted back to ObjectID
276+
// The _bsontype for ObjectId is spelled with a capital "D", to the mapping above will be used (most of the time)
277+
// specifically BSON versions 4.0.0 and 4.0.1 the _bsontype was changed to "ObjectId" so we keep this mapping to support
278+
// those version of BSON
279+
ObjectId: (o: ObjectId) => new ObjectId(o),
277280
BSONRegExp: (o: BSONRegExp) => new BSONRegExp(o.pattern, o.options),
278281
Symbol: (o: BSONSymbol) => new BSONSymbol(o.value),
279282
Timestamp: (o: Timestamp) => Timestamp.fromBits(o.low, o.high)

test/node/exports.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const EXPECTED_EXPORTS = [
2222
'MaxKey',
2323
'BSONRegExp',
2424
'Decimal128',
25-
'ObjectID',
2625
'BSONError',
2726
'BSONTypeError',
2827
'setInternalBufferSize',

test/node/extended_json.test.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as BSON from '../register-bson';
22
const EJSON = BSON.EJSON;
33
import * as vm from 'node:vm';
4+
import { expect } from 'chai';
45

56
// BSON types
67
const Binary = BSON.Binary;
@@ -13,7 +14,7 @@ const Int32 = BSON.Int32;
1314
const Long = BSON.Long;
1415
const MaxKey = BSON.MaxKey;
1516
const MinKey = BSON.MinKey;
16-
const ObjectID = BSON.ObjectID;
17+
const ObjectID = BSON.ObjectId;
1718
const ObjectId = BSON.ObjectId;
1819
const BSONRegExp = BSON.BSONRegExp;
1920
const BSONSymbol = BSON.BSONSymbol;
@@ -365,14 +366,14 @@ describe('Extended JSON', function () {
365366
binary: new bsonModule.Binary(buffer),
366367
code: new bsonModule.Code('function() {}'),
367368
dbRef: new bsonModule.DBRef('tests', new Int32(1), 'test'),
368-
decimal128: new bsonModule.Decimal128.fromString('9991223372036854775807'),
369+
decimal128: bsonModule.Decimal128.fromString('9991223372036854775807'),
369370
double: new bsonModule.Double(10.1),
370371
int32: new bsonModule.Int32(10),
371-
long: new bsonModule.Long.fromString('1223372036854775807'),
372+
long: bsonModule.Long.fromString('1223372036854775807'),
372373
maxKey: new bsonModule.MaxKey(),
373374
// minKey: new bsonModule.MinKey(), // broken until #310 is fixed in 1.x
374375
objectId: bsonModule.ObjectId.createFromHexString('111111111111111111111111'),
375-
objectID: bsonModule.ObjectID.createFromHexString('111111111111111111111111'),
376+
objectID: bsonModule.ObjectId.createFromHexString('111111111111111111111111'),
376377
bsonRegExp: new bsonModule.BSONRegExp('hello world', 'i'),
377378
symbol: bsonModule.BSONSymbol
378379
? new bsonModule.BSONSymbol('symbol')

test/node/object_id_tests.js

+55
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
const Buffer = require('buffer').Buffer;
44
const BSON = require('../register-bson');
5+
const EJSON = BSON.EJSON;
56
const BSONTypeError = BSON.BSONTypeError;
67
const ObjectId = BSON.ObjectId;
78
const util = require('util');
9+
const { expect } = require('chai');
10+
const { bufferFromHexArray } = require('./tools/utils');
811
const getSymbolFrom = require('./tools/utils').getSymbolFrom;
912
const isBufferOrUint8Array = require('./tools/utils').isBufferOrUint8Array;
1013

@@ -28,6 +31,58 @@ describe('ObjectId', function () {
2831
});
2932
});
3033

34+
describe('_bsontype casing cross compatibility', () => {
35+
it('EJSON stringify understands capital or lowercase D _bsontype', () => {
36+
const resultFromCapitalD = EJSON.stringify(
37+
{ a: new ObjectId('00'.repeat(12)) },
38+
{ relaxed: false }
39+
);
40+
const resultFromLowercaseD = EJSON.stringify(
41+
{
42+
a: new (class extends ObjectId {
43+
get _bsontype() {
44+
return 'ObjectId';
45+
}
46+
})('00'.repeat(12))
47+
},
48+
{ relaxed: false }
49+
);
50+
51+
expect(JSON.parse(resultFromCapitalD))
52+
.to.have.property('a')
53+
.that.deep.equals({ $oid: '00'.repeat(12) });
54+
expect(JSON.parse(resultFromLowercaseD))
55+
.to.have.property('a')
56+
.that.deep.equals({ $oid: '00'.repeat(12) });
57+
});
58+
59+
it('EJSON stringify understands capital or lowercase D _bsontype', () => {
60+
const resultFromCapitalD = BSON.serialize(
61+
{ a: new ObjectId('00'.repeat(12)) },
62+
{ relaxed: false }
63+
);
64+
const resultFromLowercaseD = BSON.serialize(
65+
{
66+
a: new (class extends ObjectId {
67+
get _bsontype() {
68+
return 'ObjectId';
69+
}
70+
})('00'.repeat(12))
71+
},
72+
{ relaxed: false }
73+
);
74+
75+
const expectedBytes = bufferFromHexArray([
76+
'07', // oid type
77+
'6100', // 'a\x00'
78+
'00'.repeat(12) // oid bytes
79+
]);
80+
81+
expect(resultFromCapitalD).to.deep.equal(expectedBytes);
82+
expect(resultFromLowercaseD).to.deep.equal(expectedBytes);
83+
});
84+
});
85+
3186
it('creates an objectId with user defined value in the timestamp field', function () {
3287
const a = ObjectId.createFromTime(1);
3388
expect(a.id.slice(0, 4)).to.deep.equal(Buffer.from([0, 0, 0, 1]));

test/types/bson.test-d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ expectError(MinKey.prototype.toJSON);
5151
expectError(Long.prototype.toJSON);
5252
expectError(BSONRegExp.prototype.toJSON);
5353

54-
// ObjectID uses a capital for backwards compatibility
54+
// ObjectID uses a capital "D", this does not relate to the class name, or export name, only the determination for serialization
5555
expectType<'ObjectID'>(ObjectId.prototype._bsontype)
5656
// BSONSymbol was renamed to not conflict with the global JS Symbol
5757
// but its _bsontype is still 'Symbol'

0 commit comments

Comments
 (0)