From 838a80e5c1708b9227410396b8645f7c995be341 Mon Sep 17 00:00:00 2001 From: Eduarda Scharnhorst Date: Fri, 13 Nov 2026 10:16:28 +0000 Subject: [PATCH 1/3] fix(shell-bson-parser): do not treat own _bsontype field as a BSON value in toJSString --- packages/shell-bson-parser/src/stringify.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/shell-bson-parser/src/stringify.ts b/packages/shell-bson-parser/src/stringify.ts index 203e2d74..20cbc448 100644 --- a/packages/shell-bson-parser/src/stringify.ts +++ b/packages/shell-bson-parser/src/stringify.ts @@ -36,7 +36,10 @@ function detectType(value: BSONValue) { function getTypeDescriptorForValue(value: BSONValue) { const t = detectType(value); - const _bsontype = t === 'Object' && value._bsontype; + const _bsontype = + t === 'Object' && + !Object.prototype.hasOwnProperty.call(value, '_bsontype') && + value._bsontype; return { type: _bsontype || t, isBSON: !!_bsontype, From fc77ca96b4c4991f2c404d9c6df45e257d313061 Mon Sep 17 00:00:00 2001 From: Eduarda Scharnhorst Date: Fri, 13 Nov 2026 10:33:26 +0000 Subject: [PATCH 2/3] fix(shell-bson-parser): keep bson 4 Timestamp detection in toJSString --- packages/shell-bson-parser/src/stringify.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shell-bson-parser/src/stringify.ts b/packages/shell-bson-parser/src/stringify.ts index 20cbc448..83c2b073 100644 --- a/packages/shell-bson-parser/src/stringify.ts +++ b/packages/shell-bson-parser/src/stringify.ts @@ -38,7 +38,7 @@ function getTypeDescriptorForValue(value: BSONValue) { const t = detectType(value); const _bsontype = t === 'Object' && - !Object.prototype.hasOwnProperty.call(value, '_bsontype') && + !Object.prototype.propertyIsEnumerable.call(value, '_bsontype') && value._bsontype; return { type: _bsontype || t, From c747d8b816dac405576901079301c6e2ec4f5f69 Mon Sep 17 00:00:00 2001 From: Eduarda Scharnhorst Date: Fri, 13 Nov 2026 13:21:24 +0000 Subject: [PATCH 3/3] test: include unit tests --- .../shell-bson-parser/src/stringify.spec.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/shell-bson-parser/src/stringify.spec.ts b/packages/shell-bson-parser/src/stringify.spec.ts index 72486f99..29c2e50a 100644 --- a/packages/shell-bson-parser/src/stringify.spec.ts +++ b/packages/shell-bson-parser/src/stringify.spec.ts @@ -7,6 +7,26 @@ const { toJSString } = stringifyModule; describe('stringify', function () { describe('toJSString', function () { + it('round-trips a plain object that has its own _bsontype field', function () { + const doc = { + field: { _bsontype: 'Timestamp', high: 'a', low: 'b' }, + other: { _bsontype: 'Binary', sub_type: 'c' }, + }; + const str = toJSString(doc); + assert.ok(str); + assert.deepStrictEqual(parse(str), doc); + }); + + it('serializes a BSON value that has a non-enumerable own _bsontype', function () { + // bson 4 defines _bsontype this way on Timestamp + const ts = new bson.Timestamp({ t: 1, i: 2 }); + Object.defineProperty(ts, '_bsontype', { value: 'Timestamp' }); + assert.strictEqual( + toJSString({ ts }, 0), + '{ts:Timestamp({ t: 1, i: 2 })}', + ); + }); + it('should default to two spaces', function () { assert.equal( toJSString({ a: { $exists: true } }),