All files / src/plugins/utils mergeAllOfSchemaProperties.js

100% Statements 12/12
100% Branches 8/8
100% Functions 3/3
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3130x       30x 31x 31x   31x 31x 3x     5x     31x           44x 37x         7x    
const { isObject, mergeWith } = require('lodash');
 
// Takes a schema, and if an allOf field is provided,
// merges all allOf schema properties to create one schema
module.exports = function(schema) {
  const mergedSchema = Object.assign({}, schema);
  const allOfArr = mergedSchema['allOf'];
  // delete now, so it is not merged back into the final result
  delete mergedSchema['allOf'];
  if (allOfArr && Array.isArray(allOfArr)) {
    allOfArr.forEach(function(allOfSchema) {
      // deep merges the allOf schema into the aggregate schema
      // uses a function to concatenate arrays instead of overwriting
      mergeWith(mergedSchema, allOfSchema, customizer);
    });
  }
  return mergedSchema;
};
 
function customizer(objValue, srcValue) {
  // keep the original values for non-object fields instead of overwriting
  // will maintain the `type`, `description`, `summary`, etc. fields
  if (!isObject(objValue)) {
    return objValue;
  }
  // if the object is an array combine the arrays
  // otherwise, not an array, so return undefined and
  // mergeWith will do default object deep merging
  return Array.isArray(objValue) ? objValue.concat(srcValue) : undefined;
}