@@ -2284,4 +2284,50 @@ describe('BSON', function() {
2284
2284
expect ( false ) . to . equal ( id . equals ( undefined ) ) ;
2285
2285
done ( ) ;
2286
2286
} ) ;
2287
+
2288
+ it ( 'should serialize ObjectIds from old bson versions' , function ( ) {
2289
+ // create a wrapper simulating the old ObjectID class
2290
+ class ObjectID {
2291
+ constructor ( ) {
2292
+ this . oid = new ObjectId ( ) ;
2293
+ }
2294
+ get id ( ) {
2295
+ return this . oid . id ;
2296
+ }
2297
+ toString ( ) {
2298
+ return this . oid . toString ( ) ;
2299
+ }
2300
+ }
2301
+ Object . defineProperty ( ObjectID . prototype , '_bsontype' , { value : 'ObjectID' } ) ;
2302
+
2303
+ // Array
2304
+ const array = [ new ObjectID ( ) , new ObjectId ( ) ] ;
2305
+ const deserializedArrayAsMap = BSON . deserialize ( BSON . serialize ( array ) ) ;
2306
+ const deserializedArray = Object . keys ( deserializedArrayAsMap ) . map (
2307
+ x => deserializedArrayAsMap [ x ]
2308
+ ) ;
2309
+ expect ( deserializedArray . map ( x => x . toString ( ) ) ) . to . eql ( array . map ( x => x . toString ( ) ) ) ;
2310
+
2311
+ // Map
2312
+ const map = new Map ( ) ;
2313
+ map . set ( 'oldBsonType' , new ObjectID ( ) ) ;
2314
+ map . set ( 'newBsonType' , new ObjectId ( ) ) ;
2315
+ const deserializedMapAsObject = BSON . deserialize ( BSON . serialize ( map ) , { relaxed : false } ) ;
2316
+ const deserializedMap = new Map (
2317
+ Object . keys ( deserializedMapAsObject ) . map ( k => [ k , deserializedMapAsObject [ k ] ] )
2318
+ ) ;
2319
+
2320
+ map . forEach ( ( value , key ) => {
2321
+ expect ( deserializedMap . has ( key ) ) . to . be . true ;
2322
+ const deserializedMapValue = deserializedMap . get ( key ) ;
2323
+ expect ( deserializedMapValue . toString ( ) ) . to . equal ( value . toString ( ) ) ;
2324
+ } ) ;
2325
+
2326
+ // Object
2327
+ const record = { oldBsonType : new ObjectID ( ) , newBsonType : new ObjectId ( ) } ;
2328
+ const deserializedObject = BSON . deserialize ( BSON . serialize ( record ) ) ;
2329
+ expect ( deserializedObject ) . to . have . keys ( [ 'oldBsonType' , 'newBsonType' ] ) ;
2330
+ expect ( record . oldBsonType . toString ( ) ) . to . equal ( deserializedObject . oldBsonType . toString ( ) ) ;
2331
+ expect ( record . newBsonType . toString ( ) ) . to . equal ( deserializedObject . newBsonType . toString ( ) ) ;
2332
+ } ) ;
2287
2333
} ) ;
0 commit comments