add support for empty content

no content and multi version example
This commit is contained in:
Luis Tejeda
2022-04-20 11:58:47 -05:00
committed by Erik Mendoza
parent f200446d92
commit 8245b36f2d
4 changed files with 75 additions and 5 deletions

View File

@@ -109,6 +109,24 @@ function getFileByFileName(data) {
return file;
}
/**
* When the array of files is provided as a list of file's paths it returns the
* content of the file that contains the version data
* @param {array} data The array of files in the folder provided by the user
* @param {string} specificationVersion the string of the desired version
* @returns {string} the content of the file that contains the version data
*/
function getFileByFileNameSpecificVersion(data, specificationVersion) {
const versionRegExp = getVersionRegexBySpecificationVersion(specificationVersion);
let file = data.map((element) => {
return fs.readFileSync(element.fileName, 'utf8');
}).find((content) => {
return content.match(versionRegExp);
});
return file;
}
/** 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
@@ -124,12 +142,18 @@ function getFileWithVersion(data, specificationVersion) {
file = getFileByContentSpecificationVersion(data, specificationVersion);
}
else {
file = getFileByContent(data, specificationVersion);
file = getFileByContent(data);
}
}
else if (data[0].hasOwnProperty('fileName')) {
if (specificationVersion) {
file = getFileByFileNameSpecificVersion(data, specificationVersion);
}
else {
file = getFileByFileName(data);
}
}
return file;
}

View File

@@ -4795,11 +4795,29 @@ module.exports = {
*/
mapDetectRootFilesInputToGetRootFilesInput(input) {
let adaptedData = input.data.map((file) => {
return { fileName: file.path };
return { fileName: file.fileName };
});
return { data: adaptedData };
},
/**
* Maps the input from detect root files to get root files
* @param {object} input - input schema
* @returns {Array} - Array of all MISSING_ENDPOINT objects
*/
mapDetectRootFilesInputToFolderInput(input) {
let adaptedData = input.data.map((file) => {
if (file.content) {
return { fileName: file.path, content: file.content };
}
else {
return { fileName: file.path };
}
});
input.data = adaptedData;
return input;
},
/**
* Maps the output from get root files to detect root files
* @param {object} output - output schema

View File

@@ -33,6 +33,9 @@ let path = require('path'),
class SchemaPack {
constructor (input, options = {}) {
if (this.checkInputIsDetectRoot(input)) {
input = schemaUtils.mapDetectRootFilesInputToFolderInput(input);
}
this.input = input;
this.validated = false;
this.openapi = null;
@@ -63,6 +66,10 @@ class SchemaPack {
this.validate();
}
checkInputIsDetectRoot(input) {
return input.type === 'folder' && input.data && input.data[0] && input.data[0].path;
}
// need to store the schema here
validate() {
let input = this.input,
@@ -624,7 +631,8 @@ class SchemaPack {
if (input.data[0].path === '') {
throw new Error('undefined input');
}
if (!this.hasDefinedVersion) {
if (!this.hasDefinedVersion && ('content' in input.data[0])) {
return schemaUtils.mapGetRootFilesOutputToDetectRootFilesOutput([], input.specificationVersion);
}
@@ -639,7 +647,7 @@ class SchemaPack {
}
if ('content' in input.data[0]) {
input.data.forEach((file) => {
files[path.resolve(file.path)] = file.content ? file.content : '';
files[path.resolve(file.fileName)] = file.content ? file.content : '';
});
}
adaptedInput = schemaUtils.mapDetectRootFilesInputToGetRootFilesInput(input);

View File

@@ -245,4 +245,24 @@ describe('detectRoot method', function() {
expect(res.output.data[0].path).to.equal('/swagger.json');
});
it('should read content when is not present 3.0 and no specific version', async function () {
let input = {
type: 'folder',
specificationVersion: '3.1.0',
data: [
{
path: validPetstore
},
{
path: validHopService31x
}
]
};
const res = await Converter.detectRootFiles(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.data[0].path).to.equal(validHopService31x);
});
});