Skip to content

Commit

Permalink
test(document): repro #9995
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Mar 18, 2021
1 parent 0d0c820 commit 5d96508
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10169,4 +10169,41 @@ describe('document', function() {
assert.equal(fromDb.elements[0].subelements[0].text, 'my text');
});
});

it('toObject() uses child schema `flattenMaps` option by default (gh-9995)', function() {
const MapSchema = new Schema({
value: { type: Number }
}, { _id: false });

const ChildSchema = new Schema({
map: { type: Map, of: MapSchema }
});
ChildSchema.set('toObject', { flattenMaps: true });

const ParentSchema = new Schema({
child: { type: Schema.ObjectId, ref: 'Child' }
});

const ChildModel = db.model('Child', ChildSchema);
const ParentModel = db.model('Parent', ParentSchema);

return co(function*() {
const childDocument = new ChildModel({
map: { first: { value: 1 }, second: { value: 2 } }
});
yield childDocument.save();

const parentDocument = new ParentModel({ child: childDocument });
yield parentDocument.save();

const resultDocument = yield ParentModel.findOne().populate('child').exec();

let resultObject = resultDocument.toObject();
assert.ok(resultObject.child.map);
assert.ok(!(resultObject.child.map instanceof Map));

resultObject = resultDocument.toObject({ flattenMaps: false });
assert.ok(resultObject.child.map instanceof Map);
});
});
});

0 comments on commit 5d96508

Please sign in to comment.