Skip to content

Commit

Permalink
fix(populate): handle virtual populate under single nested doc under …
Browse files Browse the repository at this point in the history
…embedded discriminator

Fix #6488
  • Loading branch information
vkarpov15 committed May 27, 2018
1 parent 2a2f9c8 commit a3352f6
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions lib/services/populate/getVirtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,27 @@ function getVirtual(schema, name) {

if (schema.paths[cur] && schema.paths[cur].schema) {
schema = schema.paths[cur].schema;
const rest = parts.slice(i + 1).join('.');

if (i === parts.length - 2 && schema.discriminators) {
// Check for embedded discriminators, don't currently support populating
// nested virtuals underneath embedded discriminators because that will
// require substantial refactoring.
if (schema.virtuals[rest]) {
if (i === parts.length - 2) {
schema.virtuals[rest].$nestedSchemaPath =
[nestedSchemaPath, cur].filter(v => !!v).join('.');
return schema.virtuals[rest];
}
continue;
}

if (i + 1 < parts.length && schema.discriminators) {
for (let key of Object.keys(schema.discriminators)) {
const discriminatorSchema = schema.discriminators[key];
let _cur = parts[i + 1];
if (discriminatorSchema.virtuals[_cur]) {
discriminatorSchema.virtuals[_cur].$nestedSchemaPath =
(nestedSchemaPath.length > 0 ? nestedSchemaPath + '.' : '') + cur;
return discriminatorSchema.virtuals[_cur];
const _virtual = getVirtual(schema.discriminators[key], rest);
if (_virtual != null) {
_virtual.$nestedSchemaPath = [
nestedSchemaPath,
cur,
_virtual.$nestedSchemaPath
].filter(v => !!v).join('.');
return _virtual;
}
}
}
Expand Down

0 comments on commit a3352f6

Please # to comment.