Skip to content

Commit

Permalink
fix(NODE-4887): serializeInto does not check for the presence of a to…
Browse files Browse the repository at this point in the history
…BSON method for values in Map entries (#555)

Co-authored-by: Bailey Pearson <bailey.pearson@mongodb.com>
  • Loading branch information
W-A-James and baileympearson authored Jan 9, 2023
1 parent 3b4b61e commit ebc1c76
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/parser/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,11 @@ export function serializeInto(

// Get the entry values
const key = entry.value[0];
const value = entry.value[1];
let value = entry.value[1];

if (typeof value?.toBSON === 'function') {
value = value.toBSON();
}

// Check the type of the value
const type = typeof value;
Expand Down
9 changes: 9 additions & 0 deletions test/node/to_bson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ describe('toBSON', function () {
const sizeNestedToBSON = BSON.calculateObjectSize({ a: [0] });
expect(sizeNestedToBSON).to.equal(33);
});

it('uses toBSON on values contained in a map', () => {
const map = new Map();
map.set('a', 100);

const serializedData = BSON.serialize(map);
const deserializedData = BSON.deserialize(serializedData);
expect(deserializedData).to.have.property('a', 'hello number');
});
});

it('should use toBSON in calculateObjectSize', () => {
Expand Down

0 comments on commit ebc1c76

Please sign in to comment.