Skip to content

Commit

Permalink
refactor: use fix from #13883 for #14172
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Dec 13, 2023
1 parent 6d25679 commit 2a78e25
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,11 @@ Document.prototype.$__set = function(pathToMark, path, options, constructing, pa
val[arrayAtomicsSymbol] = priorVal[arrayAtomicsSymbol];
val[arrayAtomicsBackupSymbol] = priorVal[arrayAtomicsBackupSymbol];
if (utils.isMongooseDocumentArray(val)) {
val.forEach(doc => { doc && (doc.isNew = false); });
val.forEach(doc => {
if (doc) {
doc.isNew = false;
}
});
}
}

Expand Down
31 changes: 31 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12383,6 +12383,37 @@ describe('document', function() {
['__stateBeforeSuspension', '__stateBeforeSuspension.jsonField']
);
});

it('should allow null values in list in self assignment (gh-14172) (gh-13859)', async function() {
const objSchema = new Schema({
date: Date,
value: Number
});

const testSchema = new Schema({
intArray: [Number],
strArray: [String],
objArray: [objSchema]
});
const Test = db.model('Test', testSchema);

const doc = new Test({
intArray: [1, 2, 3, null],
strArray: ['b', null, 'c'],
objArray: [
{ date: new Date(1000), value: 1 },
null,
{ date: new Date(3000), value: 3 }
]
});
await doc.save();
doc.intArray = doc.intArray;
doc.strArray = doc.strArray;
doc.objArray = doc.objArray; // this is the trigger for the error
assert.ok(doc);
await doc.save();
assert.ok(doc);
});
});

describe('Check if instance function that is supplied in schema option is availabe', function() {
Expand Down

0 comments on commit 2a78e25

Please sign in to comment.