Skip to content

Commit

Permalink
Merge pull request #13945 from hasezoey/backport-13911-6x
Browse files Browse the repository at this point in the history
Backport to 6.x: correctly handle global applyPluginsToChildSchemas option
  • Loading branch information
vkarpov15 authored Oct 8, 2023
2 parents 4dcc0d8 + ea85361 commit 29b09d3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,9 @@ Mongoose.prototype._applyPlugins = function(schema, options) {

options = options || {};
options.applyPluginsToDiscriminators = _mongoose.options && _mongoose.options.applyPluginsToDiscriminators || false;
options.applyPluginsToChildSchemas = typeof (_mongoose.options && _mongoose.options.applyPluginsToDiscriminators) === 'boolean' ? _mongoose.options.applyPluginsToDiscriminators : true;
options.applyPluginsToChildSchemas = typeof (_mongoose.options && _mongoose.options.applyPluginsToChildSchemas) === 'boolean' ?
_mongoose.options.applyPluginsToChildSchemas :
true;
applyPlugins(schema, _mongoose.plugins, options, '$globalPluginsApplied');
};

Expand Down
19 changes: 19 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,25 @@ describe('mongoose module:', function() {
return Promise.resolve();
});

it('global plugins with applyPluginsToChildSchemas (gh-13887)', function() {
const m = new Mongoose();
m.set('applyPluginsToChildSchemas', false);

const called = [];
m.plugin(function(s) {
called.push(s);
});

const schema = new m.Schema({
subdoc: new m.Schema({ name: String }),
arr: [new m.Schema({ name: String })]
});

m.model('Test', schema);
assert.equal(called.length, 1);
assert.ok(called.indexOf(schema) !== -1);
});

it('global plugins recompile schemas (gh-7572)', function() {
function helloPlugin(schema) {
schema.virtual('greeting').get(() => 'hello');
Expand Down

0 comments on commit 29b09d3

Please sign in to comment.