Skip to content

Commit

Permalink
fix: avoid setting defaults on insert on a path whose subpath is refe…
Browse files Browse the repository at this point in the history
…renced in the update

Fix #10624
  • Loading branch information
vkarpov15 committed Sep 1, 2021
1 parent e1d4aa4 commit e94d603
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 10 additions & 1 deletion lib/helpers/common.js
Expand Up @@ -78,7 +78,16 @@ function modifiedPaths(update, path, result) {
const key = keys[i];
let val = update[key];

result[path + key] = true;
const _path = path + key;
result[_path] = true;
if (_path.indexOf('.') !== -1) {
const sp = _path.split('.');
let cur = sp[0];
for (let i = 1; i < sp.length; ++i) {
result[cur] = true;
cur += '.' + sp[i];
}
}
if (isMongooseObject(val) && !Buffer.isBuffer(val)) {
val = val.toObject({ transform: false, virtuals: false });
}
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/update.modifiedPaths.test.js
Expand Up @@ -6,7 +6,7 @@ const modifiedPaths = require('../../lib/helpers/update/modifiedPaths');
describe('modifiedPaths', function() {
it('works without any update operators', function(done) {
const res = modifiedPaths({ a: 1, 'b.c': 2, d: { e: 1 } });
assert.deepEqual(res, { a: true, 'b.c': true, d: true, 'd.e': true });
assert.deepEqual(res, { a: true, b: true, 'b.c': true, d: true, 'd.e': true });

done();
});
Expand All @@ -17,7 +17,7 @@ describe('modifiedPaths', function() {
$set: { 'b.c': 2 },
$setOnInsert: { d: { e: 1 } }
});
assert.deepEqual(res, { a: true, 'b.c': true, d: true, 'd.e': true });
assert.deepEqual(res, { a: true, b: true, 'b.c': true, d: true, 'd.e': true });

done();
});
Expand Down

0 comments on commit e94d603

Please sign in to comment.