Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
options.allowFailure = false;
if ((options.source.indexOf('!')>=0) && (options.source.indexOf('swagger.')>=0)) {
expectFailure = true;
options.expectFailure = true;
options.allowFailure = true;
}
if ((options.source.indexOf('!')>=0) && (options.source.indexOf('openapi.')>=0)) {
expectFailure = true;
options.expectFailure = false; // because some things are corrected
options.allowFailure = true;
}
if (file.startsWith('http')) {
swagger2openapi.convertUrl(file, clone(options))
.then(function(options){
handleResult(null,options);
})
.catch(function(ex){
console.warn(common.colour.red+ex,common.colour.normal);
if (expectFailure) {
warnings.push('Converter failed ' + options.source);
}
else {
failures.push('Converter failed ' + options.source);
fail++;
}
genStackNext();
result = false;
});
}
return maybe(callback, new Promise(function (resolve, reject) {
if (!swagger) swagger = {};
options.original = swagger;
if (!options.text) options.text = yaml.stringify(swagger);
options.externals = [];
options.externalRefs = {};
options.rewriteRefs = true; // avoids stack explosions
options.preserveMiro = true;
options.promise = {};
options.promise.resolve = resolve;
options.promise.reject = reject;
if (!options.cache) options.cache = {};
if (options.source) options.cache[options.source] = options.original;
if (swagger.openapi && (typeof swagger.openapi === 'string') && swagger.openapi.startsWith('3.')) {
options.openapi = cclone(swagger);
fixInfo(options.openapi, options, reject);
fixPaths(options.openapi, options, reject);
resolver.optionalResolve(options) // is a no-op if options.resolve is not set
.then(function(){
if (options.direct) {
return resolve(options.openapi);
}
else {
return resolve(options);
}
})
.catch(function(ex){
console.warn(ex);
reject(ex);
});
if (!operation.bodyParam.required) operation.hasOptionalParams = true;
operation.bodyParam.dataType = typeMap('object',operation.bodyParam.required,{}); // can be changed below
operation.bodyParam.description = op.requestBody.description||'';
operation.bodyParam.schema = {};
operation.bodyParam.isEnum = false; // TODO?
operation.bodyParam.vendorExtensions = specificationExtensions(op.requestBody);
if (op.requestBody.content) {
let contentType = Object.values(op.requestBody.content)[0];
let mt = { mediaType: Object.keys(op.requestBody.content)[0] };
operation.consumes.push(mt);
operation.hasConsumes = true;
let tmp = obj.consumes.find(function(e,i,a){
return (e.mediaType === mt.mediaType);
});
if (!tmp) {
obj.consumes.push(clone(mt)); // so convertArray works correctly
obj.hasConsumes = true;
}
operation.bodyParam.schema = contentType.schema;
operation.bodyParam.example = JSON.stringify(safeSample(contentType.schema,{},api));
for (let p in schemaProperties) {
if (typeof contentType.schema[p] !== 'undefined') operation.bodyParam[p] = contentType.schema[p];
}
if (contentType.schema.type) {
operation.bodyParam.type = contentType.schema.type;
operation.bodyParam.dataType = typeMap(contentType.schema.type,operation.bodyParam.required,contentType.schema); // this is the below mentioned
}
}
operation.bodyParam["%dataType%"] = operation.bodyParam.dataType; // bug in typescript-fetch template?
operation.bodyParam.jsonSchema = safeJson({schema: operation.bodyParam.schema},null,2);
operation.bodyParams.push(operation.bodyParam);
operation.bodyParam.isFile = false; // TODO
}
operation.allParams.push(parameter);
if (param.in === 'path') {
parameter.isPathParam = true;
operation.pathParams.push(clone(parameter));
operation.hasPathParams = true;
}
if (param.in === 'query') {
parameter.isQueryParam = true;
operation.queryParams.push(clone(parameter));
operation.hasQueryParams = true;
}
if (param.in === 'header') {
parameter.isHeaderParam = true;
operation.headerParams.push(clone(parameter));
operation.hasHeaderParams = true;
}
/* if (param.in === 'form') { // TODO need to do this in requestBody
parameter.isFormParam = true;
operation.formParams.push(clone(parameter));
operation.hasFormParams = true;
}*/
} // end of effective parameters
operation.bodyParams = [];
if (op.requestBody) {
operation.openapi.requestBody = op.requestBody;
operation.hasParams = true;
operation.hasBodyParam = true;
operation.bodyParam = {};
operation.bodyParam.isBodyParam = true;
}
operation.bodyParam.schema = contentType.schema;
operation.bodyParam.example = JSON.stringify(safeSample(contentType.schema,{},api));
for (let p in schemaProperties) {
if (typeof contentType.schema[p] !== 'undefined') operation.bodyParam[p] = contentType.schema[p];
}
if (contentType.schema.type) {
operation.bodyParam.type = contentType.schema.type;
operation.bodyParam.dataType = typeMap(contentType.schema.type,operation.bodyParam.required,contentType.schema); // this is the below mentioned
}
}
operation.bodyParam["%dataType%"] = operation.bodyParam.dataType; // bug in typescript-fetch template?
operation.bodyParam.jsonSchema = safeJson({schema: operation.bodyParam.schema},null,2);
operation.bodyParams.push(operation.bodyParam);
operation.bodyParam.isFile = false; // TODO
operation.allParams.push(clone(operation.bodyParam));
}
operation.tags = op.tags;
operation.imports = op.tags;
operation.vendorExtensions = specificationExtensions(op);
operation.responses = [];
for (let r in op.responses) {
let response = op.responses[r];
let entry = {};
entry.code = r;
entry.isDefault = (r === 'default');
entry.nickname = 'response'+r;
entry.message = response.description;
entry.description = response.description||'';
entry.simpleType = true;
entry.schema = {};
})
.catch(function(ex){
console.warn(common.colour.red+ex,common.colour.normal);
if (expectFailure) {
warnings.push('Converter failed ' + options.source);
}
else {
failures.push('Converter failed ' + options.source);
fail++;
}
genStackNext();
result = false;
});
}
else {
swagger2openapi.convertObj(src, clone(options))
.then(function(options){
handleResult(null,options);
})
.catch(function(ex){
console.warn(common.colour.red+ex,common.colour.normal);
console.warn(ex.stack);
if (expectFailure) {
warnings.push('Converter failed ' + options.source);
}
else {
failures.push('Converter failed ' + options.source);
fail++;
}
genStackNext();
result = false;
});
if (!fs.existsSync(path.join(outputDir,subDir,file))) {
ff.createFile(path.join(outputDir,subDir,file),'','utf8');
}
}
}
}
if (config.apache) {
ff.createFile(path.join(outputDir,subDir,'LICENSE'),ff.readFileSync(tpl({}, '_common', 'LICENSE'),'utf8'),'utf8');
}
else {
ff.createFile(path.join(outputDir,subDir,'LICENSE'),ff.readFileSync(tpl({}, '_common', 'UNLICENSE'),'utf8'),'utf8');
}
let outer = model;
if (config.perApi) {
let toplevel = clone(model);
delete toplevel.apiInfo;
for (let pa of config.perApi) {
let fnTemplate = Hogan.compile(pa.output);
let template = Hogan.compile(ff.readFileSync(tpl(config, configName, pa.input), 'utf8'));
for (let api of model.apiInfo.apis) {
let cApi = Object.assign({},config.defaults,pa.defaults||{},toplevel,api);
let filename = fnTemplate.render(cApi,config.partials);
if (verbose) console.log('Rendering '+filename+' (dynamic:'+pa.input+')');
ff.createFile(path.join(outputDir,subDir,filename),template.render(cApi,config.partials),'utf8');
}
}
}
if (config.perPath) {
let toplevel = clone(model);
delete toplevel.apiInfo;