Skip to content

Commit

Permalink
Merge pull request Automattic#12452 from Automattic/Automatticgh-9541
Browse files Browse the repository at this point in the history
Gh 9541 create mergeDiscriminatorSchema function
  • Loading branch information
vkarpov15 committed Sep 30, 2022
2 parents 76894b4 + 4da36f6 commit 3107642
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
63 changes: 63 additions & 0 deletions lib/helpers/discriminator/mergeDiscriminatorSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';
const schemaMerge = require('../schema/merge');
const specialProperties = require('../../helpers/specialProperties');
const isBsonType = require('../../helpers/isBsonType');
const ObjectId = require('../../types/objectid');
const isObject = require('../../helpers/isObject');
/**
* Merges `from` into `to` without overwriting existing properties.
*
* @param {Object} to
* @param {Object} from
* @param {String} [path]
* @api private
*/

module.exports = function mergeDiscriminatorSchema(to, from, path) {
const keys = Object.keys(from);
let i = 0;
const len = keys.length;
let key;

path = path || '';

while (i < len) {
key = keys[i++];
if (key === 'discriminators' || key === 'base' || key === '_applyDiscriminators') {
continue;
}
if (path === 'tree' && from != null && from.instanceOfSchema) {
continue;
}
if (specialProperties.has(key)) {
continue;
}
if (to[key] == null) {
to[key] = from[key];
} else if (isObject(from[key])) {
if (!isObject(to[key])) {
to[key] = {};
}
if (from[key] != null) {
// Skip merging schemas if we're creating a discriminator schema and
// base schema has a given path as a single nested but discriminator schema
// has the path as a document array, or vice versa (gh-9534)
if ((from[key].$isSingleNested && to[key].$isMongooseDocumentArray) ||
(from[key].$isMongooseDocumentArray && to[key].$isSingleNested)) {
continue;
} else if (from[key].instanceOfSchema) {
if (to[key].instanceOfSchema) {
schemaMerge(to[key], from[key].clone(), true);
} else {
to[key] = from[key].clone();
}
continue;
} else if (isBsonType(from[key], 'ObjectID')) {
to[key] = new ObjectId(from[key]);
continue;
}
}
mergeDiscriminatorSchema(to[key], from[key], path ? path + '.' + key : key);
}
}
};
4 changes: 2 additions & 2 deletions lib/helpers/model/discriminator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Mixed = require('../../schema/mixed');
const defineKey = require('../document/compile').defineKey;
const get = require('../get');
const utils = require('../../utils');
const mergeDiscriminatorSchema = require('../../helpers/discriminator/mergeDiscriminatorSchema');

const CUSTOMIZABLE_DISCRIMINATOR_OPTIONS = {
toJSON: true,
Expand Down Expand Up @@ -108,8 +109,7 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
}
}

utils.merge(schema, baseSchema, {
isDiscriminatorSchemaMerge: true,
mergeDiscriminatorSchema(schema, baseSchema, {
omit: { discriminators: true, base: true, _applyDiscriminators: true },
omitNested: conflictingPaths.reduce((cur, path) => {
cur['tree.' + path] = true;
Expand Down

0 comments on commit 3107642

Please sign in to comment.