Added first scenarios

Added first scenarios
This commit is contained in:
Luis Tejeda
2022-04-19 12:23:06 -05:00
committed by Erik Mendoza
parent 006c3de8a8
commit 36e83721dd
5 changed files with 168 additions and 4 deletions

View File

@@ -32,6 +32,11 @@ module.exports = {
return SchemaPack.getOptions(mode, criteria);
},
detectRootFiles: async function(input) {
var schema = new SchemaPack(input);
return schema.detectRootFiles();
},
// new API
SchemaPack
};

View File

@@ -22,7 +22,8 @@ function getVersionRegexp({ key, version }) {
* When the array of files is provided as a list of parsed objects
* it returns the content from file that contains the version data
* @param {array} data An array of the provided file's content parsed
* @returns {string} The content of the file that contains the version data
* @returns {object} object with hasDefinedVersion property and
* The content of the file that contains the version data
*/
function getFileByContent(data) {
const version2RegExp = getVersionRegexp(GENERIC_VERSION2),
@@ -30,7 +31,10 @@ function getFileByContent(data) {
file = data.find((element) => {
return element.content.match(version2RegExp) || element.content.match(version3RegExp);
});
return file.content;
if (!file) {
return { hasDefinedVersion: false };
}
return { hasDefinedVersion: true, content: file.content };
}
/**
@@ -53,7 +57,8 @@ function getFileByFileName(data) {
/** 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
* @returns {string} The content of file with version data
* @returns {object} object with hasDefinedVersion property and
* The content of the file that contains the version data
*/
function getFileWithVersion(data) {
let file;
@@ -88,6 +93,12 @@ function getSpecVersion({ type, data }) {
return DEFAULT_SPEC_VERSION; // If path is invalid it will follow the OAS 3.0 way
}
}
if (!data.hasDefinedVersion) {
return;
}
else {
data = data.content;
}
if (type === 'json') {
data = JSON.stringify(data);
@@ -119,6 +130,9 @@ function getSpecVersion({ type, data }) {
*/
function getConcreteSchemaUtils({ type, data }) {
const specVersion = getSpecVersion({ type, data });
if (!specVersion) {
return;
}
let concreteUtils = {};
if (specVersion === DEFAULT_SPEC_VERSION) {
concreteUtils = require('../30XUtils/schemaUtils30X');

View File

@@ -4786,5 +4786,30 @@ module.exports = {
});
return endpoints;
},
inputValidation
inputValidation,
mapDetectRootFilesInputToGetRootFilesInput(input) {
let adaptedData = input.data.map((file) => {
return { fileName: file.path };
});
return { data: adaptedData };
},
mapGetRootFilesOutputToDetectRootFilesOutput(output, version) {
let adaptedData = output.map((file) => {
return { path: file };
});
return {
result: true,
output: {
type: 'rootFiles',
specification: {
type: 'OpenAPI',
version: version
},
data: adaptedData
}
};
}
};

View File

@@ -53,6 +53,8 @@ class SchemaPack {
let indentCharacter = this.computedOptions.indentCharacter;
this.computedOptions.indentCharacter = indentCharacter === 'tab' ? '\t' : ' ';
concreteUtils = getConcreteSchemaUtils(input);
this.hasDefinedVersion = concreteUtils !== undefined;
this.validate();
}
@@ -604,6 +606,35 @@ class SchemaPack {
static getOptions(mode, criteria) {
return getOptions(mode, criteria);
}
async detectRootFiles() {
const input = this.input;
if (input.data[0].path === '') {
throw new Error('undefined input');
}
if (!this.hasDefinedVersion) {
return schemaUtils.mapGetRootFilesOutputToDetectRootFilesOutput([], input.specificationVersion);
}
let files = {},
rootFiles,
res,
adaptedInput;
if (input.origin === BROWSER) {
path = pathBrowserify;
OasResolverOptions.browser = true;
}
if ('content' in input.data[0]) {
input.data.forEach((file) => {
files[path.resolve(file.path)] = file.content ? file.content : '';
});
}
adaptedInput = schemaUtils.mapDetectRootFilesInputToGetRootFilesInput(input);
rootFiles = parse.getRootFiles(adaptedInput, concreteUtils.inputValidation, this.computedOptions, files);
res = schemaUtils.mapGetRootFilesOutputToDetectRootFilesOutput(rootFiles, input.specificationVersion);
return res;
}
}
module.exports = {

View File

@@ -0,0 +1,89 @@
var expect = require('chai').expect,
Converter = require('../../index.js'),
fs = require('fs'),
path = require('path'),
VALID_OPENAPI_PATH = '../data/valid_openapi',
validPetstore = path.join(__dirname, VALID_OPENAPI_PATH + '/petstore.yaml'),
noauth = path.join(__dirname, VALID_OPENAPI_PATH + '/noauth.yaml');
describe('requestNameSource option', function() {
it('should return one root 3.0 correctly', async function() {
let contentFile = fs.readFileSync(validPetstore, 'utf8'),
input = {
type: 'folder',
specificationVersion: '3.0',
data: [
{
path: '/petstore.yaml',
content: contentFile
}
]
};
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('/petstore.yaml');
});
it('should return no root file when there is not a root file present', async function() {
let input = {
type: 'folder',
specificationVersion: '3.0',
data: [
{
path: '/petstore.yaml',
content: 'not root'
}
]
};
const res = await Converter.detectRootFiles(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.data).to.be.empty;
});
it('should return 2 root 3.0 correctly', async function() {
let petstoreContent = fs.readFileSync(validPetstore, 'utf8'),
noAuthContent = fs.readFileSync(noauth, 'utf8'),
input = {
type: 'folder',
specificationVersion: '3.0',
data: [
{
path: '/petstore.yaml',
content: petstoreContent
},
{
path: '/noauth.yaml',
content: noAuthContent
}
]
};
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('/petstore.yaml');
expect(res.output.data[1].path).to.equal('/noauth.yaml');
});
it('should propagate one error correctly', async function () {
let input = {
type: 'folder',
specificationVersion: '3.0',
data: [
{
path: '',
content: 'openapi: 3.0.0'
}
]
};
try {
await Converter.detectRootFiles(input);
}
catch (ex) {
expect(ex.message).to.equal('undefined input');
}
});
});