mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
Adding bundle files proces
This commit is contained in:
276
lib/bundle.js
276
lib/bundle.js
@@ -119,13 +119,112 @@ function getRootFileTrace(nodeParents) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a trace from the root to the current item
|
||||
* @param {array} nodeTrace - The trace from the current file to the current element
|
||||
* @param {*} connector - The trace from the root's document context to the current file context
|
||||
* @returns {array} The merged trace from the current item to the root's context
|
||||
* Get partial content from file content
|
||||
* @param {object} content - The content in related node
|
||||
* @param {string} partial - The partial part from reference
|
||||
* @returns {object} The related content to the trace
|
||||
*/
|
||||
function getTraceFromParent(nodeTrace, connector) {
|
||||
return connector.concat(nodeTrace);
|
||||
function getContentFromTrace(content, partial) {
|
||||
partial = partial[0] === '/' ? partial.substring(1) : partial;
|
||||
const trace = partial.split('/');
|
||||
let currentValue = content;
|
||||
for (let place of trace) {
|
||||
currentValue = currentValue[place];
|
||||
}
|
||||
return currentValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a value in the global components object following the provided trace
|
||||
* @param {array} keyInComponents - The trace to the key in components
|
||||
* @param {object} components - A global components object
|
||||
* @param {object} value - The value from node matched with data
|
||||
* @returns {null} It modifies components global context
|
||||
*/
|
||||
function setValueInComponents(keyInComponents, components, value) {
|
||||
let currentPlace = components,
|
||||
target = keyInComponents[keyInComponents.length - 2],
|
||||
referencedPart = keyInComponents[keyInComponents.length - 1],
|
||||
[, local] = referencedPart.split('#'),
|
||||
key = keyInComponents.length === 2 && keyInComponents[0] === 'schema' ?
|
||||
keyInComponents[1] :
|
||||
null;
|
||||
if (keyInComponents[0] === 'schema') {
|
||||
keyInComponents[0] = 'schemas';
|
||||
target = key;
|
||||
}
|
||||
|
||||
for (let place of keyInComponents) {
|
||||
if (place === target) {
|
||||
if (local) {
|
||||
value = getContentFromTrace(value, local);
|
||||
}
|
||||
currentPlace[place] = value;
|
||||
break;
|
||||
}
|
||||
else if (currentPlace[place]) {
|
||||
currentPlace = currentPlace[place];
|
||||
}
|
||||
else {
|
||||
currentPlace[place] = {};
|
||||
currentPlace = currentPlace[place];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a trace from the current node's root to the place where we find a $ref
|
||||
* @param {object} nodeContext - The current node we are processing
|
||||
* @param {object} property - The current property that contains the $ref
|
||||
* @param {string} parentFilename - The parent's filename
|
||||
* @returns {array} The trace to the place where the $ref appears
|
||||
*/
|
||||
function getTraceFromParent(nodeContext, property, parentFilename) {
|
||||
const parents = [...nodeContext.parents].reverse(),
|
||||
key = nodeContext.key,
|
||||
nodeParentsKey = [key, ...parents.map((parent) => {
|
||||
return parent.key;
|
||||
})],
|
||||
nodeTrace = getRootFileTrace(nodeParentsKey),
|
||||
cleanFileName = (filename) => {
|
||||
const [file, local] = filename.split('#');
|
||||
return [calculatePath(parentFilename, file), local];
|
||||
},
|
||||
[file, local] = cleanFileName(property.$ref),
|
||||
keyInComponents = getKeyInComponents(nodeTrace, file, local);
|
||||
return keyInComponents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the key trace if this node will be included in components else returns an empty array
|
||||
* @param {array} nodeTrace - The trace from file to the $ref
|
||||
* @returns {array} An arrat with the trace to the key in components
|
||||
*/
|
||||
function getKeyParent(nodeTrace) {
|
||||
const componentsKeys = [
|
||||
'schemas',
|
||||
'schema',
|
||||
'responses',
|
||||
'parameters',
|
||||
'examples',
|
||||
'requestBodies',
|
||||
'headers',
|
||||
'securitySchemes',
|
||||
'links',
|
||||
'callbacks'
|
||||
],
|
||||
trace = [...nodeTrace].reverse();
|
||||
let traceToKey = [];
|
||||
|
||||
for (let item of trace) {
|
||||
traceToKey.push(item);
|
||||
if (componentsKeys.includes(item)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return traceToKey.length === trace.length ?
|
||||
[] :
|
||||
traceToKey.reverse();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,11 +234,11 @@ function getTraceFromParent(nodeTrace, connector) {
|
||||
* @param {Function} pathSolver - function to resolve the Path
|
||||
* @param {string} parentFilename - The parent's filename
|
||||
* @param {object} globalComponentsContext - The global context from root file
|
||||
* @param {array} allData The data from files provided in the input
|
||||
* @returns {object} - {path : $ref value}
|
||||
*/
|
||||
function getReferences (currentNode, refTypeResolver, pathSolver, parentFilename, globalComponentsContext) {
|
||||
function getReferences (currentNode, refTypeResolver, pathSolver, parentFilename, globalComponentsContext, allData) {
|
||||
let referencesInNode = [];
|
||||
|
||||
traverseUtility(currentNode).forEach(function (property) {
|
||||
if (property) {
|
||||
let hasReferenceTypeKey;
|
||||
@@ -150,66 +249,39 @@ function getReferences (currentNode, refTypeResolver, pathSolver, parentFilename
|
||||
}
|
||||
);
|
||||
if (hasReferenceTypeKey) {
|
||||
const parents = [...this.parents].reverse(),
|
||||
key = this.key,
|
||||
nodeParentsKey = [key, ...parents.map((parent) => {
|
||||
return parent.key;
|
||||
})],
|
||||
nodeTrace = getRootFileTrace(nodeParentsKey),
|
||||
connectorFromParent = globalComponentsContext[parentFilename] ?
|
||||
globalComponentsContext[parentFilename].connector :
|
||||
[],
|
||||
traceFromParent = getTraceFromParent(nodeTrace, connectorFromParent),
|
||||
cleanFileName = (filename) => {
|
||||
const [file, local] = filename.split('#');
|
||||
return [calculatePath(parentFilename, file), local];
|
||||
},
|
||||
[file, local] = cleanFileName(property.$ref),
|
||||
newValue = Object.assign({}, this.node),
|
||||
keyInComponents = getKeyInComponents(traceFromParent, file, local, connectorFromParent),
|
||||
const nodeTrace = getTraceFromParent(this, property, parentFilename),
|
||||
keyParent = getKeyParent(nodeTrace),
|
||||
referenceInDocument = getJsonPointerRelationToRoot(
|
||||
jsonPointerEncodeAndReplace,
|
||||
file,
|
||||
property.$ref,
|
||||
traceFromParent
|
||||
keyParent
|
||||
);
|
||||
let newValue,
|
||||
nodeData = findNodeFromPath(calculatePath(parentFilename, property.$ref), allData).content,
|
||||
nodeContent = nodeData ?
|
||||
parse.getOasObject(nodeData).oasObject :
|
||||
{ $missedReference: `property ${property.$ref} was not provided in data` };
|
||||
|
||||
newValue.$ref = referenceInDocument;
|
||||
this.update(newValue);
|
||||
|
||||
if (globalComponentsContext[file]) {
|
||||
globalComponentsContext[file].isFull =
|
||||
globalComponentsContext[file].isFull && !local;
|
||||
if (local) {
|
||||
globalComponentsContext[file].partialCalled.push(local);
|
||||
}
|
||||
if (keyParent.length === 0) {
|
||||
newValue = nodeContent;
|
||||
}
|
||||
else {
|
||||
globalComponentsContext[file] = {
|
||||
calledFrom: parentFilename,
|
||||
connector: keyInComponents,
|
||||
isFull: !local,
|
||||
partialsCalled: local ? [local] : [],
|
||||
referenceInDocument,
|
||||
content: this.node
|
||||
};
|
||||
newValue = Object.assign({}, this.node);
|
||||
newValue.$ref = referenceInDocument;
|
||||
}
|
||||
globalComponentsContext[property.$ref] = {
|
||||
newValue: newValue,
|
||||
keyInComponents: keyParent,
|
||||
nodeContent
|
||||
};
|
||||
|
||||
if (!added(property.$ref, referencesInNode)) {
|
||||
referencesInNode.push({ path: pathSolver(property), keyInComponents });
|
||||
referencesInNode.push({ path: pathSolver(property), keyInComponents: keyParent, newValue: this.node });
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
if (globalComponentsContext[parentFilename]) {
|
||||
globalComponentsContext[parentFilename].content = currentNode.oasObject;
|
||||
}
|
||||
else {
|
||||
globalComponentsContext[parentFilename] = {
|
||||
isRoot: true,
|
||||
filename: parentFilename,
|
||||
content: currentNode.oasObject
|
||||
};
|
||||
}
|
||||
|
||||
return referencesInNode;
|
||||
}
|
||||
|
||||
@@ -223,17 +295,16 @@ function getReferences (currentNode, refTypeResolver, pathSolver, parentFilename
|
||||
*/
|
||||
function getAdjacentAndMissingToBundle (currentNode, allData, specRoot, globalComponentsContext) {
|
||||
let currentNodeReferences,
|
||||
currentContent = currentNode.content,
|
||||
graphAdj = [],
|
||||
missingNodes = [],
|
||||
bundleDataInAdjacent = [],
|
||||
OASObject;
|
||||
|
||||
if (currentContent.parsed) {
|
||||
OASObject = currentNode.parsed;
|
||||
if (currentNode.parsed) {
|
||||
OASObject = currentNode.parsed.oasObject;
|
||||
}
|
||||
else {
|
||||
OASObject = parse.getOasObject(currentContent);
|
||||
OASObject = parse.getOasObject(currentNode.content).oasObject;
|
||||
}
|
||||
|
||||
currentNodeReferences = getReferences(
|
||||
@@ -241,14 +312,15 @@ function getAdjacentAndMissingToBundle (currentNode, allData, specRoot, globalCo
|
||||
isExtRef,
|
||||
removeLocalReferenceFromPath,
|
||||
currentNode.fileName,
|
||||
globalComponentsContext
|
||||
globalComponentsContext,
|
||||
allData
|
||||
);
|
||||
|
||||
currentNodeReferences.forEach((reference) => {
|
||||
let referencePath = reference.path,
|
||||
adjacentNode = findNodeFromPath(calculatePath(currentNode.fileName, referencePath), allData);
|
||||
|
||||
if (adjacentNode) {
|
||||
bundleDataInAdjacent.push({ reference, adjacentNode, currentNode });
|
||||
graphAdj.push(adjacentNode);
|
||||
}
|
||||
else if (!comparePaths(referencePath, specRoot.fileName)) {
|
||||
@@ -267,56 +339,39 @@ function getAdjacentAndMissingToBundle (currentNode, allData, specRoot, globalCo
|
||||
return { graphAdj, missingNodes, bundleDataInAdjacent, currentNode };
|
||||
}
|
||||
|
||||
// function fillExistentComponents(components, componentsObject) {
|
||||
// Object.keys(components).forEach((key) => {
|
||||
// componentsObject[key] = components[key];
|
||||
// });
|
||||
// return componentsObject;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Convert the current key data in document context to an item in components object
|
||||
* @param {array} namesArray - The conector from root related with the current item
|
||||
* @param {object} target - The components global object where the result will be added
|
||||
* @param {string} dataKey - The current key in the document context
|
||||
* @param {object} documentContext - The document context data necesary to generate the component's items
|
||||
* @returns {object} The object related with the current key in document context
|
||||
*/
|
||||
function convert(namesArray, target, dataKey, documentContext) {
|
||||
let result = target,
|
||||
nestedObj = result;
|
||||
for (let [index, name] of namesArray.entries()) {
|
||||
let nextName = namesArray[index + 1];
|
||||
if (documentContext[name]) {
|
||||
continue;
|
||||
}
|
||||
else if (documentContext[nextName]) {
|
||||
nestedObj[name] = documentContext[nextName].content;
|
||||
}
|
||||
else if (!nestedObj[name]) {
|
||||
nestedObj[name] = {};
|
||||
}
|
||||
nestedObj = nestedObj[name];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the components object from the documentContext data
|
||||
* @param {object} documentContext The document context from root
|
||||
* @param {string} rootFilename - The root's filename
|
||||
* @param {object} rootContent - The root's parsed content
|
||||
* @param {function} refTypeResolver - The resolver function to test if node has a reference
|
||||
* @param {object} components - The global components object
|
||||
* @returns {object} The components object related to the file
|
||||
*/
|
||||
function generateComponentsObject (documentContext, rootFilename) {
|
||||
let components = {};
|
||||
Object.keys(documentContext).forEach((dataKey) => {
|
||||
if (dataKey === rootFilename) {
|
||||
return;
|
||||
}
|
||||
convert(documentContext[dataKey].connector, components, dataKey, documentContext);
|
||||
function generateComponentsObject (documentContext, rootContent, refTypeResolver, components) {
|
||||
[rootContent, components].forEach((contentData) => {
|
||||
traverseUtility(contentData).forEach(function (property) {
|
||||
if (property) {
|
||||
let hasReferenceTypeKey;
|
||||
hasReferenceTypeKey = Object.keys(property)
|
||||
.find(
|
||||
(key) => {
|
||||
return refTypeResolver(property, key);
|
||||
}
|
||||
);
|
||||
if (hasReferenceTypeKey) {
|
||||
let refData = documentContext[property.$ref];
|
||||
this.update(refData.newValue);
|
||||
if (refData.keyInComponents.length > 0) {
|
||||
setValueInComponents(
|
||||
refData.keyInComponents,
|
||||
components,
|
||||
refData.nodeContent
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
return components;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
@@ -332,13 +387,18 @@ module.exports = {
|
||||
path = pathBrowserify;
|
||||
}
|
||||
let algorithm = new DFS(),
|
||||
globalComponentsContext = {};
|
||||
globalComponentsContext = {},
|
||||
components = {};
|
||||
|
||||
algorithm.traverseAndBundle(specRoot, (currentNode) => {
|
||||
return getAdjacentAndMissingToBundle(currentNode, allData, specRoot, globalComponentsContext);
|
||||
});
|
||||
|
||||
return generateComponentsObject(globalComponentsContext, specRoot.fileName);
|
||||
generateComponentsObject(globalComponentsContext, specRoot.parsed.oasObject, isExtRef, components);
|
||||
return {
|
||||
fileContent: specRoot.parsed.oasObject,
|
||||
components
|
||||
};
|
||||
},
|
||||
|
||||
bundleFiles: function(data) {
|
||||
@@ -351,7 +411,7 @@ module.exports = {
|
||||
Object.keys(bundleData).forEach((key) => {
|
||||
if (bundleData[key].hasOwnProperty('components')) {
|
||||
if (componentsFromFile) {
|
||||
throw new Error('Muyltiple components definition through your files');
|
||||
throw new Error('Multiple components definition through your files');
|
||||
}
|
||||
components = fillExistentComponents(bundleData.key.components, components);
|
||||
componentsFromFile = true;
|
||||
|
||||
@@ -30,7 +30,6 @@ class DFS {
|
||||
}
|
||||
|
||||
traverseAndBundle(node, getAdjacentAndBundle) {
|
||||
const mainNode = node;
|
||||
let traverseOrder = [],
|
||||
stack = [],
|
||||
missing = [],
|
||||
@@ -42,9 +41,9 @@ class DFS {
|
||||
if (!visited.has(node)) {
|
||||
traverseOrder.push(node);
|
||||
visited.add(node);
|
||||
let { graphAdj, missingNodes } = getAdjacentAndBundle(node);
|
||||
let { graphAdj, missingNodes, bundleDataInAdjacent } = getAdjacentAndBundle(node);
|
||||
missing.push(...missingNodes);
|
||||
bundleData.push(bundleData);
|
||||
bundleData.push(...bundleDataInAdjacent);
|
||||
for (let j = 0; j < graphAdj.length; j++) {
|
||||
stack.push(graphAdj[j]);
|
||||
}
|
||||
@@ -59,7 +58,7 @@ class DFS {
|
||||
].map((str) => {
|
||||
return JSON.parse(str);
|
||||
});
|
||||
return { traverseOrder, missing, bundleData, mainNode };
|
||||
return { traverseOrder, missing, bundleData };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,20 +34,13 @@ function jsonPointerDecodeAndReplace(filePathName) {
|
||||
* @param {string} localPath the local path that the pointer will reach
|
||||
* @returns {Array} - the calculated keys in an array representing each nesting property name
|
||||
*/
|
||||
function getKeyInComponents(traceFromParent, filePathName) {
|
||||
function getKeyInComponents(traceFromParent, filePathName, localPath) {
|
||||
const localPart = localPath ? `#${localPath}` : '';
|
||||
let res = traceFromParent;
|
||||
res.push(jsonPointerDecodeAndReplace(filePathName));
|
||||
// TODOE: Add local support
|
||||
// if (localPath) {
|
||||
// if (localPath.startsWith(jsonPointerLevelSeparator)) {
|
||||
// localPathToCheck = localPath.substring(1);
|
||||
// }
|
||||
// pointer = localPathToCheck.split(jsonPointerLevelSeparator);
|
||||
// for (let i = 0; i < pointer.length; i++) {
|
||||
// pointer[i] = jsonPointerDecodeAndReplace(pointer[i]);
|
||||
// }
|
||||
// res.push(...pointer);
|
||||
// }
|
||||
// .map((key) => {
|
||||
// return key.split('#')[0];
|
||||
// });
|
||||
res.push(jsonPointerDecodeAndReplace(`${filePathName}${localPart}`));
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -72,18 +65,11 @@ function getLocalPath(jsonPointer) {
|
||||
* @param {string} localPath the local path that the pointer will reach
|
||||
* @returns {string} - the concatenated json pointer
|
||||
*/
|
||||
function concatJsonPointer(encodeFunction, filePathName, traceFromParent, localPath) {
|
||||
let local = '',
|
||||
// base = '',
|
||||
traceFromParentAsString = traceFromParent.map((trace) => {
|
||||
return encodeFunction(trace);
|
||||
}).join('/');
|
||||
// TODOE: local support
|
||||
// base = jsonPointerLevelSeparator + encodeFunction(filePathName);
|
||||
if (localPath) {
|
||||
local = `${localPath}`;
|
||||
}
|
||||
return localPointer + jsonPointerLevelSeparator + traceFromParentAsString + local;
|
||||
function concatJsonPointer(encodeFunction, traceFromParent) {
|
||||
const traceFromParentAsString = traceFromParent.map((trace) => {
|
||||
return encodeFunction(trace);
|
||||
}).join('/');
|
||||
return localPointer + '/components' + jsonPointerLevelSeparator + traceFromParentAsString;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,12 +81,12 @@ function concatJsonPointer(encodeFunction, filePathName, traceFromParent, localP
|
||||
* @param {string} traceFromParent the trace from the parent node.
|
||||
* @returns {string} - the concatenated json pointer
|
||||
*/
|
||||
function getJsonPointerRelationToRoot(encodeFunction, filePathName, refValue, traceFromParent) {
|
||||
function getJsonPointerRelationToRoot(encodeFunction, refValue, traceFromKey) {
|
||||
if (refValue.startsWith(localPointer)) {
|
||||
return refValue;
|
||||
}
|
||||
const localPath = getLocalPath(refValue);
|
||||
return concatJsonPointer(encodeFunction, filePathName, traceFromParent, localPath);
|
||||
return concatJsonPointer(encodeFunction, traceFromKey, localPath);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4826,13 +4826,15 @@ module.exports = {
|
||||
return data;
|
||||
},
|
||||
|
||||
getComponentsObject(parsedRootFiles, inputData, origin, version) {
|
||||
getBundledFileData(parsedRootFiles, inputData, origin, version) {
|
||||
const data = parsedRootFiles.map((root) => {
|
||||
let calledAs = [],
|
||||
componentsData = getRelatedFilesAndBundleData(root, inputData, origin, version, calledAs);
|
||||
return componentsData;
|
||||
bundleData = getRelatedFilesAndBundleData(root, inputData, origin, version, calledAs);
|
||||
return bundleData;
|
||||
});
|
||||
return data;
|
||||
let bundledFile = data[0].fileContent;
|
||||
bundledFile.components = data[0].components;
|
||||
return { rootFile: { path: parsedRootFiles[0].fileName }, bundledContent: bundledFile };
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -4855,9 +4857,10 @@ module.exports = {
|
||||
return compareVersion(version, rootWithParsedContent.parsed.oasObject.openapi);
|
||||
}),
|
||||
data = toBundle ?
|
||||
this.getComponentsObject(parsedRootFiles, inputData, origin, version) :
|
||||
this.getBundledFileData(parsedRootFiles, inputData, origin, version) :
|
||||
this.getRelatedFilesData(parsedRootFiles, inputData, origin);
|
||||
return data;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -4874,7 +4877,7 @@ module.exports = {
|
||||
res = {
|
||||
result: true,
|
||||
output: {
|
||||
type: toBundle ? 'bundle' : 'relatedFiles',
|
||||
type: toBundle ? 'bundledContent' : 'relatedFiles',
|
||||
specification: {
|
||||
type: 'OpenAPI',
|
||||
version: version
|
||||
|
||||
Reference in New Issue
Block a user