Skip to content

Commit 73cea77

Browse files
authored
Merge pull request #13092 from Automattic/vkarpov15/merge-schema-options
BREAKING CHANGE: copy schema options when merging schemas using `new Schema()` or `Schema.prototype.add()`
2 parents 28fd1b6 + 82fd5e4 commit 73cea77

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/helpers/schema/merge.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ module.exports = function merge(s1, s2, skipConflictingPaths) {
1515
s1.method(s2.methods);
1616
s1.static(s2.statics);
1717

18+
for (const [option, value] of Object.entries(s2._userProvidedOptions)) {
19+
if (!(option in s1._userProvidedOptions)) {
20+
s1._userProvidedOptions[option] = value;
21+
s1.options[option] = value;
22+
}
23+
}
24+
1825
for (const query in s2.query) {
1926
s1.query[query] = s2.query[query];
2027
}

test/schema.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,8 +1391,24 @@ describe('schema', function() {
13911391
assert.ok(s.path('created'));
13921392
assert.ok(s.path('name'));
13931393
assert.ok(!s.options.id);
1394+
});
1395+
1396+
it('copies options from array of schemas', function() {
1397+
const baseSchema = new Schema({ created: Date }, { id: true, toJSON: { virtuals: true } });
1398+
const s = new Schema([baseSchema, { name: String }]);
13941399

1400+
assert.ok(s.path('created'));
1401+
assert.ok(s.path('name'));
1402+
assert.ok(s.options.id);
1403+
assert.deepEqual(s.options.toJSON, { virtuals: true });
1404+
assert.strictEqual(s.options.toObject, undefined);
13951405

1406+
s.add(new Schema({}, { toObject: { getters: true } }));
1407+
assert.ok(s.path('created'));
1408+
assert.ok(s.path('name'));
1409+
assert.ok(s.options.id);
1410+
assert.deepEqual(s.options.toJSON, { virtuals: true });
1411+
assert.deepEqual(s.options.toObject, { getters: true });
13961412
});
13971413
});
13981414

0 commit comments

Comments
 (0)