Skip to content
New issue

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

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

Already on GitHub? # to your account

fix(NODE-4887): serializeInto does not check for the presence of a toBSON method for values in Map entries #555

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
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