From 8245b36f2dc77fcc1e6df702d9186084fd4c0e12 Mon Sep 17 00:00:00 2001 From: Luis Tejeda <46000487+LuisTejedaS@users.noreply.github.com> Date: Wed, 20 Apr 2022 11:58:47 -0500 Subject: [PATCH] add support for empty content no content and multi version example --- lib/common/versionUtils.js | 28 ++++++++++++++++++++++++++-- lib/schemaUtils.js | 20 +++++++++++++++++++- lib/schemapack.js | 12 ++++++++++-- test/unit/detectRoot.test.js | 20 ++++++++++++++++++++ 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/lib/common/versionUtils.js b/lib/common/versionUtils.js index d9af5a1..3b53f76 100644 --- a/lib/common/versionUtils.js +++ b/lib/common/versionUtils.js @@ -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,11 +142,17 @@ function getFileWithVersion(data, specificationVersion) { file = getFileByContentSpecificationVersion(data, specificationVersion); } else { - file = getFileByContent(data, specificationVersion); + file = getFileByContent(data); } } else if (data[0].hasOwnProperty('fileName')) { - file = getFileByFileName(data); + if (specificationVersion) { + file = getFileByFileNameSpecificVersion(data, specificationVersion); + } + else { + file = getFileByFileName(data); + } + } return file; } diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index 9882317..2748604 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -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 diff --git a/lib/schemapack.js b/lib/schemapack.js index ca58bf1..d4e146b 100644 --- a/lib/schemapack.js +++ b/lib/schemapack.js @@ -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); diff --git a/test/unit/detectRoot.test.js b/test/unit/detectRoot.test.js index 69f32e2..14189de 100644 --- a/test/unit/detectRoot.test.js +++ b/test/unit/detectRoot.test.js @@ -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); + + }); + });