From 82fd5e4e5d576c2dd1d0d69bca9df02167943861 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 27 Feb 2023 12:22:55 -0500 Subject: [PATCH] BREAKING CHANGE: copy schema options when merging schemas using `new Schema()` or `Schema.prototype.add()` --- lib/helpers/schema/merge.js | 7 +++++++ test/schema.test.js | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) 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 }); }); });