Resolving properties as a reusable component

Resolve each property as a different schema instead of inline
This commit is contained in:
Erik Mendoza
2022-06-07 21:39:53 -05:00
parent b9a01676de
commit 446e8651e5
30 changed files with 1141 additions and 195 deletions

View File

@@ -6,26 +6,7 @@ const slashes = /\//g,
escapedTilde = /~0/g,
jsonPointerLevelSeparator = '/',
escapedTildeString = '~0',
COMPONENTS_KEYS = [
'schemas',
'responses',
'parameters',
'examples',
'requestBodies',
'headers',
'securitySchemes',
'links',
'callbacks'
],
SCHEMA_PARENT_KEYS_IN_DOC = [
'allOf',
'oneOf',
'anyOf',
'not',
'additionalProperties',
'items',
'schema'
];
{ getKeyInComponents30 } = require('./30XUtils/componentsParentMatcher');
/**
* Encodes a filepath name so it can be a json pointer
@@ -52,49 +33,14 @@ function jsonPointerDecodeAndReplace(filePathName) {
* @param {string} traceFromParent the node trace from root.
* @param {string} filePathName the filePathName of the file
* @param {string} localPath the local path that the pointer will reach
* @param {string} version - The current spec version
* @returns {Array} - the calculated keys in an array representing each nesting property name
*/
function getKeyInComponents(traceFromParent, filePathName, localPath) {
const localPart = localPath ? `${localPointer}${localPath}` : '';
let res = traceFromParent,
trace = [],
traceToKey = [],
matchFound = false,
inComponents = false;
if (traceFromParent[0] === 'components') {
inComponents = true;
return [[], inComponents];
}
res.push(jsonPointerDecodeAndReplace(`${filePathName}${localPart}`));
trace = [...res].reverse();
for (let item of trace) {
if (SCHEMA_PARENT_KEYS_IN_DOC.includes(item)) {
item = 'schemas';
}
traceToKey.push(item);
if (COMPONENTS_KEYS.includes(item)) {
matchFound = true;
break;
}
}
return [matchFound ?
traceToKey.reverse() :
[], inComponents];
}
/**
* returns the local path of a pointer #/definitions/dog etc.
* @param {string} jsonPointer the complet pointer
* @returns {string} - the calculated key
*/
function getLocalPath(jsonPointer) {
if (jsonPointer.includes(localPointer)) {
return jsonPointer.split(localPointer)[1];
}
return '';
let result;
result = getKeyInComponents30(traceFromParent, filePathName, localPart, jsonPointerDecodeAndReplace);
return result;
}
/**
@@ -102,13 +48,14 @@ function getLocalPath(jsonPointer) {
* @constructor
* @param {Function} encodeFunction function to encode url
* @param {string} traceFromParent the trace from parent.
* @param {string} targetInRoot - The root element where we will point
* @returns {string} - the concatenated json pointer
*/
function concatJsonPointer(encodeFunction, traceFromParent) {
function concatJsonPointer(encodeFunction, traceFromParent, targetInRoot) {
const traceFromParentAsString = traceFromParent.map((trace) => {
return encodeFunction(trace);
}).join('/');
return localPointer + '/components' + jsonPointerLevelSeparator + traceFromParentAsString;
return localPointer + targetInRoot + jsonPointerLevelSeparator + traceFromParentAsString;
}
/**
@@ -117,14 +64,15 @@ function concatJsonPointer(encodeFunction, traceFromParent) {
* @param {Function} encodeFunction function to encode url
* @param {string} refValue the type of component e.g. schemas, parameters, etc.
* @param {string} traceFromKey the trace from the parent node.
* @param {string} version - The version we are working on
* @returns {string} - the concatenated json pointer
*/
function getJsonPointerRelationToRoot(encodeFunction, refValue, traceFromKey) {
let targetInRoot = '/components';
if (refValue.startsWith(localPointer)) {
return refValue;
}
const localPath = getLocalPath(refValue);
return concatJsonPointer(encodeFunction, traceFromKey, localPath);
return concatJsonPointer(encodeFunction, traceFromKey, targetInRoot);
}
/**