Skip to content

Commit d219306

Browse files
authored
Merge pull request #13911 from Automattic/vkarpov15/gh-13887
fix(mongoose): correctly handle global applyPluginsToChildSchemas option
2 parents fde2bec + 6304917 commit d219306

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
@@ -694,7 +694,9 @@ Mongoose.prototype._applyPlugins = function(schema, options) {
694694

695695
options = options || {};
696696
options.applyPluginsToDiscriminators = _mongoose.options && _mongoose.options.applyPluginsToDiscriminators || false;
697-
options.applyPluginsToChildSchemas = typeof (_mongoose.options && _mongoose.options.applyPluginsToDiscriminators) === 'boolean' ? _mongoose.options.applyPluginsToDiscriminators : true;
697+
options.applyPluginsToChildSchemas = typeof (_mongoose.options && _mongoose.options.applyPluginsToChildSchemas) === 'boolean' ?
698+
_mongoose.options.applyPluginsToChildSchemas :
699+
true;
698700
applyPlugins(schema, _mongoose.plugins, options, '$globalPluginsApplied');
699701
};
700702

test/index.test.js

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

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

0 commit comments

Comments
 (0)