Adding bundleFormat support

This commit is contained in:
Erik Mendoza
2022-05-24 22:00:48 -05:00
parent 10524b0911
commit 0f8d118ed4
3 changed files with 83 additions and 9 deletions

View File

@@ -4826,14 +4826,16 @@ module.exports = {
return data;
},
getBundledFileData(parsedRootFiles, inputData, origin, version) {
getBundledFileData(parsedRootFiles, inputData, origin, format) {
const data = parsedRootFiles.map((root) => {
let calledAs = [],
bundleData = getRelatedFilesAndBundleData(root, inputData, origin, version, calledAs);
let bundleData = getRelatedFilesAndBundleData(root, inputData, origin);
return bundleData;
});
let bundledFile = data[0].fileContent;
bundledFile.components = data[0].components;
if (format === parse.YAML_FORMAT) {
bundledFile = parse.toYAML(bundledFile);
}
return { rootFile: { path: parsedRootFiles[0].fileName }, bundledContent: bundledFile };
},
@@ -4845,19 +4847,22 @@ module.exports = {
* @param {array} inputData - [{path:string}]}
* @param {string} origin - process origin
* @param {string} version - process specification version
* @param {string} format - the format required by the user
* @param {boolean} toBundle - if it will be used in bundle
*
* @returns {object} root files information and data input
*/
mapProcessRelatedFiles(rootFiles, inputData, origin, version, toBundle = false) {
let parsedRootFiles = rootFiles.map((rootFile) => {
mapProcessRelatedFiles(rootFiles, inputData, origin, version, format, toBundle = false) {
let bundleFormat = format,
parsedRootFiles = rootFiles.map((rootFile) => {
let parsedContent = parse.getOasObject(rootFile.content);
return { fileName: rootFile.fileName, content: rootFile.content, parsed: parsedContent };
}).filter((rootWithParsedContent) => {
bundleFormat = bundleFormat ? bundleFormat : rootWithParsedContent.parsed.inputFormat;
return compareVersion(version, rootWithParsedContent.parsed.oasObject.openapi);
}),
data = toBundle ?
this.getBundledFileData(parsedRootFiles, inputData, origin, version) :
this.getBundledFileData(parsedRootFiles, inputData, origin, bundleFormat.toLowerCase()) :
this.getRelatedFilesData(parsedRootFiles, inputData, origin);
return data;
@@ -4888,7 +4893,7 @@ module.exports = {
};
if (inputRelatedFiles.rootFiles && inputRelatedFiles.rootFiles.length > 0) {
res.output.data = this.mapProcessRelatedFiles(inputRelatedFiles.rootFiles, inputRelatedFiles.data,
inputRelatedFiles.origin, version, toBundle);
inputRelatedFiles.origin, version, inputRelatedFiles.bundleFormat, toBundle);
return res;
}
else {

View File

@@ -0,0 +1 @@
"openapi: 3.0.0\ninfo:\n title: Sample API\n description: >-\n Optional multiline or single-line description in\n [CommonMark](http://commonmark.org/help/) or HTML.\n version: 0.1.9\nservers:\n - url: 'http://api.example.com/v1'\n description: 'Optional server description, e.g. Main (production) server'\n - url: 'http://staging-api.example.com'\n description: 'Optional server description, e.g. Internal staging server for testing'\npaths:\n '/users/{userId}':\n get:\n summary: Get a user by ID\n responses:\n '200':\n description: A single user.\n content:\n application/json:\n schema:\n $ref: '#/components/schemas/~1schemas~1user.yaml'\ncomponents:\n schemas:\n /schemas/user.yaml:\n type: object\n properties:\n id:\n type: integer\n userName:\n type: string\n"

View File

@@ -11,7 +11,7 @@ let expect = require('chai').expect,
withParamsFolder = path.join(__dirname, BUNDLES_FOLDER + '/with_parameters'),
withRefInItems = path.join(__dirname, BUNDLES_FOLDER + '/with_ref_in_items');
describe('bundle files method', function () {
describe('bundle files method - 3.0', function () {
it('Should return bundled file with an schema called from a response', async function () {
let contentRootFile = fs.readFileSync(easyFolder + '/root.yaml', 'utf8'),
user = fs.readFileSync(easyFolder + '/schemas/user.yaml', 'utf8'),
@@ -30,7 +30,9 @@ describe('bundle files method', function () {
path: '/schemas/user.yaml',
content: user
}
]
],
options: {},
bundleFormat: 'JSON'
};
const res = await Converter.bundle(input);
expect(res).to.not.be.empty;
@@ -43,6 +45,62 @@ describe('bundle files method', function () {
expect(JSON.stringify(res.output.data.bundledContent)).to.be.equal(expected);
});
it('Should return bundled file in yaml format providing bundleFormat', async function () {
let contentRootFile = fs.readFileSync(easyFolder + '/root.yaml', 'utf8'),
user = fs.readFileSync(easyFolder + '/schemas/user.yaml', 'utf8'),
expected = fs.readFileSync(easyFolder + '/expected.yaml', 'utf8'),
input = {
type: 'folder',
specificationVersion: '3.0',
rootFiles: [
{
path: '/root.yaml',
content: contentRootFile
}
],
data: [
{
path: '/schemas/user.yaml',
content: user
}
],
options: {},
bundleFormat: 'yaml'
};
const res = await Converter.bundle(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(JSON.stringify(res.output.data.bundledContent)).to.be.equal(expected);
});
it('Should return bundled file in yaml format when user does not provide bundleFormat' +
' and the provided input is yaml', async function () {
let contentRootFile = fs.readFileSync(easyFolder + '/root.yaml', 'utf8'),
user = fs.readFileSync(easyFolder + '/schemas/user.yaml', 'utf8'),
expected = fs.readFileSync(easyFolder + '/expected.yaml', 'utf8'),
input = {
type: 'folder',
specificationVersion: '3.0',
rootFiles: [
{
path: '/root.yaml',
content: contentRootFile
}
],
data: [
{
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(JSON.stringify(res.output.data.bundledContent)).to.be.equal(expected);
});
it('Should return bundled file from root with components with', async function () {
let contentRootFile = fs.readFileSync(swaggerMultifileFolder + '/v1.yaml', 'utf8'),
responses = fs.readFileSync(swaggerMultifileFolder + '/responses.yaml', 'utf8'),
@@ -61,6 +119,8 @@ describe('bundle files method', function () {
content: contentRootFile
}
],
options: {},
bundleFormat: 'JSON',
data: [
{
path: '/responses.yaml',
@@ -110,6 +170,8 @@ describe('bundle files method', function () {
content: contentRootFile
}
],
options: {},
bundleFormat: 'JSON',
data: [
{
path: '/responses.yaml',
@@ -163,6 +225,8 @@ describe('bundle files method', function () {
content: contentRootFile
}
],
options: {},
bundleFormat: 'JSON',
data: [
{
path: '/spec/NewPet.yaml',
@@ -250,6 +314,8 @@ describe('bundle files method', function () {
content: contentRootFile
}
],
options: {},
bundleFormat: 'JSON',
data: [
{
path: '/schemas/user.yaml',
@@ -286,6 +352,8 @@ describe('bundle files method', function () {
content: contentRootFile
}
],
options: {},
bundleFormat: 'JSON',
data: [
{
path: '/schemas/user.yaml',