Skip to content

Commit

Permalink
fix(populate): handle virtual populate of an embedded discriminator n…
Browse files Browse the repository at this point in the history
…ested path

Fix #8173
Re: #6488
  • Loading branch information
vkarpov15 committed Sep 24, 2019
1 parent b42d0f5 commit 0a33412
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
11 changes: 7 additions & 4 deletions lib/helpers/populate/getModelsMapForPopulate.js
Expand Up @@ -78,12 +78,14 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
}
}

const virtual = getVirtual(model.schema, options.path);
const _virtualRes = getVirtual(model.schema, options.path);
const virtual = _virtualRes == null ? null : _virtualRes.virtual;

let localField;
let count = false;
if (virtual && virtual.options) {
const virtualPrefix = virtual.$nestedSchemaPath ?
virtual.$nestedSchemaPath + '.' : '';
const virtualPrefix = _virtualRes.nestedSchemaPath ?
_virtualRes.nestedSchemaPath + '.' : '';
if (typeof virtual.options.localField === 'function') {
localField = virtualPrefix + virtual.options.localField.call(doc, doc);
} else {
Expand Down Expand Up @@ -312,7 +314,8 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
} else {
schemaForCurrentDoc = schema;
}
const virtual = getVirtual(modelForCurrentDoc.schema, options.path);
const _virtualRes = getVirtual(modelForCurrentDoc.schema, options.path);
const virtual = _virtualRes == null ? null : _virtualRes.virtual;

let ref;
let refPath;
Expand Down
23 changes: 13 additions & 10 deletions lib/helpers/populate/getVirtual.js
Expand Up @@ -8,7 +8,7 @@ module.exports = getVirtual;

function getVirtual(schema, name) {
if (schema.virtuals[name]) {
return schema.virtuals[name];
return { virtual: schema.virtuals[name], path: void 0 };
}
const parts = name.split('.');
let cur = '';
Expand All @@ -17,8 +17,7 @@ function getVirtual(schema, name) {
cur += (cur.length > 0 ? '.' : '') + parts[i];
if (schema.virtuals[cur]) {
if (i === parts.length - 1) {
schema.virtuals[cur].$nestedSchemaPath = nestedSchemaPath;
return schema.virtuals[cur];
return { virtual: schema.virtuals[cur], path: nestedSchemaPath };
}
continue;
}
Expand All @@ -33,20 +32,24 @@ function getVirtual(schema, name) {

if (schema.virtuals[rest]) {
if (i === parts.length - 2) {
schema.virtuals[rest].$nestedSchemaPath =
[nestedSchemaPath, cur].filter(v => !!v).join('.');
return schema.virtuals[rest];
return {
virtual: schema.virtuals[rest],
nestedSchemaPath:[nestedSchemaPath, cur].filter(v => !!v).join('.')
};
}
continue;
}

if (i + 1 < parts.length && schema.discriminators) {
for (const key of Object.keys(schema.discriminators)) {
const _virtual = getVirtual(schema.discriminators[key], rest);
if (_virtual != null) {
_virtual.$nestedSchemaPath = [nestedSchemaPath, cur].
const res = getVirtual(schema.discriminators[key], rest);
if (res != null) {
const _path = [nestedSchemaPath, cur, res.nestedSchemaPath].
filter(v => !!v).join('.');
return _virtual;
return {
virtual: res.virtual,
nestedSchemaPath: _path
};
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions test/helpers/populate.getVirtual.test.js
Expand Up @@ -39,7 +39,7 @@ describe('getVirtual', function() {
product: { type: String }
}));

assert.equal(getVirtual(batchSchema, 'nested.events.users_$').options.ref,
assert.equal(getVirtual(batchSchema, 'nested.events.users_$').virtual.options.ref,
'Users');

done();
Expand Down Expand Up @@ -80,8 +80,8 @@ describe('getVirtual', function() {
// Second embedded discriminator
docArray.discriminator('Purchased', new Schema({ product: String }));

const virtual = getVirtual(batchSchema, 'nested.events.nestedLayer.users_$');
assert.equal(virtual.options.ref, 'Users');
const res = getVirtual(batchSchema, 'nested.events.nestedLayer.users_$');
assert.equal(res.virtual.options.ref, 'Users');

done();
});
Expand All @@ -106,13 +106,13 @@ describe('getVirtual', function() {

docArray.discriminator('Clicked', clickedSchema);

let virtual = getVirtual(batchSchema, 'events.users_$');
assert.equal(virtual.options.ref, 'Users');
assert.equal(virtual.$nestedSchemaPath, 'events');
let res = getVirtual(batchSchema, 'events.users_$');
assert.equal(res.virtual.options.ref, 'Users');
assert.equal(res.nestedSchemaPath, 'events');

virtual = getVirtual(batchSchema, 'events.users_$');
assert.equal(virtual.options.ref, 'Users');
assert.equal(virtual.$nestedSchemaPath, 'events');
res = getVirtual(batchSchema, 'events.users_$');
assert.equal(res.virtual.options.ref, 'Users');
assert.equal(res.nestedSchemaPath, 'events');

done();
});
Expand Down
8 changes: 3 additions & 5 deletions test/model.populate.test.js
Expand Up @@ -8628,7 +8628,7 @@ describe('model: populate:', function() {
});
});

it('handles virtual populate underneath embedded discriminator nested path (gh-6488) (gh-8173)', function() {
it('handles virtual populate of an embedded discriminator nested path (gh-6488) (gh-8173)', function() {
return co(function*() {
const UserModel = db.model('gh6488_User', Schema({
employeeId: Number,
Expand All @@ -8646,10 +8646,8 @@ describe('model: populate:', function() {
});

const docArray = batchSchema.path('nested.events');
const Clicked = docArray.
discriminator('gh6488_Clicked', Schema({ nestedLayer: nestedLayerSchema }));
const Purchased = docArray.
discriminator('gh6488_Purchased', Schema({ purchased: String }));
docArray.discriminator('gh6488_Clicked', Schema({ nestedLayer: nestedLayerSchema }));
docArray.discriminator('gh6488_Purchased', Schema({ purchased: String }));

const Batch = db.model('gh6488', batchSchema);

Expand Down

0 comments on commit 0a33412

Please sign in to comment.