Skip to content

Commit ea85361

Browse files
vkarpov15hasezoey
authored andcommitted
fix(mongoose): correctly handle global applyPluginsToChildSchemas option
Fix #13887
1 parent 917f2ff commit ea85361

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,9 @@ Mongoose.prototype._applyPlugins = function(schema, options) {
718718

719719
options = options || {};
720720
options.applyPluginsToDiscriminators = _mongoose.options && _mongoose.options.applyPluginsToDiscriminators || false;
721-
options.applyPluginsToChildSchemas = typeof (_mongoose.options && _mongoose.options.applyPluginsToDiscriminators) === 'boolean' ? _mongoose.options.applyPluginsToDiscriminators : true;
721+
options.applyPluginsToChildSchemas = typeof (_mongoose.options && _mongoose.options.applyPluginsToChildSchemas) === 'boolean' ?
722+
_mongoose.options.applyPluginsToChildSchemas :
723+
true;
722724
applyPlugins(schema, _mongoose.plugins, options, '$globalPluginsApplied');
723725
};
724726

test/index.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,25 @@ describe('mongoose module:', function() {
430430
return Promise.resolve();
431431
});
432432

433+
it('global plugins with applyPluginsToChildSchemas (gh-13887)', function() {
434+
const m = new Mongoose();
435+
m.set('applyPluginsToChildSchemas', false);
436+
437+
const called = [];
438+
m.plugin(function(s) {
439+
called.push(s);
440+
});
441+
442+
const schema = new m.Schema({
443+
subdoc: new m.Schema({ name: String }),
444+
arr: [new m.Schema({ name: String })]
445+
});
446+
447+
m.model('Test', schema);
448+
assert.equal(called.length, 1);
449+
assert.ok(called.indexOf(schema) !== -1);
450+
});
451+
433452
it('global plugins recompile schemas (gh-7572)', function() {
434453
function helloPlugin(schema) {
435454
schema.virtual('greeting').get(() => 'hello');

0 commit comments

Comments
 (0)