Skip to content

Commit 1f28237

Browse files
committedSep 3, 2021
fix(populate): avoid setting empty array on lean document when populate result is undefined
Fix #10599
1 parent 1dc9b45 commit 1f28237

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed
 

‎lib/helpers/populate/assignVals.js

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ module.exports = function assignVals(o) {
4343
if (val instanceof SkipPopulateValue) {
4444
return val.val;
4545
}
46+
if (val === void 0) {
47+
return val;
48+
}
4649

4750
const _allIds = o.allIds[i];
4851

‎lib/helpers/populate/lookupLocalFields.js

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ module.exports = function lookupLocalFields(cur, path, val) {
1313
if (typeof cur !== 'object') {
1414
return void 0;
1515
}
16+
if (val === void 0) {
17+
return void 0;
18+
}
1619
cur[path] = val;
1720
return val;
1821
}

‎test/model.populate.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -10565,4 +10565,41 @@ describe('model: populate:', function() {
1056510565
assert.equal(populatedBooks[0].author.name, 'Author1');
1056610566
});
1056710567
});
10568+
10569+
it('avoids setting empty array on lean document when populate result is undefined (gh-10599)', function() {
10570+
return co(function*() {
10571+
const ImageSchema = Schema({ imageName: String }, { _id: false });
10572+
const TextSchema = Schema({
10573+
textName: String,
10574+
attached: [{ type: 'ObjectId', ref: 'Test2' }]
10575+
}, { _id: false });
10576+
10577+
const ItemSchema = Schema({ objectType: String }, {
10578+
discriminatorKey: 'objectType',
10579+
_id: false
10580+
});
10581+
10582+
const ExampleSchema = Schema({ test: String, list: [ItemSchema] });
10583+
10584+
ExampleSchema.path('list').discriminator('Image', ImageSchema);
10585+
ExampleSchema.path('list').discriminator('Text', TextSchema);
10586+
const Example = db.model('Test', ExampleSchema);
10587+
const Another = db.model('Test2', Schema({ test: 'String' }));
10588+
10589+
yield Another.create({ _id: '61254490ea89de0004c8f2a0', test: 'test' });
10590+
10591+
const example = yield Example.create({
10592+
test: 'example',
10593+
list: [
10594+
{ imageName: 'an image', objectType: 'Image' },
10595+
{ textName: 'this is a text', attached: ['61254490ea89de0004c8f2a0'], objectType: 'Text' }
10596+
]
10597+
});
10598+
const query = Example.findById(example._id).populate('list.attached').lean();
10599+
10600+
const result = yield query.exec();
10601+
assert.strictEqual(result.list[0].attached, void 0);
10602+
assert.equal(result.list[1].attached[0].test, 'test');
10603+
});
10604+
});
1056810605
});

0 commit comments

Comments
 (0)
Please sign in to comment.