From 931c59f11f607e2d372a5416c2ac8a728f3f48b5 Mon Sep 17 00:00:00 2001 From: Warren James Date: Thu, 5 Jan 2023 15:30:07 -0500 Subject: [PATCH 1/3] fix(NODE-4887): WIP fix for maps --- src/parser/serializer.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/parser/serializer.ts b/src/parser/serializer.ts index baf3fe9b..076bd3eb 100644 --- a/src/parser/serializer.ts +++ b/src/parser/serializer.ts @@ -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; From 6de6fdb409a4329441d287b5b8af09b7df6ed2f9 Mon Sep 17 00:00:00 2001 From: Warren James Date: Fri, 6 Jan 2023 14:35:16 -0500 Subject: [PATCH 2/3] test(NODE-4887): Add test for toBSON --- test/node/to_bson_test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/node/to_bson_test.js b/test/node/to_bson_test.js index 1178cda3..4ee56c51 100644 --- a/test/node/to_bson_test.js +++ b/test/node/to_bson_test.js @@ -183,6 +183,15 @@ describe('toBSON', function () { const sizeNestedToBSON = BSON.calculateObjectSize({ a: [0] }); expect(sizeNestedToBSON).to.equal(33); }); + + it('uses toBSON on elements of 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', () => { From 6f927fa8469113370e359403119e12d042cff400 Mon Sep 17 00:00:00 2001 From: Warren James <28974128+W-A-James@users.noreply.github.com> Date: Mon, 9 Jan 2023 14:23:12 -0500 Subject: [PATCH 3/3] test(NODE-4877): Change test name Co-authored-by: Bailey Pearson --- test/node/to_bson_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/node/to_bson_test.js b/test/node/to_bson_test.js index 4ee56c51..0c78ab90 100644 --- a/test/node/to_bson_test.js +++ b/test/node/to_bson_test.js @@ -184,7 +184,7 @@ describe('toBSON', function () { expect(sizeNestedToBSON).to.equal(33); }); - it('uses toBSON on elements of a Map', () => { + it('uses toBSON on values contained in a map', () => { const map = new Map(); map.set('a', 100);