Skip to content

Commit

Permalink
fix(document): avoid flattening dotted paths in mixed path underneath…
Browse files Browse the repository at this point in the history
… nested path

Fix #14178
  • Loading branch information
vkarpov15 committed Dec 27, 2023
1 parent 44f391b commit 9725d93
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 0 additions & 2 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,6 @@ function $applyDefaultsToNested(val, path, doc) {
return;
}

flattenObjectWithDottedPaths(val);

const paths = Object.keys(doc.$__schema.paths);
const plen = paths.length;

Expand Down
23 changes: 23 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },

Check failure on line 12422 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma

Check failure on line 12422 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma
}
});
const Test = db.model('Test', testSchema);
let eventObj = new Test({

Check warning on line 12426 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

'eventObj' is never reassigned. Use 'const' instead

Check warning on line 12426 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

'eventObj' is never reassigned. Use 'const' instead
__stateBeforeSuspension: { field1: 'test' }
})

Check failure on line 12428 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Missing semicolon

Check failure on line 12428 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Missing semicolon
await eventObj.save();
const newO = eventObj.toObject();
newO.__stateBeforeSuspension.field3 = {'.ippo': 5};

Check failure on line 12431 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

A space is required after '{'

Check failure on line 12431 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

A space is required before '}'

Check failure on line 12431 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

A space is required after '{'

Check failure on line 12431 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

A space is required before '}'
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() {
Expand Down

0 comments on commit 9725d93

Please sign in to comment.