Skip to content

Commit

Permalink
fix(populate): find connect justOne when double-populating underneath…
Browse files Browse the repository at this point in the history
… an array
  • Loading branch information
vkarpov15 committed Sep 12, 2018
1 parent b5841c2 commit a01b827
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
47 changes: 37 additions & 10 deletions lib/helpers/populate/getSchemaTypes.js
Expand Up @@ -17,12 +17,13 @@ const mpath = require('mpath');

module.exports = function getSchemaTypes(schema, doc, path) {
const pathschema = schema.path(path);
const topLevelDoc = doc;

if (pathschema) {
return pathschema;
}

function search(parts, schema, subdoc) {
function search(parts, schema, subdoc, nestedPath) {
let p = parts.length + 1;
let foundschema;
let trypath;
Expand Down Expand Up @@ -70,7 +71,12 @@ module.exports = function getSchemaTypes(schema, doc, path) {
return foundschema;
}
// comments.$.comments.$.title
ret = search(parts.slice(p + 1), schema, subdoc ? mpath.get(trypath, subdoc) : null);
ret = search(
parts.slice(p + 1),
schema,
subdoc ? mpath.get(trypath, subdoc) : null,
nestedPath.concat(parts.slice(0, p))
);
if (ret) {
ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
!foundschema.schema.$isSingleNested;
Expand All @@ -81,7 +87,12 @@ module.exports = function getSchemaTypes(schema, doc, path) {
if (schemas != null && schemas.length > 0) {
ret = [];
for (let i = 0; i < schemas.length; ++i) {
const _ret = search(parts.slice(p), schemas[i], subdoc ? mpath.get(trypath, subdoc) : null);
const _ret = search(
parts.slice(p),
schemas[i],
subdoc ? mpath.get(trypath, subdoc) : null,
nestedPath.concat(parts.slice(0, p))
);
if (_ret != null) {
_ret.$isUnderneathDocArray = _ret.$isUnderneathDocArray ||
!foundschema.schema.$isSingleNested;
Expand All @@ -93,7 +104,12 @@ module.exports = function getSchemaTypes(schema, doc, path) {
}
return ret;
} else {
ret = search(parts.slice(p), foundschema.schema, subdoc ? mpath.get(trypath, subdoc) : null);
ret = search(
parts.slice(p),
foundschema.schema,
subdoc ? mpath.get(trypath, subdoc) : null,
nestedPath.concat(parts.slice(0, p))
);

if (ret) {
ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
Expand All @@ -105,10 +121,16 @@ module.exports = function getSchemaTypes(schema, doc, path) {
}
}

if (doc.$__ && doc.populated(trypath) && p < parts.length) {
const schema = get(doc.$__.populated[trypath], 'options.model.schema');
const fullPath = nestedPath.concat([trypath]).join('.');
if (topLevelDoc.$__ && topLevelDoc.populated(fullPath) && p < parts.length) {
const schema = get(doc.$__.populated[fullPath], 'options.model.schema');
if (schema != null) {
const ret = search(parts.slice(p), schema, subdoc ? mpath.get(trypath, subdoc) : null);
const ret = search(
parts.slice(p),
schema,
subdoc ? mpath.get(trypath, subdoc) : null,
nestedPath.concat(parts.slice(0, p))
);

if (ret) {
ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
Expand All @@ -119,12 +141,17 @@ module.exports = function getSchemaTypes(schema, doc, path) {
}
}

const _val = get(doc, trypath);
const _val = get(topLevelDoc, trypath);
if (_val != null && _val[leanPopulateSymbol] != null) {
// Populated using lean, `_val[leanPopulateSymbol]` is the foreign model
const schema = _val[leanPopulateSymbol].schema;
if (schema != null) {
const ret = search(parts.slice(p), schema, subdoc ? mpath.get(trypath, subdoc) : null);
const ret = search(
parts.slice(p),
schema,
subdoc ? mpath.get(trypath, subdoc) : null,
nestedPath.concat(parts.slice(0, p))
);

if (ret) {
ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
Expand All @@ -147,5 +174,5 @@ module.exports = function getSchemaTypes(schema, doc, path) {
parts[i] = '0';
}
}
return search(parts, schema, doc);
return search(parts, schema, doc, []);
};
2 changes: 1 addition & 1 deletion test/model.populate.test.js
Expand Up @@ -7599,7 +7599,7 @@ describe('model: populate:', function() {
{ make: 'Ford', versions: [{ type: 'F150', editions: [editions[0]] }] },
{ make: 'BMW', versions: [{ type: 'i8', editions: [editions[1]] }] }
]);

cars = yield Car.find().sort({ make: 1 });
cars = yield Car.populate(cars, 'versions.editions');
cars = yield Car.populate(cars, 'versions.editions.editionOptions.image');
Expand Down

0 comments on commit a01b827

Please sign in to comment.