Skip to content

Commit 10ff060

Browse files
authored
Merge pull request #15774 from Automattic/vkarpov15/populate-embedded-discriminator-fix
fix(populate): correctly populate embedded discriminators
2 parents f7bf692 + ee920b7 commit 10ff060

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/helpers/populate/getSchemaTypes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ module.exports = function getSchemaTypes(model, schema, doc, path) {
5858
continue;
5959
}
6060

61-
if (foundschema.embeddedSchemaType) {
61+
if (foundschema.embeddedSchemaType || foundschema.schema) {
6262
// array of Mixed?
6363
if (foundschema.embeddedSchemaType instanceof Mixed) {
6464
return foundschema.embeddedSchemaType;

test/model.populate.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11689,4 +11689,30 @@ describe('model: populate:', function() {
1168911689
assert.deepStrictEqual(obj.rubric.parameters.param1.evaluationPrompts.map(prompt => prompt.name), ['Test Prompt', 'Test Prompt 2']);
1169011690
assert.deepStrictEqual(obj.rubric.parameters.param2.evaluationPrompts.map(prompt => prompt.name), ['Test Prompt 3']);
1169111691
});
11692+
11693+
it('handles populating embedded discriminator', async function() {
11694+
const sectionSchema = new Schema({
11695+
subdoc: new Schema({
11696+
name: String
11697+
})
11698+
});
11699+
sectionSchema.path('subdoc').discriminator('Test', new Schema({
11700+
subSection: {
11701+
type: 'ObjectId',
11702+
ref: 'SubSection'
11703+
}
11704+
}));
11705+
const Section = db.model('Section', sectionSchema);
11706+
const SubSection = db.model('SubSection', new Schema({ name: String }));
11707+
11708+
const subsection = await SubSection.create({ name: 'foo' });
11709+
let section = await Section.create({
11710+
subdoc: {
11711+
__t: 'Test',
11712+
subSection: subsection._id
11713+
}
11714+
});
11715+
section = await Section.findById(section).populate('subdoc.subSection');
11716+
assert.equal(section.subdoc.subSection.name, 'foo');
11717+
});
1169211718
});

0 commit comments

Comments
 (0)