Skip to content

Commit b51413c

Browse files
authored
Merge pull request #14453 from Automattic/vkarpov15/gh-14435
fix(schema): avoid returning string 'nested' as schematype
2 parents d516f50 + e6970bb commit b51413c

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

lib/schema.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -1220,10 +1220,18 @@ function _getPath(schema, path, cleanPath) {
12201220
return schema.paths[path];
12211221
}
12221222
if (schema.subpaths.hasOwnProperty(cleanPath)) {
1223-
return schema.subpaths[cleanPath];
1223+
const subpath = schema.subpaths[cleanPath];
1224+
if (subpath === 'nested') {
1225+
return undefined;
1226+
}
1227+
return subpath;
12241228
}
12251229
if (schema.singleNestedPaths.hasOwnProperty(cleanPath) && typeof schema.singleNestedPaths[cleanPath] === 'object') {
1226-
return schema.singleNestedPaths[cleanPath];
1230+
const singleNestedPath = schema.singleNestedPaths[cleanPath];
1231+
if (singleNestedPath === 'nested') {
1232+
return undefined;
1233+
}
1234+
return singleNestedPath;
12271235
}
12281236

12291237
return null;

test/document.populate.test.js

+60
Original file line numberDiff line numberDiff line change
@@ -934,4 +934,64 @@ describe('document.populate', function() {
934934
assert.ok(foundBook.populated('authorId'));
935935
assert.ok(foundBook.authorId.populated('websiteId'));
936936
});
937+
938+
it('works when populating a nested document inside an array parent (gh-14435)', async function() {
939+
const CodeSchema = new Schema({
940+
code: String
941+
});
942+
943+
const UserSchema = new Schema({
944+
username: String,
945+
extras: [
946+
new Schema({
947+
config: new Schema({
948+
paymentConfiguration: {
949+
paymentMethods: [
950+
{
951+
type: Schema.Types.ObjectId,
952+
ref: 'Code'
953+
}
954+
]
955+
}
956+
})
957+
})
958+
]
959+
});
960+
961+
const Code = db.model('Code', CodeSchema);
962+
const CodeUser = db.model('CodeUser', UserSchema);
963+
964+
const code = await Code.create({
965+
code: 'test code'
966+
});
967+
968+
await CodeUser.create({
969+
username: 'TestUser',
970+
extras: [
971+
{
972+
config: {
973+
paymentConfiguration: {
974+
paymentMethods: [code._id]
975+
}
976+
}
977+
}
978+
]
979+
});
980+
981+
const codeUser = await CodeUser.findOne({ username: 'TestUser' }).populate(
982+
'extras.config.paymentConfiguration.paymentMethods'
983+
);
984+
985+
assert.ok(codeUser.username);
986+
assert.strictEqual(codeUser.username, 'TestUser');
987+
assert.ok(codeUser.extras);
988+
assert.strictEqual(codeUser.extras.length, 1);
989+
assert.ok(codeUser.extras[0]);
990+
assert.ok(codeUser.extras[0].config);
991+
assert.ok(codeUser.extras[0].config.paymentConfiguration);
992+
assert.ok(codeUser.extras[0].config.paymentConfiguration.paymentMethods);
993+
assert.strictEqual(codeUser.extras[0].config.paymentConfiguration.paymentMethods.length, 1);
994+
assert.deepStrictEqual(codeUser.extras[0].config.paymentConfiguration.paymentMethods[0]._id, code._id);
995+
assert.strictEqual(codeUser.extras[0].config.paymentConfiguration.paymentMethods[0].code, 'test code');
996+
});
937997
});

0 commit comments

Comments
 (0)