Skip to content

Commit

Permalink
fix(populate): avoid setting empty array on lean document when popula…
Browse files Browse the repository at this point in the history
…te result is undefined

Fix #10599
  • Loading branch information
vkarpov15 committed Sep 3, 2021
1 parent 1dc9b45 commit 1f28237
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/helpers/populate/assignVals.js
Expand Up @@ -43,6 +43,9 @@ module.exports = function assignVals(o) {
if (val instanceof SkipPopulateValue) {
return val.val;
}
if (val === void 0) {
return val;
}

const _allIds = o.allIds[i];

Expand Down
3 changes: 3 additions & 0 deletions lib/helpers/populate/lookupLocalFields.js
Expand Up @@ -13,6 +13,9 @@ module.exports = function lookupLocalFields(cur, path, val) {
if (typeof cur !== 'object') {
return void 0;
}
if (val === void 0) {
return void 0;
}
cur[path] = val;
return val;
}
Expand Down
37 changes: 37 additions & 0 deletions test/model.populate.test.js
Expand Up @@ -10565,4 +10565,41 @@ describe('model: populate:', function() {
assert.equal(populatedBooks[0].author.name, 'Author1');
});
});

it('avoids setting empty array on lean document when populate result is undefined (gh-10599)', function() {
return co(function*() {
const ImageSchema = Schema({ imageName: String }, { _id: false });
const TextSchema = Schema({
textName: String,
attached: [{ type: 'ObjectId', ref: 'Test2' }]
}, { _id: false });

const ItemSchema = Schema({ objectType: String }, {
discriminatorKey: 'objectType',
_id: false
});

const ExampleSchema = Schema({ test: String, list: [ItemSchema] });

ExampleSchema.path('list').discriminator('Image', ImageSchema);
ExampleSchema.path('list').discriminator('Text', TextSchema);
const Example = db.model('Test', ExampleSchema);
const Another = db.model('Test2', Schema({ test: 'String' }));

yield Another.create({ _id: '61254490ea89de0004c8f2a0', test: 'test' });

const example = yield Example.create({
test: 'example',
list: [
{ imageName: 'an image', objectType: 'Image' },
{ textName: 'this is a text', attached: ['61254490ea89de0004c8f2a0'], objectType: 'Text' }
]
});
const query = Example.findById(example._id).populate('list.attached').lean();

const result = yield query.exec();
assert.strictEqual(result.list[0].attached, void 0);
assert.equal(result.list[1].attached[0].test, 'test');
});
});
});

0 comments on commit 1f28237

Please sign in to comment.