mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
add multiple versions support
add multiple versions support
This commit is contained in:
committed by
Erik Mendoza
parent
36e83721dd
commit
30cc74b720
@@ -32,9 +32,67 @@ function getFileByContent(data) {
|
||||
return element.content.match(version2RegExp) || element.content.match(version3RegExp);
|
||||
});
|
||||
if (!file) {
|
||||
return { hasDefinedVersion: false };
|
||||
throw new Error('Not files with version');
|
||||
}
|
||||
return { hasDefinedVersion: true, content: file.content };
|
||||
return file.content;
|
||||
}
|
||||
|
||||
/**
|
||||
* compares a version with an input
|
||||
* @param {string} input The input to compare
|
||||
* @param {string} version The version that will be used
|
||||
* @returns {boolean} wheter the input corresponds to the version
|
||||
*/
|
||||
function compareVersion(input, version) {
|
||||
let numberInput,
|
||||
numberVersion;
|
||||
numberInput = parseFloat(input);
|
||||
numberVersion = parseFloat(version);
|
||||
if (!isNaN(numberInput) && !isNaN(numberVersion)) {
|
||||
return numberInput === numberVersion;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determins the version regex according to the specificationVersion
|
||||
* @param {string} specificationVersion the string of the desired version
|
||||
* @returns {object} the resultant regular expresion using the provided data
|
||||
*/
|
||||
function getVersionRegexBySpecificationVersion(specificationVersion) {
|
||||
let versionRegExp;
|
||||
if (compareVersion(specificationVersion, '2.0')) {
|
||||
versionRegExp = getVersionRegexp(VERSION_20);
|
||||
}
|
||||
else if (compareVersion(specificationVersion, '3.1')) {
|
||||
versionRegExp = getVersionRegexp(VERSION_31);
|
||||
}
|
||||
else {
|
||||
versionRegExp = getVersionRegexp(VERSION_30);
|
||||
}
|
||||
return versionRegExp;
|
||||
}
|
||||
|
||||
/**
|
||||
* When the array of files is provided as a list of parsed objects
|
||||
* it returns the content from file that contains the version data
|
||||
* specified in the specificationVersion parameter
|
||||
* @param {array} data An array of the provided file's content parsed
|
||||
* @param {string} specificationVersion the string of the desired version
|
||||
* @returns {object} object with hasDefinedVersion property and
|
||||
* The content of the file that contains the version data
|
||||
*/
|
||||
function getFileByContentSpecificationVersion(data, specificationVersion) {
|
||||
let versionRegExp,
|
||||
file;
|
||||
versionRegExp = getVersionRegexBySpecificationVersion(specificationVersion);
|
||||
file = data.find((element) => {
|
||||
return element.content.match(versionRegExp);
|
||||
});
|
||||
if (!file) {
|
||||
throw new Error('Not files with version');
|
||||
}
|
||||
return file.content;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,14 +115,20 @@ function getFileByFileName(data) {
|
||||
/** When the user provides a folder, this function returns the file
|
||||
* that contains the version data
|
||||
* @param {array} data An array of file's paths
|
||||
* @param {string} specificationVersion the string of the desired version
|
||||
* @returns {object} object with hasDefinedVersion property and
|
||||
* The content of the file that contains the version data
|
||||
*/
|
||||
function getFileWithVersion(data) {
|
||||
function getFileWithVersion(data, specificationVersion) {
|
||||
let file;
|
||||
|
||||
if (data[0].hasOwnProperty('content')) {
|
||||
file = getFileByContent(data);
|
||||
if (specificationVersion) {
|
||||
file = getFileByContentSpecificationVersion(data, specificationVersion);
|
||||
}
|
||||
else {
|
||||
file = getFileByContent(data, specificationVersion);
|
||||
}
|
||||
}
|
||||
else if (data[0].hasOwnProperty('fileName')) {
|
||||
file = getFileByFileName(data);
|
||||
@@ -77,13 +141,13 @@ function getFileWithVersion(data) {
|
||||
* @param {string} spec Data from input file
|
||||
* @returns {string} version of specification
|
||||
*/
|
||||
function getSpecVersion({ type, data }) {
|
||||
function getSpecVersion({ type, data, specificationVersion }) {
|
||||
if (!data) {
|
||||
return DEFAULT_SPEC_VERSION;
|
||||
}
|
||||
|
||||
if (['folder'].includes(type)) {
|
||||
data = getFileWithVersion(data);
|
||||
data = getFileWithVersion(data, specificationVersion);
|
||||
}
|
||||
else if (['file'].includes(type)) {
|
||||
try {
|
||||
@@ -93,12 +157,6 @@ function getSpecVersion({ type, data }) {
|
||||
return DEFAULT_SPEC_VERSION; // If path is invalid it will follow the OAS 3.0 way
|
||||
}
|
||||
}
|
||||
if (!data.hasDefinedVersion) {
|
||||
return;
|
||||
}
|
||||
else {
|
||||
data = data.content;
|
||||
}
|
||||
|
||||
if (type === 'json') {
|
||||
data = JSON.stringify(data);
|
||||
@@ -128,8 +186,8 @@ function getSpecVersion({ type, data }) {
|
||||
* @param {string} specVersion - the OAS specification version
|
||||
* @returns {NodeRequire} the schema utils according to version
|
||||
*/
|
||||
function getConcreteSchemaUtils({ type, data }) {
|
||||
const specVersion = getSpecVersion({ type, data });
|
||||
function getConcreteSchemaUtils({ type, data, specificationVersion }) {
|
||||
const specVersion = getSpecVersion({ type, data, specificationVersion });
|
||||
if (!specVersion) {
|
||||
return;
|
||||
}
|
||||
@@ -176,5 +234,7 @@ module.exports = {
|
||||
getSpecVersion,
|
||||
getConcreteSchemaUtils,
|
||||
filterOptionsByVersion,
|
||||
isSwagger
|
||||
isSwagger,
|
||||
compareVersion,
|
||||
getVersionRegexBySpecificationVersion
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user