Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Automattic/mongoose
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Feb 17, 2019
2 parents aa43200 + 0daf626 commit 3e44bc2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/model.js
Expand Up @@ -4188,8 +4188,9 @@ function getModelsMapForPopulate(model, docs, options) {
foreignField = foreignField.call(doc);
}

const localFieldPath = modelSchema.paths[localField];
const localFieldGetters = localFieldPath ? localFieldPath.getters : [];
const localFieldPathType = modelSchema._getPathType(localField);
const localFieldPath = localFieldPathType === 'real' ? modelSchema.paths[localField] : localFieldPathType.schema;
const localFieldGetters = localFieldPath && localFieldPath.getters ? localFieldPath.getters : [];
let ret;

const _populateOptions = get(options, 'options', {});
Expand All @@ -4199,7 +4200,14 @@ function getModelsMapForPopulate(model, docs, options) {
options.isVirtual && get(virtual, 'options.getters', false);
if (localFieldGetters.length > 0 && getters) {
const hydratedDoc = (doc.$__ != null) ? doc : model.hydrate(doc);
ret = localFieldPath.applyGetters(doc[localField], hydratedDoc);
const localFieldValue = utils.getValue(localField, doc);
if (Array.isArray(localFieldValue)) {
const localFieldHydratedValue = utils.getValue(localField.split('.').slice(0, -1), hydratedDoc);
ret = localFieldValue.map((localFieldArrVal, localFieldArrIndex) =>
localFieldPath.applyGetters(localFieldArrVal, localFieldHydratedValue[localFieldArrIndex]));
} else {
ret = localFieldPath.applyGetters(localFieldValue, hydratedDoc);
}
} else {
ret = convertTo_id(utils.getValue(localField, doc));
}
Expand Down
64 changes: 64 additions & 0 deletions test/document.populate.test.js
Expand Up @@ -833,4 +833,68 @@ describe('document.populate', function() {
});
});
});

describe('#populated() with getters on embedded schema (gh-7521)', function() {
let Team;
let Player;

before(function() {
const playerSchema = mongoose.Schema({
_id: String,
});

const teamSchema = mongoose.Schema({
captain: {
type: String,
ref: 'gh7521_Player',
get: (v) => {
if (!v || typeof v !== 'string') {
return v;
}

return v.split(' ')[0];
}
},
players: [new mongoose.Schema({
player: {
type: String,
ref: 'gh7521_Player',
get: (v) => {
if (!v || typeof v !== 'string') {
return v;
}

return v.split(' ')[0];
}
}
})],
});

Player = db.model('gh7521_Player', playerSchema);
Team = db.model('gh7521_Team', teamSchema);
});

it('works with populate', function() {
return co(function*() {
const player1 = yield Player.create({ _id: 'John' });
yield Player.create({ _id: 'Foo' });
const createdTeam = yield Team.create({ captain: 'John Doe', players: [{ player: 'John Doe' }, { player: 'Foo Bar' }] });

const team = yield Team.findOne({ _id: createdTeam._id })
.populate({ path: 'captain', options: { getters: true } })
.populate({ path: 'players.player', options: { getters: true } })
.exec();

console.log(team);
console.log(player1);
assert.ok(team.captain);
assert.strictEqual(team.captain._id, 'John');
assert.strictEqual(team.players.length, 2);
assert.ok(team.players[0].player);
assert.ok(team.players[1].player);
assert.strictEqual(team.players[0].player._id, 'John');
assert.strictEqual(team.players[1].player._id, 'Foo');
});
});
});
});

0 comments on commit 3e44bc2

Please sign in to comment.