Skip to content

Commit 2a78e25

Browse files
committed
refactor: use fix from #13883 for #14172
1 parent 6d25679 commit 2a78e25

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lib/document.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,11 @@ Document.prototype.$__set = function(pathToMark, path, options, constructing, pa
16891689
val[arrayAtomicsSymbol] = priorVal[arrayAtomicsSymbol];
16901690
val[arrayAtomicsBackupSymbol] = priorVal[arrayAtomicsBackupSymbol];
16911691
if (utils.isMongooseDocumentArray(val)) {
1692-
val.forEach(doc => { doc && (doc.isNew = false); });
1692+
val.forEach(doc => {
1693+
if (doc) {
1694+
doc.isNew = false;
1695+
}
1696+
});
16931697
}
16941698
}
16951699

test/document.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12383,6 +12383,37 @@ describe('document', function() {
1238312383
['__stateBeforeSuspension', '__stateBeforeSuspension.jsonField']
1238412384
);
1238512385
});
12386+
12387+
it('should allow null values in list in self assignment (gh-14172) (gh-13859)', async function() {
12388+
const objSchema = new Schema({
12389+
date: Date,
12390+
value: Number
12391+
});
12392+
12393+
const testSchema = new Schema({
12394+
intArray: [Number],
12395+
strArray: [String],
12396+
objArray: [objSchema]
12397+
});
12398+
const Test = db.model('Test', testSchema);
12399+
12400+
const doc = new Test({
12401+
intArray: [1, 2, 3, null],
12402+
strArray: ['b', null, 'c'],
12403+
objArray: [
12404+
{ date: new Date(1000), value: 1 },
12405+
null,
12406+
{ date: new Date(3000), value: 3 }
12407+
]
12408+
});
12409+
await doc.save();
12410+
doc.intArray = doc.intArray;
12411+
doc.strArray = doc.strArray;
12412+
doc.objArray = doc.objArray; // this is the trigger for the error
12413+
assert.ok(doc);
12414+
await doc.save();
12415+
assert.ok(doc);
12416+
});
1238612417
});
1238712418

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

0 commit comments

Comments
 (0)