Skip to content

Commit

Permalink
test(document): repro #8295
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Nov 8, 2019
1 parent 6c1d46a commit d4648ee
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions test/document.test.js
Expand Up @@ -868,19 +868,19 @@ describe('document', function() {
userSchema.virtual('hello').get(function() {
return 'Hello, ' + this.name;
});
const User = db.model('User', userSchema);
const User = db.model('gh1376_User', userSchema);

const groupSchema = new Schema({
name: String,
_users: [{type: Schema.ObjectId, ref: 'User'}]
_users: [{type: Schema.ObjectId, ref: 'gh1376_User'}]
});

const Group = db.model('Group', groupSchema);
const Group = db.model('gh1376_Group', groupSchema);

User.create({name: 'Alice'}, {name: 'Bob'}, function(err, alice, bob) {
assert.ifError(err);

new Group({name: 'mongoose', _users: [alice, bob]}).save(function(err, group) {
Group.create({name: 'mongoose', _users: [alice, bob]}, function(err, group) {
Group.findById(group).populate('_users').exec(function(err, group) {
assert.ifError(err);
assert.ok(group.toJSON()._users[0].hello);
Expand Down Expand Up @@ -8158,4 +8158,28 @@ describe('document', function() {
assert.equal(fromDb.activity.description, 'after');
});
});

it('handles getter setting virtual on manually populated doc when calling toJSON (gh-8295)', function() {
const childSchema = Schema({}, { toJSON: { getters: true } });
childSchema.virtual('field').
get(function() { return this._field; }).
set(function(v) { return this._field = v; });
const Child = db.model('gh8295_Child', childSchema);

const parentSchema = Schema({
child: { type: mongoose.ObjectId, ref: 'gh8295_Child', get }
}, { toJSON: { getters: true } });
const Parent = db.model('gh8295_Parent', parentSchema);

function get(child) {
child.field = true;
return child;
}

let p = new Parent({ child: new Child({}) });
assert.strictEqual(p.toJSON().child.field, true);

p = new Parent({ child: new Child({}) });
assert.strictEqual(p.child.toJSON().field, true);
});
});

0 comments on commit d4648ee

Please sign in to comment.