mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
add path solving while searching
add path solving while searching
This commit is contained in:
committed by
Erik Mendoza
parent
0f23e60afd
commit
c65d80ae05
@@ -1,5 +1,6 @@
|
|||||||
const traverseUtility = require('traverse'),
|
const traverseUtility = require('traverse'),
|
||||||
parse = require('./parse.js'),
|
parse = require('./parse.js'),
|
||||||
|
BROWSER = 'browser',
|
||||||
{ DFS } = require('./dfs');
|
{ DFS } = require('./dfs');
|
||||||
let path = require('path'),
|
let path = require('path'),
|
||||||
pathBrowserify = require('path-browserify');
|
pathBrowserify = require('path-browserify');
|
||||||
@@ -36,15 +37,6 @@ function isExtRef(obj, key) {
|
|||||||
!stringIsAValidUrl(obj[key]);
|
!stringIsAValidUrl(obj[key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**
|
|
||||||
// * returns the dirName of the file
|
|
||||||
// * @param {string} filePath - path to find
|
|
||||||
// * @returns {string} - the dirName of the path
|
|
||||||
// */
|
|
||||||
// function getDirName(filePath) {
|
|
||||||
// return path.getDirName(filePath);
|
|
||||||
// }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* verifies if the path has been added to the result
|
* verifies if the path has been added to the result
|
||||||
* @param {string} path - path to find
|
* @param {string} path - path to find
|
||||||
@@ -93,6 +85,18 @@ function comparePaths(path1, path2) {
|
|||||||
return path1 === path2; // todo change this comparision
|
return path1 === path2; // todo change this comparision
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the path relative to parent
|
||||||
|
* @param {string} parentFileName - parent file name of the current node
|
||||||
|
* @param {string} referencePath - value of the $ref property
|
||||||
|
* @returns {object} - Detect root files result object
|
||||||
|
*/
|
||||||
|
function calculatePath(parentFileName, referencePath) {
|
||||||
|
let currentDirName = path.dirname(parentFileName),
|
||||||
|
refDirName = path.join(currentDirName, referencePath);
|
||||||
|
return refDirName;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Locates a referenced node from the data input by path
|
* Locates a referenced node from the data input by path
|
||||||
* @param {string} referencePath - value from the $ref property
|
* @param {string} referencePath - value from the $ref property
|
||||||
@@ -105,25 +109,6 @@ function findNodeFromPath(referencePath, allData) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Calculates the path relative to root file
|
|
||||||
// * @param {object} currentNodeRelativeToRootPath -
|
|
||||||
// * the relative to root path of the current node.
|
|
||||||
// * @param {Array} referencePath - the path in the $ref
|
|
||||||
// * @param {object} specRoot - root file information
|
|
||||||
// * @returns {object} - Detect root files result object
|
|
||||||
// */
|
|
||||||
// function calculatePathToRoot(currentNodeRelativeToRootPath, referencePath) {
|
|
||||||
// let currentDirName = '',
|
|
||||||
// refDirName = '';
|
|
||||||
// if (!currentNodeRelativeToRootPath) {
|
|
||||||
// currentDirName = getDirName(currentNodeRelativeToRootPath);
|
|
||||||
// }
|
|
||||||
// refDirName = path.join(currentDirName, referencePath);
|
|
||||||
// return refDirName;
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Maps the output from get root files to detect root files
|
* Maps the output from get root files to detect root files
|
||||||
* @param {object} currentNode - current { path, content} object
|
* @param {object} currentNode - current { path, content} object
|
||||||
@@ -138,13 +123,12 @@ function getAdjacentAndMissing (currentNode, allData, specRoot) {
|
|||||||
|
|
||||||
currentNodeReferences.forEach((reference) => {
|
currentNodeReferences.forEach((reference) => {
|
||||||
let referencePath = reference.path,
|
let referencePath = reference.path,
|
||||||
adjacentNode = findNodeFromPath(referencePath, allData);
|
adjacentNode = findNodeFromPath(calculatePath(currentNode.fileName, referencePath), allData);
|
||||||
if (adjacentNode) {
|
if (adjacentNode) {
|
||||||
adjacentNode.relativeToRootPath = referencePath;
|
|
||||||
graphAdj.push(adjacentNode);
|
graphAdj.push(adjacentNode);
|
||||||
}
|
}
|
||||||
else if (!comparePaths(referencePath, specRoot.path)) {
|
else if (!comparePaths(referencePath, specRoot.fileName)) {
|
||||||
missingNodes.push({ relativeToRootPath: referencePath });
|
missingNodes.push({ path: referencePath });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return { graphAdj, missingNodes };
|
return { graphAdj, missingNodes };
|
||||||
@@ -156,12 +140,13 @@ module.exports = {
|
|||||||
* Maps the output from get root files to detect root files
|
* Maps the output from get root files to detect root files
|
||||||
* @param {object} specRoot - root file information
|
* @param {object} specRoot - root file information
|
||||||
* @param {Array} allData - array of { path, content} objects
|
* @param {Array} allData - array of { path, content} objects
|
||||||
|
* @param {Array} origin - process origin (BROWSER or node)
|
||||||
* @returns {object} - Detect root files result object
|
* @returns {object} - Detect root files result object
|
||||||
*/
|
*/
|
||||||
getRelatedFiles: function (specRoot, allData) {
|
getRelatedFiles: function (specRoot, allData, origin) {
|
||||||
// if (origin === BROWSER) {
|
if (origin === BROWSER) {
|
||||||
// path = pathBrowserify;
|
path = pathBrowserify;
|
||||||
// }
|
}
|
||||||
let algorithm = new DFS(),
|
let algorithm = new DFS(),
|
||||||
{ traverseOrder, missing } =
|
{ traverseOrder, missing } =
|
||||||
algorithm.traverse(specRoot, (currentNode) => {
|
algorithm.traverse(specRoot, (currentNode) => {
|
||||||
@@ -169,12 +154,12 @@ module.exports = {
|
|||||||
}),
|
}),
|
||||||
outputRelatedFiles = traverseOrder.slice(1).map((relatedFile) => {
|
outputRelatedFiles = traverseOrder.slice(1).map((relatedFile) => {
|
||||||
return {
|
return {
|
||||||
relativeToRootPath: relatedFile.relativeToRootPath,
|
|
||||||
path: relatedFile.fileName
|
path: relatedFile.fileName
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
return { relatedFiles: outputRelatedFiles, missingRelatedFiles: missing };
|
return { relatedFiles: outputRelatedFiles, missingRelatedFiles: missing };
|
||||||
},
|
},
|
||||||
getReferences,
|
getReferences,
|
||||||
getAdjacentAndMissing
|
getAdjacentAndMissing,
|
||||||
|
calculatePath
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4845,11 +4845,11 @@ module.exports = {
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
mapRootFiles(rootFiles, inputData) {
|
mapRootFiles(rootFiles, inputData, origin) {
|
||||||
let data = rootFiles.map((root) => {
|
let data = rootFiles.map((root) => {
|
||||||
let { relatedFiles, missingRelatedFiles } = getRelatedFiles(root, inputData);
|
let { relatedFiles, missingRelatedFiles } = getRelatedFiles(root, inputData, origin);
|
||||||
return {
|
return {
|
||||||
rootFile: { path: root.path },
|
rootFile: { path: root.fileName },
|
||||||
relatedFiles,
|
relatedFiles,
|
||||||
missingRelatedFiles };
|
missingRelatedFiles };
|
||||||
});
|
});
|
||||||
@@ -4871,7 +4871,8 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
if (inputRelatedFiles.rootFiles && inputRelatedFiles.rootFiles.length > 0) {
|
if (inputRelatedFiles.rootFiles && inputRelatedFiles.rootFiles.length > 0) {
|
||||||
res.output.data = this.mapRootFiles(inputRelatedFiles.rootFiles, inputRelatedFiles.data);
|
res.output.data = this.mapRootFiles(inputRelatedFiles.rootFiles, inputRelatedFiles.data,
|
||||||
|
inputRelatedFiles.origin);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -670,13 +670,17 @@ class SchemaPack {
|
|||||||
rootFiles.output.data.forEach((rootFile) => {
|
rootFiles.output.data.forEach((rootFile) => {
|
||||||
let founInData = input.data.find((dataFile) => { return dataFile.fileName === rootFile.path; });
|
let founInData = input.data.find((dataFile) => { return dataFile.fileName === rootFile.path; });
|
||||||
if (founInData) {
|
if (founInData) {
|
||||||
inputContent.push({ path: founInData.fileName, content: founInData.content });
|
inputContent.push({ fileName: founInData.fileName, content: founInData.content });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
input.rootFiles = inputContent;
|
input.rootFiles = inputContent;
|
||||||
return schemaUtils.processRelatedFiles(input);
|
return schemaUtils.processRelatedFiles(input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
let adaptedRootFiles = input.rootFiles.map((rootFile) => {
|
||||||
|
return { fileName: rootFile.path, content: rootFile.content };
|
||||||
|
});
|
||||||
|
input.rootFiles = adaptedRootFiles;
|
||||||
return schemaUtils.processRelatedFiles(input);
|
return schemaUtils.processRelatedFiles(input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ describe('detectRoot method', function() {
|
|||||||
],
|
],
|
||||||
data: [
|
data: [
|
||||||
{
|
{
|
||||||
path: 'Pet.yaml',
|
path: '/Pet.yaml',
|
||||||
content: contentFilePet
|
content: contentFilePet
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -89,9 +89,8 @@ describe('detectRoot method', function() {
|
|||||||
expect(res).to.not.be.empty;
|
expect(res).to.not.be.empty;
|
||||||
expect(res.result).to.be.true;
|
expect(res.result).to.be.true;
|
||||||
expect(res.output.data[0].rootFile.path).to.equal('/missedRef.yaml');
|
expect(res.output.data[0].rootFile.path).to.equal('/missedRef.yaml');
|
||||||
expect(res.output.data[0].relatedFiles[0].path).to.equal('Pet.yaml');
|
expect(res.output.data[0].relatedFiles[0].path).to.equal('/Pet.yaml');
|
||||||
expect(res.output.data[0].relatedFiles[0].relativeToRootPath).to.equal('Pet.yaml');
|
expect(res.output.data[0].missingRelatedFiles[0].path).to.equal('../common/Error.yaml');
|
||||||
expect(res.output.data[0].missingRelatedFiles[0].relativeToRootPath).to.equal('../common/Error.yaml');
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -118,9 +117,8 @@ describe('detectRoot method', function() {
|
|||||||
expect(res).to.not.be.empty;
|
expect(res).to.not.be.empty;
|
||||||
expect(res.result).to.be.true;
|
expect(res.result).to.be.true;
|
||||||
expect(res.output.data[0].relatedFiles[0].path).to.equal('NewPet.yaml');
|
expect(res.output.data[0].relatedFiles[0].path).to.equal('NewPet.yaml');
|
||||||
expect(res.output.data[0].relatedFiles[0].relativeToRootPath).to.equal('NewPet.yaml');
|
|
||||||
expect(res.output.data[0].missingRelatedFiles.length).to.equal(1);
|
expect(res.output.data[0].missingRelatedFiles.length).to.equal(1);
|
||||||
expect(res.output.data[0].missingRelatedFiles[0].relativeToRootPath).to.equal('Pet.yaml');
|
expect(res.output.data[0].missingRelatedFiles[0].path).to.equal('Pet.yaml');
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -176,9 +174,7 @@ describe('detectRoot method', function() {
|
|||||||
expect(res.result).to.be.true;
|
expect(res.result).to.be.true;
|
||||||
expect(res.output.data[0].relatedFiles.length).to.equal(2);
|
expect(res.output.data[0].relatedFiles.length).to.equal(2);
|
||||||
expect(res.output.data[0].relatedFiles[0].path).to.equal('oldPet.yaml');
|
expect(res.output.data[0].relatedFiles[0].path).to.equal('oldPet.yaml');
|
||||||
expect(res.output.data[0].relatedFiles[0].relativeToRootPath).to.equal('oldPet.yaml');
|
|
||||||
expect(res.output.data[0].relatedFiles[1].path).to.equal('Pet.yaml');
|
expect(res.output.data[0].relatedFiles[1].path).to.equal('Pet.yaml');
|
||||||
expect(res.output.data[0].relatedFiles[1].relativeToRootPath).to.equal('Pet.yaml');
|
|
||||||
expect(res.output.data[0].missingRelatedFiles.length).to.equal(0);
|
expect(res.output.data[0].missingRelatedFiles.length).to.equal(0);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const { getRelatedFiles, getReferences, getAdjacentAndMissing } = require('./../../lib/relatedFiles'),
|
const { getRelatedFiles, getReferences, getAdjacentAndMissing,
|
||||||
|
calculatePath } = require('./../../lib/relatedFiles'),
|
||||||
expect = require('chai').expect,
|
expect = require('chai').expect,
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
@@ -21,12 +22,12 @@ describe('getAdjacentAndMissing function', function () {
|
|||||||
content: contentFileNewPet
|
content: contentFileNewPet
|
||||||
},
|
},
|
||||||
inputData = [{
|
inputData = [{
|
||||||
fileName: 'Pet.yaml',
|
fileName: '/Pet.yaml',
|
||||||
content: contentFilePet
|
content: contentFilePet
|
||||||
}],
|
}],
|
||||||
{ graphAdj, missingNodes } = getAdjacentAndMissing(inputNode, inputData, inputNode);
|
{ graphAdj, missingNodes } = getAdjacentAndMissing(inputNode, inputData, inputNode);
|
||||||
expect(graphAdj.length).to.equal(1);
|
expect(graphAdj.length).to.equal(1);
|
||||||
expect(graphAdj[0].fileName).to.equal('Pet.yaml');
|
expect(graphAdj[0].fileName).to.equal('/Pet.yaml');
|
||||||
expect(missingNodes.length).to.equal(0);
|
expect(missingNodes.length).to.equal(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -34,18 +35,18 @@ describe('getAdjacentAndMissing function', function () {
|
|||||||
const contentFileMissedRef = fs.readFileSync(missedRef, 'utf8'),
|
const contentFileMissedRef = fs.readFileSync(missedRef, 'utf8'),
|
||||||
contentFilePet = fs.readFileSync(petstoreSeparatedPet, 'utf8'),
|
contentFilePet = fs.readFileSync(petstoreSeparatedPet, 'utf8'),
|
||||||
inputNode = {
|
inputNode = {
|
||||||
path: '/missedRef.yaml',
|
fileName: '/missedRef.yaml',
|
||||||
content: contentFileMissedRef
|
content: contentFileMissedRef
|
||||||
},
|
},
|
||||||
inputData = [{
|
inputData = [{
|
||||||
fileName: 'Pet.yaml',
|
fileName: '/Pet.yaml',
|
||||||
content: contentFilePet
|
content: contentFilePet
|
||||||
}],
|
}],
|
||||||
{ graphAdj, missingNodes } = getAdjacentAndMissing(inputNode, inputData, inputNode);
|
{ graphAdj, missingNodes } = getAdjacentAndMissing(inputNode, inputData, inputNode);
|
||||||
expect(graphAdj.length).to.equal(1);
|
expect(graphAdj.length).to.equal(1);
|
||||||
expect(graphAdj[0].fileName).to.equal('Pet.yaml');
|
expect(graphAdj[0].fileName).to.equal('/Pet.yaml');
|
||||||
expect(missingNodes.length).to.equal(1);
|
expect(missingNodes.length).to.equal(1);
|
||||||
expect(missingNodes[0].relativeToRootPath).to.equal('../common/Error.yaml');
|
expect(missingNodes[0].path).to.equal('../common/Error.yaml');
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -92,14 +93,14 @@ describe('getRelatedFiles function ', function () {
|
|||||||
content: contentFileMissedRef
|
content: contentFileMissedRef
|
||||||
},
|
},
|
||||||
inputData = [{
|
inputData = [{
|
||||||
fileName: 'Pet.yaml',
|
fileName: '/Pet.yaml',
|
||||||
content: contentFilePet
|
content: contentFilePet
|
||||||
}],
|
}],
|
||||||
{ relatedFiles, missingRelatedFiles } = getRelatedFiles(rootNode, inputData);
|
{ relatedFiles, missingRelatedFiles } = getRelatedFiles(rootNode, inputData);
|
||||||
expect(relatedFiles.length).to.equal(1);
|
expect(relatedFiles.length).to.equal(1);
|
||||||
expect(relatedFiles[0].path).to.equal('Pet.yaml');
|
expect(relatedFiles[0].path).to.equal('/Pet.yaml');
|
||||||
expect(missingRelatedFiles.length).to.equal(1);
|
expect(missingRelatedFiles.length).to.equal(1);
|
||||||
expect(missingRelatedFiles[0].relativeToRootPath).to.equal('../common/Error.yaml');
|
expect(missingRelatedFiles[0].path).to.equal('../common/Error.yaml');
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -116,3 +117,14 @@ describe('getRelatedFiles function ', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('calculatePath function', function () {
|
||||||
|
it('should return the path from the parent', function () {
|
||||||
|
const result = calculatePath('sf/newpet.yaml', '../error.yaml');
|
||||||
|
expect(result).to.equal('error.yaml');
|
||||||
|
});
|
||||||
|
it('should return localhost:3000 for entry "http://localhost:3000/projects"', function () {
|
||||||
|
const result = calculatePath('sf/spec/newpet.yaml', '../common/error.yaml');
|
||||||
|
expect(result).to.equal('sf/common/error.yaml');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user