From 9725d93f6d49c13eaf49547b849e82d9879b942d Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 27 Dec 2023 17:38:32 -0500 Subject: [PATCH] fix(document): avoid flattening dotted paths in mixed path underneath nested path Fix #14178 --- lib/document.js | 2 -- test/document.test.js | 23 +++++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/document.js b/lib/document.js index 8acb6103a4c..432e4105ee5 100644 --- a/lib/document.js +++ b/lib/document.js @@ -472,8 +472,6 @@ function $applyDefaultsToNested(val, path, doc) { return; } - flattenObjectWithDottedPaths(val); - const paths = Object.keys(doc.$__schema.paths); const plen = paths.length; diff --git a/test/document.test.js b/test/document.test.js index a0ba629d72f..8a95159c2e0 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -12414,6 +12414,29 @@ describe('document', function() { await doc.save(); assert.ok(doc); }); + + it('avoids overwriting dotted paths in mixed path underneath nested path (gh-14178)', async function() { + const testSchema = new Schema({ + __stateBeforeSuspension: { + field1: String, + field3: { type: Schema.Types.Mixed }, + } + }); + const Test = db.model('Test', testSchema); + let eventObj = new Test({ + __stateBeforeSuspension: { field1: 'test' } + }) + await eventObj.save(); + const newO = eventObj.toObject(); + newO.__stateBeforeSuspension.field3 = {'.ippo': 5}; + eventObj.set(newO); + await eventObj.save(); + + assert.strictEqual(eventObj.__stateBeforeSuspension.field3['.ippo'], 5); + + const fromDb = await Test.findById(eventObj._id).lean().orFail(); + assert.strictEqual(fromDb.__stateBeforeSuspension.field3['.ippo'], 5); + }); }); describe('Check if instance function that is supplied in schema option is availabe', function() {