Skip to content

Commit

Permalink
Merge pull request #13092 from Automattic/vkarpov15/merge-schema-options
Browse files Browse the repository at this point in the history
BREAKING CHANGE: copy schema options when merging schemas using `new Schema()` or `Schema.prototype.add()`
  • Loading branch information
vkarpov15 authored Feb 27, 2023
2 parents 28fd1b6 + 82fd5e4 commit 73cea77
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/helpers/schema/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
16 changes: 16 additions & 0 deletions test/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
});

Expand Down

0 comments on commit 73cea77

Please sign in to comment.