Skip to content

Commit 2b7bb96

Browse files
authored
Merge pull request #16271 from Automattic/vkarpov15/gh-16252
fix(document): correctly handle modified subpaths when parent path is unset after modifying
2 parents ca7f2a0 + 0f6e35d commit 2b7bb96

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

lib/document.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3750,15 +3750,17 @@ Document.prototype.$__dirty = function() {
37503750
}
37513751

37523752
let top = null;
3753+
let foundParent = false;
37533754

37543755
const array = parentPaths(item.path);
37553756
for (let i = 0; i < array.length - 1; i++) {
37563757
if (allPaths.has(array[i])) {
37573758
top = allPaths.get(array[i]);
3759+
foundParent = true;
37583760
break;
37593761
}
37603762
}
3761-
if (top == null) {
3763+
if (!foundParent) {
37623764
minimal.push(item);
37633765
} else if (top != null &&
37643766
top[arrayAtomicsSymbol] != null &&

test/document.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15705,6 +15705,31 @@ describe('document', function() {
1570515705
{ updatePipeline: true }
1570615706
);
1570715707
});
15708+
15709+
it('clears child modified paths when parent mixed path is unset (gh-16252)', async function() {
15710+
const schema = new Schema({
15711+
mail: Schema.Types.Mixed
15712+
}, { versionKey: false });
15713+
const Test = db.model('Test', schema);
15714+
15715+
const created = await Test.create({
15716+
mail: { 207: { emId: 207, readStatus: 0, getAttach: 0 } }
15717+
});
15718+
15719+
const doc = await Test.findById(created._id).orFail();
15720+
doc.mail[207].readStatus = 1;
15721+
doc.markModified('mail.207.readStatus');
15722+
15723+
delete doc.mail[207];
15724+
doc.markModified('mail.207');
15725+
15726+
const delta = doc.$__delta()[1];
15727+
assert.deepStrictEqual(delta, { $unset: { 'mail.207': 1 } });
15728+
15729+
await doc.save();
15730+
const reloaded = await Test.findById(created._id).orFail();
15731+
assert.strictEqual(reloaded.mail[207], undefined);
15732+
});
1570815733
});
1570915734

1571015735
describe('Check if instance function that is supplied in schema option is available', function() {

0 commit comments

Comments
 (0)