Skip to content

Commit 2d8c168

Browse files
authored
Merge pull request #15302 from Automattic/vkarpov15/gh-9359
fix(populate): correctly get schematypes when deep populating under a map
2 parents ad418a0 + abcf996 commit 2d8c168

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

lib/helpers/populate/getSchemaTypes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ module.exports = function getSchemaTypes(model, schema, doc, path) {
178178
}
179179

180180
const fullPath = nestedPath.concat([trypath]).join('.');
181-
if (topLevelDoc != null && topLevelDoc.$__ && topLevelDoc.$populated(fullPath) && p < parts.length) {
182-
const model = doc.$__.populated[fullPath].options[populateModelSymbol];
181+
if (topLevelDoc != null && topLevelDoc.$__ && topLevelDoc.$populated(fullPath, true) && p < parts.length) {
182+
const model = topLevelDoc.$populated(fullPath, true).options[populateModelSymbol];
183183
if (model != null) {
184184
const ret = search(
185185
parts.slice(p),

test/document.populate.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,4 +1115,41 @@ describe('document.populate', function() {
11151115
assert.equal(docD.refB.title, 'test 2');
11161116
assert.equal(docD.refC.content, 'test 3');
11171117
});
1118+
1119+
it('handles re-populating map of array of refs (gh-9359)', async function() {
1120+
const UserSchema = mongoose.Schema({
1121+
columns: { type: Map, of: [{ type: 'ObjectId', ref: 'Test1' }] }
1122+
});
1123+
const CardSchema = mongoose.Schema({
1124+
title: { type: String },
1125+
sequence: { type: 'ObjectId', ref: 'Test2' }
1126+
});
1127+
const SequenceSchema = mongoose.Schema({
1128+
foo: { type: String }
1129+
});
1130+
1131+
const Sequence = db.model('Test2', SequenceSchema);
1132+
const Card = db.model('Test1', CardSchema);
1133+
const User = db.model('Test', UserSchema);
1134+
1135+
const sequence = await Sequence.create({ foo: 'bar' });
1136+
const card1 = await Card.create({ title: 'card1', sequence });
1137+
const card2 = await Card.create({ title: 'card2', sequence });
1138+
const card3 = await Card.create({ title: 'card3' });
1139+
const card4 = await Card.create({ title: 'card4', sequence });
1140+
await User.create({
1141+
columns: { key1: [card1, card2], key2: [card3, card4] }
1142+
});
1143+
1144+
const user = await User.findOne();
1145+
await user.populate('columns.$*');
1146+
assert.deepStrictEqual(user.columns.get('key1').map(subdoc => subdoc.title), ['card1', 'card2']);
1147+
assert.deepStrictEqual(user.columns.get('key2').map(subdoc => subdoc.title), ['card3', 'card4']);
1148+
await user.populate('columns.$*.sequence');
1149+
assert.deepStrictEqual(user.columns.get('key1').map(subdoc => subdoc.title), ['card1', 'card2']);
1150+
assert.deepStrictEqual(user.columns.get('key1').map(subdoc => subdoc.sequence.foo), ['bar', 'bar']);
1151+
assert.deepStrictEqual(user.columns.get('key2').map(subdoc => subdoc.title), ['card3', 'card4']);
1152+
assert.deepStrictEqual(user.columns.get('key2').map(subdoc => subdoc.sequence?.foo), [undefined, 'bar']);
1153+
1154+
});
11181155
});

0 commit comments

Comments
 (0)