mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
Merge branch 'split/develop/multiFileSupport' into feature/swagger20BundleSupport
This commit is contained in:
@@ -92,7 +92,8 @@ const { formatDataPath, checkIsCorrectType, isKnownType } = require('./common/sc
|
||||
{ getRelatedFiles } = require('./relatedFiles'),
|
||||
{ compareVersion } = require('./common/versionUtils.js'),
|
||||
parse = require('./parse'),
|
||||
{ getBundleContentAndComponents, parseFileOrThrow } = require('./bundle.js');
|
||||
{ getBundleContentAndComponents, parseFileOrThrow } = require('./bundle.js'),
|
||||
MULTI_FILE_API_TYPE_ALLOWED_VALUE = 'multiFile';
|
||||
/* eslint-enable */
|
||||
|
||||
// See https://github.com/json-schema-faker/json-schema-faker/tree/master/docs#available-options
|
||||
@@ -4797,7 +4798,7 @@ module.exports = {
|
||||
*/
|
||||
mapGetRootFilesOutputToDetectRootFilesOutput(output, version) {
|
||||
if (!version) {
|
||||
version = '3.0.0';
|
||||
version = '3.0';
|
||||
}
|
||||
let adaptedData = output.map((file) => {
|
||||
return { path: file };
|
||||
@@ -4839,7 +4840,39 @@ module.exports = {
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* Maps the output for each bundled root file
|
||||
* @param {object} format - defined output format from options
|
||||
* @param {string} parsedRootFiles - The parsed root files
|
||||
* @param {string} version - specified version of the process
|
||||
* @returns {object} - { rootFile: { path }, bundledContent }
|
||||
*/
|
||||
mapBundleOutput(format, parsedRootFiles, version) {
|
||||
return (contentAndComponents) => {
|
||||
let bundledFile = contentAndComponents.fileContent;
|
||||
if (version === SWAGGER_VERSION) {
|
||||
Object.entries(contentAndComponents.components).forEach(([key, value]) => {
|
||||
bundledFile[key] = value;
|
||||
});
|
||||
}
|
||||
else {
|
||||
bundledFile.components = contentAndComponents.components;
|
||||
}
|
||||
if (!format) {
|
||||
let rootFormat = parsedRootFiles.find((inputRoot) => {
|
||||
return inputRoot.fileName === contentAndComponents.fileName;
|
||||
}).parsed.inputFormat;
|
||||
if (rootFormat.toLowerCase() === parse.YAML_FORMAT) {
|
||||
bundledFile = parse.toYAML(bundledFile);
|
||||
}
|
||||
}
|
||||
else if (format.toLowerCase() === parse.YAML_FORMAT) {
|
||||
bundledFile = parse.toYAML(bundledFile);
|
||||
}
|
||||
return { rootFile: { path: contentAndComponents.fileName }, bundledContent: bundledFile };
|
||||
};
|
||||
},
|
||||
|
||||
/*
|
||||
* @description Takes in parsed root files and bundle it
|
||||
* @param {object} parsedRootFiles - found parsed root files
|
||||
* @param {array} inputData - file data information [{path, content}]
|
||||
@@ -4854,22 +4887,10 @@ module.exports = {
|
||||
let bundleData = getBundleContentAndComponents(root, inputData, origin, version);
|
||||
return bundleData;
|
||||
});
|
||||
let bundledFile = data[0].fileContent;
|
||||
if (version === SWAGGER_VERSION) {
|
||||
Object.entries(data[0].components).forEach(([key, value]) => {
|
||||
bundledFile[key] = value;
|
||||
});
|
||||
}
|
||||
else {
|
||||
bundledFile.components = data[0].components;
|
||||
}
|
||||
if (format === parse.YAML_FORMAT) {
|
||||
bundledFile = parse.toYAML(bundledFile);
|
||||
}
|
||||
else if (format === parse.JSON_FORMAT) {
|
||||
bundledFile = parse.toJSON(bundledFile, 2);
|
||||
}
|
||||
return { rootFile: { path: parsedRootFiles[0].fileName }, bundledContent: bundledFile };
|
||||
|
||||
let bundleData = data.map(this.mapBundleOutput(format, parsedRootFiles, version));
|
||||
|
||||
return bundleData;
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -4894,11 +4915,10 @@ module.exports = {
|
||||
let fileVersion = version === SWAGGER_VERSION ?
|
||||
rootWithParsedContent.parsed.oasObject.swagger :
|
||||
rootWithParsedContent.parsed.oasObject.openapi;
|
||||
bundleFormat = bundleFormat ? bundleFormat : rootWithParsedContent.parsed.inputFormat;
|
||||
return compareVersion(version, fileVersion);
|
||||
}),
|
||||
data = toBundle ?
|
||||
this.getBundledFileData(parsedRootFiles, inputData, origin, bundleFormat.toLowerCase(), version) :
|
||||
this.getBundledFileData(parsedRootFiles, inputData, origin, bundleFormat, version) :
|
||||
this.getRelatedFilesData(parsedRootFiles, inputData, origin);
|
||||
return data;
|
||||
|
||||
@@ -4914,7 +4934,7 @@ module.exports = {
|
||||
* @returns {object} root files information and data input
|
||||
*/
|
||||
processRelatedFiles(inputRelatedFiles, toBundle = false) {
|
||||
let version = inputRelatedFiles.specificationVersion ? inputRelatedFiles.specificationVersion : '3.0.0',
|
||||
let version = inputRelatedFiles.specificationVersion ? inputRelatedFiles.specificationVersion : '3.0',
|
||||
res = {
|
||||
result: true,
|
||||
output: {
|
||||
@@ -4932,10 +4952,9 @@ module.exports = {
|
||||
res.output.data = this.mapProcessRelatedFiles(inputRelatedFiles.rootFiles, inputRelatedFiles.data,
|
||||
inputRelatedFiles.origin, version, inputRelatedFiles.bundleFormat, toBundle);
|
||||
if (res.output.data === undefined || res.output.data.result === false ||
|
||||
res.output.data.length === 0) {
|
||||
res.output.data.length === 0) {
|
||||
res.result = false;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof ParseError) {
|
||||
@@ -4945,36 +4964,36 @@ module.exports = {
|
||||
newError.stack = error.stack;
|
||||
throw (newError);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
else {
|
||||
return res;
|
||||
throw new Error('Input should have at least one root file');
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* @description Validates the input for multi file APIs
|
||||
* @param {object} processInput - Process input data
|
||||
* @param {boolean} bundleProcess - Wheter the process input corresponds to bundel process
|
||||
* @param {string} processInput - Process input data
|
||||
*
|
||||
* @returns {undefined} - nothing
|
||||
*/
|
||||
validateInputMultiFileAPI(processInput, bundleProcess = false) {
|
||||
validateInputMultiFileAPI(processInput) {
|
||||
if (_.isEmpty(processInput)) {
|
||||
throw new Error('Input object must have "type" and "data" information');
|
||||
}
|
||||
if (!processInput.type) {
|
||||
throw new Error('"Type" parameter should be provided');
|
||||
}
|
||||
if (processInput.type !== MULTI_FILE_API_TYPE_ALLOWED_VALUE) {
|
||||
throw new Error('"Type" parameter value allowed is ' + MULTI_FILE_API_TYPE_ALLOWED_VALUE);
|
||||
}
|
||||
if (!processInput.data || processInput.data.length === 0) {
|
||||
throw new Error('"Data" parameter should be provided');
|
||||
}
|
||||
if (processInput.data[0].path === '') {
|
||||
throw new Error('"Path" of the data element should be provided');
|
||||
}
|
||||
if (bundleProcess && (!processInput.rootFiles || processInput.rootFiles.length === 0)) {
|
||||
throw new Error('"RootFiles" parameter should be provided');
|
||||
}
|
||||
}
|
||||
},
|
||||
MULTI_FILE_API_TYPE_ALLOWED_VALUE
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user