Skip to content

Commit

Permalink
fix(populate): handle virtual populate with embedded discriminator un…
Browse files Browse the repository at this point in the history
…der single nested subdoc

Fix #6571
  • Loading branch information
vkarpov15 committed Jun 10, 2018
1 parent 512e090 commit 6909d30
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/services/populate/getSchemaTypes.js
Expand Up @@ -20,7 +20,7 @@ module.exports = function getSchemaTypes(schema, doc, path) {
return pathschema;
}

function search(parts, schema) {
function search(parts, schema, subdoc) {
let p = parts.length + 1;
let foundschema;
let trypath;
Expand All @@ -38,8 +38,9 @@ module.exports = function getSchemaTypes(schema, doc, path) {
let schemas = null;
if (doc != null && foundschema.schema != null && foundschema.schema.discriminators != null) {
const discriminators = foundschema.schema.discriminators;
const keys = mpath.get(trypath + '.' + foundschema.schema.options.discriminatorKey,
doc);
const discriminatorKeyPath = trypath + '.' +
foundschema.schema.options.discriminatorKey;
const keys = mpath.get(discriminatorKeyPath, subdoc) || [];
schemas = Object.keys(discriminators).
reduce(function(cur, discriminator) {
if (keys.indexOf(discriminator) !== -1) {
Expand All @@ -63,7 +64,7 @@ module.exports = function getSchemaTypes(schema, doc, path) {
return foundschema;
}
// comments.$.comments.$.title
ret = search(parts.slice(p + 1), schema);
ret = search(parts.slice(p + 1), schema, mpath.get(trypath, subdoc));
if (ret) {
ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
!foundschema.schema.$isSingleNested;
Expand All @@ -74,7 +75,7 @@ module.exports = function getSchemaTypes(schema, doc, path) {
if (schemas != null && schemas.length > 0) {
ret = [];
for (var i = 0; i < schemas.length; ++i) {
let _ret = search(parts.slice(p), schemas[i]);
let _ret = search(parts.slice(p), schemas[i], mpath.get(trypath, subdoc));
if (_ret != null) {
_ret.$isUnderneathDocArray = _ret.$isUnderneathDocArray ||
!foundschema.schema.$isSingleNested;
Expand All @@ -86,7 +87,7 @@ module.exports = function getSchemaTypes(schema, doc, path) {
}
return ret;
} else {
ret = search(parts.slice(p), foundschema.schema);
ret = search(parts.slice(p), foundschema.schema, mpath.get(trypath, subdoc));

if (ret) {
ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
Expand All @@ -111,5 +112,5 @@ module.exports = function getSchemaTypes(schema, doc, path) {
parts[i] = '0';
}
}
return search(parts, schema);
return search(parts, schema, doc);
};

0 comments on commit 6909d30

Please sign in to comment.