Skip to content

Commit

Permalink
fix(document): support setting nested path to itself when it has nest…
Browse files Browse the repository at this point in the history
…ed subpaths

Fix #9313
  • Loading branch information
vkarpov15 committed Aug 13, 2020
1 parent 3d52fff commit c071e52
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/helpers/document/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function defineKey(prop, subprops, prototype, prefix, keys, options) {
if (v != null && v.$__isNested) {
// Convert top-level to POJO, but leave subdocs hydrated so `$set`
// can handle them. See gh-9293.
v = v.$__parent.get(v.$__.nestedPath);
v = v.$__parent.get(path);
}
const doc = this.$__[scopeSymbol] || this;
doc.$set(path, v);
Expand Down
33 changes: 33 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9164,6 +9164,39 @@ describe('document', function() {
});
});

it('doesnt wipe out nested paths when setting a nested path to itself (gh-9313)', function() {
const schema = new Schema({
nested: {
prop1: { type: Number, default: 50 },
prop2: {
type: String,
enum: ['val1', 'val2'],
default: 'val1',
required: true
},
prop3: {
prop4: { type: Number, default: 0 }
}
}
});

const Model = db.model('Test', schema);

return co(function*() {
let doc = yield Model.create({});

doc = yield Model.findById(doc);

doc.nested = doc.nested;

assert.equal(doc.nested.prop2, 'val1');
yield doc.save();

const fromDb = yield Model.collection.findOne({ _id: doc._id });
assert.equal(fromDb.nested.prop2, 'val1');
});
});

it('allows saving after setting document array to itself (gh-9266)', function() {
const Model = db.model('Test', Schema({ keys: [{ _id: false, name: String }] }));

Expand Down

0 comments on commit c071e52

Please sign in to comment.