Take format from root

take the format of each root when the option for the process is not present
This commit is contained in:
Luis Tejeda
2022-06-09 11:46:23 -05:00
parent fcf43811d0
commit 775682b7c3
4 changed files with 110 additions and 71 deletions

View File

@@ -402,7 +402,8 @@ module.exports = {
generateComponentsObject(rootContextData, rootContextData.nodeContents[specRoot.fileName], isExtRef, components); generateComponentsObject(rootContextData, rootContextData.nodeContents[specRoot.fileName], isExtRef, components);
return { return {
fileContent: rootContextData.nodeContents[specRoot.fileName], fileContent: rootContextData.nodeContents[specRoot.fileName],
components components,
fileName: specRoot.fileName
}; };
}, },
getReferences getReferences

View File

@@ -4839,8 +4839,32 @@ module.exports = {
return data; return data;
}, },
/**
* Maps the output for each bundled root file
* @param {object} format - defined output format from options
* @param {string} parsedRootFiles - specified version of the process
* @returns {object} - { rootFile: { path }, bundledContent }
*/
mapBundleOutput(format, parsedRootFiles) {
return (contentAndComponents) => {
let bundledFile = contentAndComponents.fileContent;
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: parsedRootFiles[0].fileName }, bundledContent: bundledFile };
};
},
/* /*
*
* @description Takes in parsed root files and bundle it * @description Takes in parsed root files and bundle it
* @param {object} parsedRootFiles - found parsed root files * @param {object} parsedRootFiles - found parsed root files
* @param {array} inputData - file data information [{path, content}] * @param {array} inputData - file data information [{path, content}]
@@ -4856,14 +4880,7 @@ module.exports = {
return bundleData; return bundleData;
}); });
let bundleData = data.map((contentAndComponents) => { let bundleData = data.map(this.mapBundleOutput(format, parsedRootFiles));
let bundledFile = contentAndComponents.fileContent;
bundledFile.components = contentAndComponents.components;
if (format === parse.YAML_FORMAT) {
bundledFile = parse.toYAML(bundledFile);
}
return { rootFile: { path: parsedRootFiles[0].fileName }, bundledContent: bundledFile };
});
return bundleData; return bundleData;
}, },
@@ -4887,11 +4904,11 @@ module.exports = {
let parsedContent = this.parseFileOrThrow(rootFile.content); let parsedContent = this.parseFileOrThrow(rootFile.content);
return { fileName: rootFile.fileName, content: rootFile.content, parsed: parsedContent }; return { fileName: rootFile.fileName, content: rootFile.content, parsed: parsedContent };
}).filter((rootWithParsedContent) => { }).filter((rootWithParsedContent) => {
bundleFormat = bundleFormat ? bundleFormat : rootWithParsedContent.parsed.inputFormat; // bundleFormat = bundleFormat ? bundleFormat : rootWithParsedContent.parsed.inputFormat;
return compareVersion(version, rootWithParsedContent.parsed.oasObject.openapi); return compareVersion(version, rootWithParsedContent.parsed.oasObject.openapi);
}), }),
data = toBundle ? data = toBundle ?
this.getBundledFileData(parsedRootFiles, inputData, origin, bundleFormat.toLowerCase()) : this.getBundledFileData(parsedRootFiles, inputData, origin, bundleFormat) :
this.getRelatedFilesData(parsedRootFiles, inputData, origin); this.getRelatedFilesData(parsedRootFiles, inputData, origin);
return data; return data;

View File

@@ -0,0 +1,37 @@
{
"openapi": "3.0.0",
"info": {
"title": "Sample API",
"description": "Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.",
"version": "0.1.9"
},
"servers": [
{
"url": "http://api.example.com/v1",
"description": "Optional server description, e.g. Main (production) server"
},
{
"url": "http://staging-api.example.com",
"description": "Optional server description, e.g. Internal staging server for testing"
}
],
"paths": {
"/users/{userId}": {
"get": {
"summary": "Get a user by ID",
"responses": {
"200": {
"description": "A single user.",
"content": {
"application/json": {
"schema": {
"$ref": "./schemas/user.yaml"
}
}
}
}
}
}
}
}
}

View File

@@ -1116,6 +1116,49 @@ describe('bundle files method - 3.0', function () {
expect(res.output.data.length).to.equal(1); expect(res.output.data.length).to.equal(1);
expect(JSON.stringify(res.output.data[0].bundledContent, null, 2)).to.be.equal(expected); expect(JSON.stringify(res.output.data[0].bundledContent, null, 2)).to.be.equal(expected);
}); });
it('Should bundle according to the input root format when bundleFormat is not present', async function () {
let contentRootFile = fs.readFileSync(schemaFromResponse + '/root.yaml', 'utf8'),
contentRootJSON = fs.readFileSync(schemaFromResponse + '/root.json', 'utf8'),
user = fs.readFileSync(schemaFromResponse + '/schemas/user.yaml', 'utf8'),
expectedJSON = fs.readFileSync(schemaFromResponse + '/expected.json', 'utf8'),
expected = fs.readFileSync(schemaFromResponse + '/expected.yaml', 'utf8'),
input = {
type: 'multiFile',
specificationVersion: '3.0',
rootFiles: [
{
path: '/root.json'
},
{
path: '/root.yaml'
}
],
data: [
{
path: '/root.yaml',
content: contentRootFile
},
{
path: '/root.json',
content: contentRootJSON
},
{
path: '/schemas/user.yaml',
content: user
}
],
options: {}
};
const res = await Converter.bundle(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.specification.version).to.equal('3.0');
expect(res.output.data.length).to.equal(2);
expect(JSON.stringify(res.output.data[0].bundledContent, null, 2)).to.be.equal(expectedJSON);
expect(res.output.data[1].bundledContent).to.be.equal(expected);
});
}); });
@@ -1177,63 +1220,4 @@ describe('getReferences method when node does not have any reference', function(
expect(result.referencesInNode[0].newValue.$ref).to.equal('the/parent/user.yaml'); expect(result.referencesInNode[0].newValue.$ref).to.equal('the/parent/user.yaml');
}); });
it('should return error when "type" parameter is not sent', async function () {
let input = {
rootFiles: [
{
path: '/root.yaml',
content: ''
}
],
data: [
{
path: '/examples.yaml',
content: ''
}
],
options: {},
bundleFormat: 'JSON'
};
try {
await Converter.bundle(input);
}
catch (error) {
expect(error).to.not.be.undefined;
expect(error.message).to.equal('"Type" parameter should be provided');
}
});
it('should return error when input is an empty object', async function () {
try {
await Converter.bundle({});
}
catch (error) {
expect(error).to.not.be.undefined;
expect(error.message).to.equal('Input object must have "type" and "data" information');
}
});
it('should return error when input data is an empty array', async function () {
try {
await Converter.bundle({ type: 'multiFile', data: [] });
}
catch (error) {
expect(error).to.not.be.undefined;
expect(error.message).to.equal('"Data" parameter should be provided');
}
});
it('should return error when "type" parameter is not multiFile', async function () {
try {
await Converter.bundle({
type: 'folder',
bundleFormat: 'JSON',
data: []
});
}
catch (error) {
expect(error).to.not.be.undefined;
expect(error.message).to.equal('"Type" parameter value allowed is multiFile');
}
});
}); });