Skip to content

Commit

Permalink
Merge pull request #5192 from jhob/gh-5189
Browse files Browse the repository at this point in the history
Update `parentArray` references when directly assigning document arrays
  • Loading branch information
vkarpov15 committed Apr 22, 2017
2 parents 2fcd595 + bfd3b00 commit 5fe1296
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/document.js
Expand Up @@ -878,6 +878,13 @@ Document.prototype.$__set = function(pathToMark, path, constructing, parts, sche
if (val && val.isMongooseArray) {
val._registerAtomic('$set', val);

// Update embedded document parent references (gh-5189)
if (val.isMongooseDocumentArray) {
val.forEach(function(item) {
item && item.__parentArray && (item.__parentArray = val);
});
}

// Small hack for gh-1638: if we're overwriting the entire array, ignore
// paths that were modified before the array overwrite
this.$__.activePaths.forEach(function(modifiedPath) {
Expand Down
22 changes: 22 additions & 0 deletions test/document.modified.test.js
Expand Up @@ -465,6 +465,28 @@ describe('document modified', function() {

done();
});

it('updates embedded doc parents upon direct assignment (gh-5189)', function(done) {
var db = start();
var familySchema = new Schema({
children: [{name: {type: String, required: true}}]
});
var Family = db.model('Family', familySchema);
Family.create({
children: [
{name: 'John'},
{name: 'Mary'}
]
}, function(err, family) {
family.set({children: family.children.slice(1)});
family.children.forEach(function(child) {
child.set({name: 'Maryanne'});
});

assert.equal(family.validateSync(), undefined);
done();
});
});
});

it('should support setting mixed paths by string (gh-1418)', function(done) {
Expand Down

0 comments on commit 5fe1296

Please sign in to comment.