swagger2openapi
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -70,2 +70,13 @@ 'use strict'; | ||
const httpVerbs = [ | ||
'get', | ||
'post', | ||
'put', | ||
'delete', | ||
'patch', | ||
'head', | ||
'options', | ||
'trace' | ||
]; | ||
module.exports = { | ||
@@ -80,4 +91,5 @@ | ||
resolve : resolve, | ||
parameterTypeProperties : parameterTypeProperties | ||
parameterTypeProperties : parameterTypeProperties, | ||
httpVerbs : httpVerbs | ||
}; |
55
index.js
@@ -58,2 +58,16 @@ 'use strict'; | ||
function processHeader(header) { | ||
if (header.type && !header.schema) { | ||
header.schema = {}; | ||
} | ||
if (header.type) header.schema.type = header.type; | ||
delete header.type; | ||
for (var prop of common.parameterTypeProperties) { | ||
if (typeof header[prop] !== 'undefined') { | ||
header.schema[prop] = header[prop]; | ||
delete header[prop]; | ||
} | ||
} | ||
} | ||
function processParameter(param,op,path,index,openapi) { | ||
@@ -277,17 +291,24 @@ var result = {}; | ||
} | ||
if (!response.description) response.description = ''; | ||
if (response.schema) { | ||
common.recurse(response,{},fixupSchema); | ||
var produces = (op.produces||[]).concat(openapi.produces||[]).filter(common.uniqueOnly); | ||
if (!produces.length) produces.push('*/*'); // TODO verify default | ||
response.content = {}; | ||
for (var mimetype of produces) { | ||
response.content[mimetype] = {}; | ||
response.content[mimetype].schema = common.clone(response.schema); | ||
if (response.content[mimetype].schema.type == 'file') { | ||
delete response.content[mimetype].schema; | ||
else { | ||
if (!response.description) response.description = ''; | ||
if (response.schema) { | ||
common.recurse(response,{},fixupSchema); | ||
var produces = (op.produces||[]).concat(openapi.produces||[]).filter(common.uniqueOnly); | ||
if (!produces.length) produces.push('*/*'); // TODO verify default | ||
response.content = {}; | ||
for (var mimetype of produces) { | ||
response.content[mimetype] = {}; | ||
response.content[mimetype].schema = common.clone(response.schema); | ||
if (response.content[mimetype].schema.type == 'file') { | ||
delete response.content[mimetype].schema; | ||
} | ||
} | ||
delete response.schema; | ||
} | ||
delete response.schema; | ||
if (response.headers) { | ||
for (var h in response.headers) { | ||
processHeader(response.headers[h]); | ||
} | ||
} | ||
} | ||
@@ -461,2 +482,10 @@ } | ||
common.recurse(openapi.components.responses,{},fixupSchema); | ||
for (var r in openapi.components.responses) { | ||
var response = openapi.components.responses[r]; | ||
if (response.headers) { | ||
for (var h in response.headers) { | ||
processHeader(response.headers[h]); | ||
} | ||
} | ||
} | ||
@@ -463,0 +492,0 @@ return openapi; |
{ | ||
"name": "swagger2openapi", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Convert Swagger 2.0 specifications to OpenApi 3.0", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -19,5 +19,8 @@ 'use strict'; | ||
.describe('stop','stop on first error') | ||
.boolean('quiet') | ||
.alias('q','quiet') | ||
.describe('quiet','do not show test passes on console, for CI') | ||
.count('verbose') | ||
.alias('v','verbose') | ||
.describe('verbose','Increase verbosity') | ||
.describe('verbose','increase verbosity') | ||
.help('h') | ||
@@ -67,3 +70,3 @@ .alias('h', 'help') | ||
if (!src || ((!src.swagger && !src.openapi))) return true; | ||
console.log(normal+file); | ||
if (!argv.quiet) console.log(normal+file); | ||
@@ -79,7 +82,10 @@ try { | ||
console.log(green+' %s %s',src.info.title,src.info.version); | ||
console.log(' %s',src.swagger ? (src.host ? src.host : 'relative') : (src.servers && src.servers.length ? src.servers[0].url : 'relative')); | ||
if (!argv.quiet) { | ||
console.log(green+' %s %s',src.info.title,src.info.version); | ||
console.log(' %s',src.swagger ? (src.host ? src.host : 'relative') : (src.servers && src.servers.length ? src.servers[0].url : 'relative')); | ||
} | ||
result = true; | ||
} | ||
catch (ex) { | ||
if (argv.quiet) console.log(normal+file); | ||
console.log(red+options.context.pop()+'\n'+ex.message); | ||
@@ -86,0 +92,0 @@ result = false; |
@@ -43,3 +43,17 @@ var url = require('url'); | ||
function checkResponse(response,openapi){ | ||
function checkHeader(header,openapi,options) { | ||
if (header.$ref) { | ||
header = common.resolve(openapi,header.$ref); | ||
header.should.not.be.exactly(false,'Could not resolve reference'); | ||
} | ||
header.should.not.have.property('name'); | ||
header.should.not.have.property('in'); | ||
header.should.not.have.property('type'); | ||
for (var prop of common.parameterTypeProperties) { | ||
header.should.not.have.property(prop); | ||
} | ||
} | ||
function checkResponse(response,openapi,options) { | ||
if (response.$ref) { | ||
@@ -51,2 +65,11 @@ response = common.resolve(openapi,response.$ref); | ||
should(response.description).have.type('string','response description should be of type string'); | ||
if (response.headers) { | ||
contextAppend(options,'headers'); | ||
for (var h in response.headers) { | ||
contextAppend(options,h); | ||
checkHeader(response.headers[h],openapi); | ||
options.context.pop(); | ||
} | ||
options.context.pop(); | ||
} | ||
} | ||
@@ -72,3 +95,2 @@ | ||
} | ||
param.should.not.have.property('default'); // TODO just testing | ||
param.in.should.not.be.exactly('body','Parameter type body is no-longer valid'); | ||
@@ -92,15 +114,6 @@ param.in.should.not.be.exactly('formData','Parameter type formData is no-longer valid'); | ||
} | ||
else if (o.startsWith('x-')) { | ||
// nop | ||
} | ||
else if (o == 'summary') { | ||
// nop | ||
} | ||
else if (o == 'description') { | ||
// nop | ||
} | ||
else if (o == 'servers') { | ||
checkServers(op); // won't be here in converted specs | ||
} | ||
else { | ||
else if (common.httpVerbs.indexOf(o)>=0) { | ||
op.should.not.have.property('consumes'); | ||
@@ -115,3 +128,3 @@ op.should.not.have.property('produces'); | ||
var response = op.responses[r]; | ||
checkResponse(response,openapi); | ||
checkResponse(response,openapi,options); | ||
options.context.pop(); | ||
@@ -147,2 +160,5 @@ } | ||
openapi.openapi.should.have.type('string'); | ||
openapi.should.not.have.key('host'); | ||
openapi.should.not.have.key('basePath'); | ||
openapi.should.not.have.key('schemes'); | ||
openapi.should.have.key('paths'); | ||
@@ -261,8 +277,8 @@ openapi.should.not.have.key('definitions'); | ||
if (openapi.components && openapi.components.parameters) { | ||
options.context.push('#/components/parameters/'); | ||
for (var p in openapi.components.parameters) { | ||
options.context.push('#/components/parameters/'+p); | ||
validateComponentName(p).should.be.ok(); | ||
checkParam(openapi.components.parameters[p],p,openapi,options); | ||
options.context.pop(); | ||
} | ||
options.context.pop(); | ||
} | ||
@@ -293,4 +309,4 @@ for (var p in openapi.paths) { | ||
options.context.push('#/components/responses/'+r); | ||
validateComponentName(s).should.be.ok(); | ||
checkResponse(openapi.components.responses[r],openapi); | ||
validateComponentName(r).should.be.ok(); | ||
checkResponse(openapi.components.responses[r],openapi,options); | ||
options.context.pop(); | ||
@@ -300,2 +316,11 @@ } | ||
if (openapi.components && openapi.components.headers) { | ||
for (var h in openapi.components.headers) { | ||
options.context.push('#/components/headers/'+h); | ||
validateComponentName(h).should.be.ok(); | ||
checkHeader(openapi.components.headers[h],openapi,options); | ||
options.context.pop(); | ||
} | ||
} | ||
return true; | ||
@@ -302,0 +327,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
98458
14
1170