diff --git a/lib/helpers/schema/merge.js b/lib/helpers/schema/merge.js index 55ed82466f7..aeb8c453b00 100644 --- a/lib/helpers/schema/merge.js +++ b/lib/helpers/schema/merge.js @@ -15,6 +15,13 @@ module.exports = function merge(s1, s2, skipConflictingPaths) { s1.method(s2.methods); s1.static(s2.statics); + for (const [option, value] of Object.entries(s2._userProvidedOptions)) { + if (!(option in s1._userProvidedOptions)) { + s1._userProvidedOptions[option] = value; + s1.options[option] = value; + } + } + for (const query in s2.query) { s1.query[query] = s2.query[query]; } diff --git a/test/schema.test.js b/test/schema.test.js index 8ec9a32c5f0..cb334f0ce79 100644 --- a/test/schema.test.js +++ b/test/schema.test.js @@ -1391,8 +1391,24 @@ describe('schema', function() { assert.ok(s.path('created')); assert.ok(s.path('name')); assert.ok(!s.options.id); + }); + + it('copies options from array of schemas', function() { + const baseSchema = new Schema({ created: Date }, { id: true, toJSON: { virtuals: true } }); + const s = new Schema([baseSchema, { name: String }]); + assert.ok(s.path('created')); + assert.ok(s.path('name')); + assert.ok(s.options.id); + assert.deepEqual(s.options.toJSON, { virtuals: true }); + assert.strictEqual(s.options.toObject, undefined); + s.add(new Schema({}, { toObject: { getters: true } })); + assert.ok(s.path('created')); + assert.ok(s.path('name')); + assert.ok(s.options.id); + assert.deepEqual(s.options.toJSON, { virtuals: true }); + assert.deepEqual(s.options.toObject, { getters: true }); }); });