Skip to content

Commit

Permalink
fix(document): update single nested subdoc parent when setting to exi…
Browse files Browse the repository at this point in the history
…sting single nested doc

Fix #8400
  • Loading branch information
vkarpov15 committed Dec 12, 2019
1 parent 2c2f98b commit bbabfee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/schema/SingleNestedPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ SingleNestedPath.prototype.$conditionalHandlers.$exists = $exists;
*/

SingleNestedPath.prototype.cast = function(val, doc, init, priorVal) {
if (val && val.$isSingleNested) {
if (val && val.$isSingleNested && val.parent === doc) {
return val;
}

Expand Down
21 changes: 20 additions & 1 deletion test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2571,7 +2571,7 @@ describe('document', function() {
velvetRevolver.guitarist = gnr.guitarist;
velvetRevolver.save(function(error) {
assert.ifError(error);
assert.equal(velvetRevolver.guitarist, gnr.guitarist);
assert.equal(velvetRevolver.guitarist.name, 'Slash');
done();
});
});
Expand Down Expand Up @@ -8282,4 +8282,23 @@ describe('document', function() {
}, /Cannot call.*multiple times/);
});
});

it('setting a path to a single nested document should update the single nested doc parent (gh-8400)', function() {
const schema = Schema({
name: String,
subdoc: new Schema({
name: String
})
});
const Model = db.model('gh8400', schema);

const doc1 = new Model({ name: 'doc1', subdoc: { name: 'subdoc1' } });
const doc2 = new Model({ name: 'doc2', subdoc: { name: 'subdoc2' } });

doc1.subdoc = doc2.subdoc;
assert.equal(doc1.subdoc.name, 'subdoc2');
assert.equal(doc2.subdoc.name, 'subdoc2');
assert.strictEqual(doc1.subdoc.ownerDocument(), doc1);
assert.strictEqual(doc2.subdoc.ownerDocument(), doc2);
});
});

0 comments on commit bbabfee

Please sign in to comment.