Add root detection

Add root detection in bundle and detect related files
This commit is contained in:
Luis Tejeda
2022-06-09 12:16:11 -05:00
parent e2a7b3fb05
commit 9b9164c0c2
3 changed files with 159 additions and 4 deletions

View File

@@ -669,7 +669,18 @@ class SchemaPack {
schemaUtils.validateInputMultiFileAPI(input);
if (!input.rootFiles || input.rootFiles.length === 0) {
throw new Error('Input should have at least one root file');
let rootFiles = await this.detectRootFiles(input);
if (rootFiles.output.data) {
let inputContent = [];
rootFiles.output.data.forEach((rootFile) => {
let founInData = input.data.find((dataFile) => { return dataFile.fileName === rootFile.path; });
if (founInData) {
inputContent.push({ fileName: founInData.fileName, content: founInData.content });
}
});
input.rootFiles = inputContent;
return schemaUtils.processRelatedFiles(input);
}
}
let adaptedRootFiles = input.rootFiles.map((rootFile) => {
@@ -699,7 +710,18 @@ class SchemaPack {
const input = this.input;
schemaUtils.validateInputMultiFileAPI(input);
if (!input.rootFiles || input.rootFiles.length === 0) {
throw new Error('Input should have at least one root file');
let rootFiles = await this.detectRootFiles(input);
if (rootFiles.output.data) {
let inputContent = [];
rootFiles.output.data.forEach((rootFile) => {
let founInData = input.data.find((dataFile) => { return dataFile.fileName === rootFile.path; });
if (founInData) {
inputContent.push({ fileName: founInData.fileName, content: founInData.content });
}
});
input.rootFiles = inputContent;
return schemaUtils.processRelatedFiles(input, true);
}
}
let adaptedRootFiles = input.rootFiles.map((rootFile) => {
let foundInData = input.data.find((file) => { return file.fileName === rootFile.path; });

View File

@@ -826,8 +826,14 @@ describe('bundle files method - 3.0', function () {
expect(JSON.stringify(res.output.data[0].bundledContent, null, 2)).to.be.equal(expected);
});
it('Should throw error when root files is undefined', async function () {
it('Should take the root file from data array root file prop undefined', async function () {
let contentRootFile = fs.readFileSync(sameRefDiffSource + '/root.yaml', 'utf8'),
user = fs.readFileSync(sameRefDiffSource + '/schemas/user/user.yaml', 'utf8'),
client = fs.readFileSync(sameRefDiffSource + '/schemas/client/client.yaml', 'utf8'),
specialUser = fs.readFileSync(sameRefDiffSource + '/schemas/user/special.yaml', 'utf8'),
specialClient = fs.readFileSync(sameRefDiffSource + '/schemas/client/special.yaml', 'utf8'),
magic = fs.readFileSync(sameRefDiffSource + '/schemas/client/magic.yaml', 'utf8'),
expected = fs.readFileSync(sameRefDiffSource + '/expected.json', 'utf8'),
input = {
type: 'multiFile',
specificationVersion: '3.0',
@@ -835,6 +841,46 @@ describe('bundle files method - 3.0', function () {
{
path: '/root.yaml',
content: contentRootFile
},
{
path: '/schemas/user/user.yaml',
content: user
},
{
path: '/schemas/user/special.yaml',
content: specialUser
},
{
path: '/schemas/client/client.yaml',
content: client
},
{
path: '/schemas/client/special.yaml',
content: specialClient
},
{
path: '/schemas/client/magic.yaml',
content: magic
}
],
options: {},
bundleFormat: 'JSON'
};
const res = await Converter.bundle(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(JSON.stringify(res.output.data[0].bundledContent, null, 2)).to.be.equal(expected);
});
it('Should throw error when root files is undefinied and in data there is no root file', async function () {
let user = fs.readFileSync(schemaFromResponse + '/schemas/user.yaml', 'utf8'),
input = {
type: 'multiFile',
specificationVersion: '3.0',
data: [
{
path: '/schemas/user.yaml',
content: user
}
],
options: {},
@@ -849,7 +895,54 @@ describe('bundle files method - 3.0', function () {
}
});
it('Should throw error when root files is empty array', async function () {
it('Should take the root file from data array root file prop empty array', async function () {
let contentRootFile = fs.readFileSync(sameRefDiffSource + '/root.yaml', 'utf8'),
user = fs.readFileSync(sameRefDiffSource + '/schemas/user/user.yaml', 'utf8'),
client = fs.readFileSync(sameRefDiffSource + '/schemas/client/client.yaml', 'utf8'),
specialUser = fs.readFileSync(sameRefDiffSource + '/schemas/user/special.yaml', 'utf8'),
specialClient = fs.readFileSync(sameRefDiffSource + '/schemas/client/special.yaml', 'utf8'),
magic = fs.readFileSync(sameRefDiffSource + '/schemas/client/magic.yaml', 'utf8'),
expected = fs.readFileSync(sameRefDiffSource + '/expected.json', 'utf8'),
input = {
type: 'multiFile',
specificationVersion: '3.0',
rootFiles: [],
data: [
{
path: '/root.yaml',
content: contentRootFile
},
{
path: '/schemas/user/user.yaml',
content: user
},
{
path: '/schemas/user/special.yaml',
content: specialUser
},
{
path: '/schemas/client/client.yaml',
content: client
},
{
path: '/schemas/client/special.yaml',
content: specialClient
},
{
path: '/schemas/client/magic.yaml',
content: magic
}
],
options: {},
bundleFormat: 'JSON'
};
const res = await Converter.bundle(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(JSON.stringify(res.output.data[0].bundledContent, null, 2)).to.be.equal(expected);
});
it('Should throw error when root files is empty array and in data there is no root file', async function () {
let user = fs.readFileSync(schemaFromResponse + '/schemas/user.yaml', 'utf8'),
input = {
type: 'multiFile',

View File

@@ -489,4 +489,44 @@ describe('detectRelatedFiles method', function () {
expect(res.output.data[0].rootFile.path).to.equal('/petstore.yaml');
expect(res.output.data.length).to.equal(1);
});
it('Should take the root file from data array root file prop empty array', async function () {
let contentFile = fs.readFileSync(swaggerRoot, 'utf8'),
input = {
type: 'multiFile',
specificationVersion: '3.0',
rootFiles: [
],
data: [
{
path: '/swagger.yaml',
content: contentFile
}
]
};
const res = await Converter.detectRelatedFiles(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.data[0].rootFile.path).to.equal('/swagger.yaml');
expect(res.output.data.length).to.equal(1);
});
it('Should take the root file from data array root file prop undefined', async function () {
let contentFile = fs.readFileSync(swaggerRoot, 'utf8'),
input = {
type: 'multiFile',
specificationVersion: '3.0',
data: [
{
path: '/swagger.yaml',
content: contentFile
}
]
};
const res = await Converter.detectRelatedFiles(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.data[0].rootFile.path).to.equal('/swagger.yaml');
expect(res.output.data.length).to.equal(1);
});
});