From bdfea16779b1372635f1289b5794b087ef714021 Mon Sep 17 00:00:00 2001 From: Luca Pizzini Date: Tue, 1 Nov 2022 10:23:38 +0100 Subject: [PATCH] applied requested changes used applyPlugins option in discriminator helper instead of creating new option mergePlugins --- lib/helpers/model/discriminator.js | 8 +++----- lib/model.js | 3 ++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/helpers/model/discriminator.js b/lib/helpers/model/discriminator.js index 574df112930..96443002ab1 100644 --- a/lib/helpers/model/discriminator.js +++ b/lib/helpers/model/discriminator.js @@ -19,13 +19,12 @@ const CUSTOMIZABLE_DISCRIMINATOR_OPTIONS = { * ignore */ -module.exports = function discriminator(model, name, schema, tiedValue, applyPlugins, mergeHooks, mergePlugins) { +module.exports = function discriminator(model, name, schema, tiedValue, applyPlugins, mergeHooks) { if (!(schema && schema.instanceOfSchema)) { throw new Error('You must pass a valid discriminator Schema'); } mergeHooks = mergeHooks == null ? true : mergeHooks; - mergePlugins = mergePlugins == null ? true : mergePlugins; if (model.schema.discriminatorMapping && !model.schema.discriminatorMapping.isRoot) { @@ -35,7 +34,7 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu if (applyPlugins) { const applyPluginsToDiscriminators = get(model.base, - 'options.applyPluginsToDiscriminators', false) || !mergeHooks || !mergePlugins; + 'options.applyPluginsToDiscriminators', false) || !mergeHooks; // Even if `applyPluginsToDiscriminators` isn't set, we should still apply // global plugins to schemas embedded in the discriminator schema (gh-7370) model.base._applyPlugins(schema, { @@ -185,8 +184,7 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu if (mergeHooks) { schema.s.hooks = model.schema.s.hooks.merge(schema.s.hooks); } - - if (mergePlugins) { + if (applyPlugins) { schema.plugins = Array.prototype.slice.call(baseSchema.plugins); } schema.callQueue = baseSchema.callQueue.concat(schema.callQueue); diff --git a/lib/model.js b/lib/model.js index 4741a49e773..8f876bc3321 100644 --- a/lib/model.js +++ b/lib/model.js @@ -1233,6 +1233,7 @@ Model.discriminator = function(name, schema, options) { options = options || {}; const value = utils.isPOJO(options) ? options.value : options; const clone = typeof options.clone === 'boolean' ? options.clone : true; + const mergePlugins = typeof options.mergePlugins === 'boolean' ? options.mergePlugins : true; _checkContext(this, 'discriminator'); @@ -1243,7 +1244,7 @@ Model.discriminator = function(name, schema, options) { schema = schema.clone(); } - schema = discriminator(this, name, schema, value, true, options.mergeHooks, options.mergePlugins); + schema = discriminator(this, name, schema, value, mergePlugins, options.mergeHooks); if (this.db.models[name] && !schema.options.overwriteModels) { throw new OverwriteModelError(name); }