mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
ignore web refs like http...
ignore web refs and fix the root location
This commit is contained in:
committed by
Erik Mendoza
parent
578943adfd
commit
0f23e60afd
@@ -1,6 +1,26 @@
|
||||
const traverseUtility = require('traverse'),
|
||||
parse = require('./parse.js'),
|
||||
{ DFS } = require('./dfs');
|
||||
let path = require('path'),
|
||||
pathBrowserify = require('path-browserify');
|
||||
|
||||
/**
|
||||
* Checks if the input value is a valid url
|
||||
* @param {string} stringToCheck - specified version of the process
|
||||
* @returns {object} - Detect root files result object
|
||||
*/
|
||||
function stringIsAValidUrl(stringToCheck) {
|
||||
try {
|
||||
let url = new URL(stringToCheck);
|
||||
if (url) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
catch (err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps the output from get root files to detect root files
|
||||
@@ -12,9 +32,19 @@ function isExtRef(obj, key) {
|
||||
return key === '$ref' &&
|
||||
typeof obj[key] === 'string' &&
|
||||
obj[key] !== undefined &&
|
||||
!obj[key].startsWith('#');
|
||||
!obj[key].startsWith('#') &&
|
||||
!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
|
||||
* @param {string} path - path to find
|
||||
@@ -36,14 +66,14 @@ function getReferences (currentNode) {
|
||||
const OASObject = parse.getOasObject(currentContent);
|
||||
traverseUtility(OASObject).forEach((property) => {
|
||||
if (property) {
|
||||
let isExtRef;
|
||||
isExtRef = Object.keys(property)
|
||||
let hasReferenceTypeKey;
|
||||
hasReferenceTypeKey = Object.keys(property)
|
||||
.find(
|
||||
(key) => {
|
||||
return isExtRef(property, key);
|
||||
}
|
||||
);
|
||||
if (isExtRef) {
|
||||
if (hasReferenceTypeKey) {
|
||||
if (!added(property.$ref, referencesInNode)) {
|
||||
referencesInNode.push({ path: property.$ref });
|
||||
}
|
||||
@@ -75,6 +105,25 @@ 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
|
||||
* @param {object} currentNode - current { path, content} object
|
||||
@@ -110,6 +159,9 @@ module.exports = {
|
||||
* @returns {object} - Detect root files result object
|
||||
*/
|
||||
getRelatedFiles: function (specRoot, allData) {
|
||||
// if (origin === BROWSER) {
|
||||
// path = pathBrowserify;
|
||||
// }
|
||||
let algorithm = new DFS(),
|
||||
{ traverseOrder, missing } =
|
||||
algorithm.traverse(specRoot, (currentNode) => {
|
||||
|
||||
@@ -665,8 +665,17 @@ class SchemaPack {
|
||||
const input = this.input;
|
||||
if (!input.rootFiles || input.rootFiles.length === 0) {
|
||||
let rootFiles = await this.detectRootFiles(input);
|
||||
input.rootFiles = rootFiles.output.data;
|
||||
return schemaUtils.processRelatedFiles(input);
|
||||
if (rootFiles.output.data) {
|
||||
let inputContent = [];
|
||||
rootFiles.output.data.forEach((rootFile) => {
|
||||
let founInData = input.data.find((dataFile) => { return dataFile.fileName === rootFile.path; });
|
||||
if (founInData) {
|
||||
inputContent.push({ path: founInData.fileName, content: founInData.content });
|
||||
}
|
||||
});
|
||||
input.rootFiles = inputContent;
|
||||
return schemaUtils.processRelatedFiles(input);
|
||||
}
|
||||
}
|
||||
return schemaUtils.processRelatedFiles(input);
|
||||
}
|
||||
|
||||
@@ -62,6 +62,8 @@ describe('detectRoot method', function() {
|
||||
expect(res).to.not.be.empty;
|
||||
expect(res.result).to.be.true;
|
||||
expect(res.output.data[0].rootFile.path).to.equal('/petstore.yaml');
|
||||
expect(res.output.data[0].relatedFiles.length).to.equal(0);
|
||||
expect(res.output.data[0].missingRelatedFiles.length).to.equal(0);
|
||||
});
|
||||
|
||||
it('should return adjacent and missing nodes', async function () {
|
||||
|
||||
Reference in New Issue
Block a user