Skip to content

Commit ebc1c76

Browse files
fix(NODE-4887): serializeInto does not check for the presence of a toBSON method for values in Map entries (#555)
Co-authored-by: Bailey Pearson <[email protected]>
1 parent 3b4b61e commit ebc1c76

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

Diff for: src/parser/serializer.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,11 @@ export function serializeInto(
757757

758758
// Get the entry values
759759
const key = entry.value[0];
760-
const value = entry.value[1];
760+
let value = entry.value[1];
761+
762+
if (typeof value?.toBSON === 'function') {
763+
value = value.toBSON();
764+
}
761765

762766
// Check the type of the value
763767
const type = typeof value;

Diff for: test/node/to_bson_test.js

+9
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ describe('toBSON', function () {
183183
const sizeNestedToBSON = BSON.calculateObjectSize({ a: [0] });
184184
expect(sizeNestedToBSON).to.equal(33);
185185
});
186+
187+
it('uses toBSON on values contained in a map', () => {
188+
const map = new Map();
189+
map.set('a', 100);
190+
191+
const serializedData = BSON.serialize(map);
192+
const deserializedData = BSON.deserialize(serializedData);
193+
expect(deserializedData).to.have.property('a', 'hello number');
194+
});
186195
});
187196

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

0 commit comments

Comments
 (0)