From 78bca702fa3dc6b59086fef8d9aed2ca570ee448 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 8 Sep 2023 09:56:40 -0400 Subject: [PATCH] test: add test case for #13839 --- .../populate/markArraySubdocsPopulated.js | 2 +- test/model.populate.test.js | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/helpers/populate/markArraySubdocsPopulated.js b/lib/helpers/populate/markArraySubdocsPopulated.js index a22605a1d78..f9cc83cb6bf 100644 --- a/lib/helpers/populate/markArraySubdocsPopulated.js +++ b/lib/helpers/populate/markArraySubdocsPopulated.js @@ -38,7 +38,7 @@ module.exports = function markArraySubdocsPopulated(doc, populated) { if (utils.isMongooseDocumentArray(val)) { for (let j = 0; j < val.length; ++j) { - if(val[j]){ + if (val[j]) { val[j].populated(rest, item._docs[id] == null ? void 0 : item._docs[id][j], item); } } diff --git a/test/model.populate.test.js b/test/model.populate.test.js index 690e7783872..bd79133c9a1 100644 --- a/test/model.populate.test.js +++ b/test/model.populate.test.js @@ -10285,6 +10285,28 @@ describe('model: populate:', function() { }); + it('handles populating underneath document arrays that have null (gh-13839)', async function() { + const schema = new Schema({ + children: [ + { + doc: { type: mongoose.Schema.Types.ObjectId, ref: 'Child' }, + name: String + }, + ] + }); + const Parent = db.model('Parent', schema); + const Child = db.model('Child', new Schema({ name: String })); + + const child = new Child({ name: 'gh-13839-test' }); + await child.save(); + let parent = new Parent({ children: [{ doc: child._id, name: 'foo' }, null] }); + await parent.save(); + + parent = await Parent.findById(parent._id).populate('children.doc'); + assert.equal(parent.children.length, 2); + assert.equal(parent.children[0].doc.name, 'gh-13839-test'); + assert.equal(parent.children[1], null); + }); describe('strictPopulate', function() { it('reports full path when throwing `strictPopulate` error with deep populate (gh-10923)', async function() {