Skip to content

Commit

Permalink
applied requested changes
Browse files Browse the repository at this point in the history
used applyPlugins option in discriminator helper instead of creating new option mergePlugins
  • Loading branch information
lpizzinidev committed Nov 1, 2022
1 parent d4ebadc commit bdfea16
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
8 changes: 3 additions & 5 deletions lib/helpers/model/discriminator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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, {
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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);
}
Expand Down

0 comments on commit bdfea16

Please sign in to comment.