Skip to content

Commit 0a33412

Browse files
committed
fix(populate): handle virtual populate of an embedded discriminator nested path
Fix #8173 Re: #6488
1 parent b42d0f5 commit 0a33412

File tree

4 files changed

+32
-28
lines changed

4 files changed

+32
-28
lines changed

lib/helpers/populate/getModelsMapForPopulate.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,14 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
7878
}
7979
}
8080

81-
const virtual = getVirtual(model.schema, options.path);
81+
const _virtualRes = getVirtual(model.schema, options.path);
82+
const virtual = _virtualRes == null ? null : _virtualRes.virtual;
83+
8284
let localField;
8385
let count = false;
8486
if (virtual && virtual.options) {
85-
const virtualPrefix = virtual.$nestedSchemaPath ?
86-
virtual.$nestedSchemaPath + '.' : '';
87+
const virtualPrefix = _virtualRes.nestedSchemaPath ?
88+
_virtualRes.nestedSchemaPath + '.' : '';
8789
if (typeof virtual.options.localField === 'function') {
8890
localField = virtualPrefix + virtual.options.localField.call(doc, doc);
8991
} else {
@@ -312,7 +314,8 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
312314
} else {
313315
schemaForCurrentDoc = schema;
314316
}
315-
const virtual = getVirtual(modelForCurrentDoc.schema, options.path);
317+
const _virtualRes = getVirtual(modelForCurrentDoc.schema, options.path);
318+
const virtual = _virtualRes == null ? null : _virtualRes.virtual;
316319

317320
let ref;
318321
let refPath;

lib/helpers/populate/getVirtual.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = getVirtual;
88

99
function getVirtual(schema, name) {
1010
if (schema.virtuals[name]) {
11-
return schema.virtuals[name];
11+
return { virtual: schema.virtuals[name], path: void 0 };
1212
}
1313
const parts = name.split('.');
1414
let cur = '';
@@ -17,8 +17,7 @@ function getVirtual(schema, name) {
1717
cur += (cur.length > 0 ? '.' : '') + parts[i];
1818
if (schema.virtuals[cur]) {
1919
if (i === parts.length - 1) {
20-
schema.virtuals[cur].$nestedSchemaPath = nestedSchemaPath;
21-
return schema.virtuals[cur];
20+
return { virtual: schema.virtuals[cur], path: nestedSchemaPath };
2221
}
2322
continue;
2423
}
@@ -33,20 +32,24 @@ function getVirtual(schema, name) {
3332

3433
if (schema.virtuals[rest]) {
3534
if (i === parts.length - 2) {
36-
schema.virtuals[rest].$nestedSchemaPath =
37-
[nestedSchemaPath, cur].filter(v => !!v).join('.');
38-
return schema.virtuals[rest];
35+
return {
36+
virtual: schema.virtuals[rest],
37+
nestedSchemaPath:[nestedSchemaPath, cur].filter(v => !!v).join('.')
38+
};
3939
}
4040
continue;
4141
}
4242

4343
if (i + 1 < parts.length && schema.discriminators) {
4444
for (const key of Object.keys(schema.discriminators)) {
45-
const _virtual = getVirtual(schema.discriminators[key], rest);
46-
if (_virtual != null) {
47-
_virtual.$nestedSchemaPath = [nestedSchemaPath, cur].
45+
const res = getVirtual(schema.discriminators[key], rest);
46+
if (res != null) {
47+
const _path = [nestedSchemaPath, cur, res.nestedSchemaPath].
4848
filter(v => !!v).join('.');
49-
return _virtual;
49+
return {
50+
virtual: res.virtual,
51+
nestedSchemaPath: _path
52+
};
5053
}
5154
}
5255
}

test/helpers/populate.getVirtual.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('getVirtual', function() {
3939
product: { type: String }
4040
}));
4141

42-
assert.equal(getVirtual(batchSchema, 'nested.events.users_$').options.ref,
42+
assert.equal(getVirtual(batchSchema, 'nested.events.users_$').virtual.options.ref,
4343
'Users');
4444

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

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

8686
done();
8787
});
@@ -106,13 +106,13 @@ describe('getVirtual', function() {
106106

107107
docArray.discriminator('Clicked', clickedSchema);
108108

109-
let virtual = getVirtual(batchSchema, 'events.users_$');
110-
assert.equal(virtual.options.ref, 'Users');
111-
assert.equal(virtual.$nestedSchemaPath, 'events');
109+
let res = getVirtual(batchSchema, 'events.users_$');
110+
assert.equal(res.virtual.options.ref, 'Users');
111+
assert.equal(res.nestedSchemaPath, 'events');
112112

113-
virtual = getVirtual(batchSchema, 'events.users_$');
114-
assert.equal(virtual.options.ref, 'Users');
115-
assert.equal(virtual.$nestedSchemaPath, 'events');
113+
res = getVirtual(batchSchema, 'events.users_$');
114+
assert.equal(res.virtual.options.ref, 'Users');
115+
assert.equal(res.nestedSchemaPath, 'events');
116116

117117
done();
118118
});

test/model.populate.test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8628,7 +8628,7 @@ describe('model: populate:', function() {
86288628
});
86298629
});
86308630

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

86488648
const docArray = batchSchema.path('nested.events');
8649-
const Clicked = docArray.
8650-
discriminator('gh6488_Clicked', Schema({ nestedLayer: nestedLayerSchema }));
8651-
const Purchased = docArray.
8652-
discriminator('gh6488_Purchased', Schema({ purchased: String }));
8649+
docArray.discriminator('gh6488_Clicked', Schema({ nestedLayer: nestedLayerSchema }));
8650+
docArray.discriminator('gh6488_Purchased', Schema({ purchased: String }));
86538651

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

0 commit comments

Comments
 (0)