swagger2openapi
Advanced tools
Comparing version 1.2.1 to 1.3.0
'use strict'; | ||
var crypto = require('crypto'); | ||
var util = require('util'); | ||
var jptr = require('jgexml/jpath.js'); | ||
@@ -44,8 +46,22 @@ | ||
function resolve(root,pointer) { | ||
function* resolve(root,pointer,callback) { | ||
// TODO to be extended to resolve external references | ||
// use yield to wrap node-fetch for url refs ? | ||
return jptr.jptr(root,pointer); | ||
var result = yield jptr.jptr(root,pointer); | ||
callback(null,result); | ||
return result; | ||
} | ||
function resolveSync(root,pointer) { | ||
var obj = false; | ||
var r = resolve(root,pointer,function(err,data){ | ||
obj = data; | ||
}); | ||
var result = r.next(); | ||
while (!result.done) { | ||
result = r.next(result.value); | ||
} | ||
return obj; // just in case | ||
} | ||
const parameterTypeProperties = [ | ||
@@ -91,2 +107,3 @@ 'format', | ||
resolve : resolve, | ||
resolveSync : resolveSync, | ||
parameterTypeProperties : parameterTypeProperties, | ||
@@ -93,0 +110,0 @@ httpVerbs : httpVerbs |
67
index.js
'use strict'; | ||
var fs = require('fs'); | ||
var fetch = require('node-fetch'); | ||
var yaml = require('js-yaml'); | ||
var common = require('./common.js'); | ||
@@ -95,3 +100,3 @@ | ||
if (rbody) { | ||
param = common.resolve(openapi,param.$ref); | ||
param = common.resolveSync(openapi,param.$ref); | ||
if (!param) common.forceFailure('Could not resolve reference'); | ||
@@ -289,2 +294,3 @@ } | ||
if (response.$ref) { | ||
if (response.description) delete response.description; // rebilly! | ||
response.$ref = response.$ref.replace('#/responses/','#/components/responses/'); | ||
@@ -356,3 +362,3 @@ } | ||
function convert(swagger, options) { | ||
function convertSync(swagger, options) { | ||
if ((swagger.openapi) && (swagger.openapi.startsWith('3.'))) return swagger; | ||
@@ -495,7 +501,62 @@ if ((!swagger.swagger) || (swagger.swagger != "2.0")) return {}; // handle 1.2 later? | ||
function convertObj(swagger,options,callback) { | ||
process.nextTick(function(){ | ||
convertSync(swagger,options,callback); | ||
}); | ||
} | ||
function convertStr(str,options,callback) { | ||
var obj = null; | ||
try { | ||
obj = JSON.parse(str); | ||
} | ||
catch (ex) { | ||
try { | ||
obj = yaml.safeLoad(str); | ||
} | ||
catch (ex) {} | ||
} | ||
if (obj) { | ||
convertObj(obj,options,callback); | ||
} | ||
else { | ||
callback(new Error('Could not resolve url'),null,options); | ||
} | ||
} | ||
function convertUrl(url,options,callback) { | ||
if (!options.origin) { | ||
options.origin = url; | ||
} | ||
fetch(url).then(function(res) { | ||
return res.text(); | ||
}).then(function(body) { | ||
convertStr(body,options,callback); | ||
}).catch(function(err){ | ||
callback(err,null,options); | ||
}); | ||
} | ||
function convertFile(filename,options,callback) { | ||
fs.readFile(filename,options.encoding||'utf8',function(err,s){ | ||
if (err) { | ||
callback(err,null,options); | ||
} | ||
else { | ||
options.sourceFile = filename; | ||
convertStr(s,options,callback); | ||
} | ||
}); | ||
} | ||
module.exports = { | ||
targetVersion : targetVersion, | ||
convert : convert | ||
convertSync : convertSync, | ||
convert : convertSync, // for backwards compatibility | ||
convertObj : convertObj, | ||
convertUrl : convertUrl, | ||
convertStr : convertStr, | ||
convertFile : convertFile | ||
}; |
{ | ||
"name": "swagger2openapi", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Convert Swagger 2.0 specifications to OpenApi 3.0", | ||
@@ -19,2 +19,3 @@ "main": "index.js", | ||
"js-yaml": "^3.6.1", | ||
"node-fetch": "^1.6.3", | ||
"recursive-readdir": "^2.1.1", | ||
@@ -21,0 +22,0 @@ "should": "^11.2.0", |
@@ -8,3 +8,3 @@ # swagger2openapi | ||
[![Tested on APIs.guru](https://api.apis.guru/badges/tested_on.svg)](https://APIs.guru) | ||
[![Tested on Mermade OpenAPIs](https://img.shields.io/badge/Additional%20Specs-1258-brightgreen.svg)](https://github.com/mermade/openapi_specifications) | ||
[![Tested on Mermade OpenAPIs](https://img.shields.io/badge/Additional%20Specs-1258-brightgreen.svg)](https://github.com/mermade/openapi-definitions) | ||
[![Coverage Status](https://coveralls.io/repos/github/Mermade/swagger2openapi/badge.svg?branch=master)](https://coveralls.io/github/Mermade/swagger2openapi?branch=master) | ||
@@ -20,4 +20,6 @@ [![Known Vulnerabilities](https://snyk.io/test/npm/swagger2openapi/badge.svg)](https://snyk.io/test/npm/swagger2openapi) | ||
```` | ||
swagger2openapi [options] [filename|url] | ||
Options: | ||
-d, --debug enable debug mode, adds specification-extensions [boolean] | ||
-e, --encoding encoding for input/output files [string] [default: "utf8"] | ||
-h, --help Show help [boolean] | ||
@@ -35,3 +37,4 @@ -o, --outfile the output file to write to [string] | ||
//options.debug = true; // sets various x-s2o- debugging properties | ||
var openapi = converter.convert(swagger, options); | ||
var openapi = converter.convertSync(swagger, options); | ||
// also available are asynchronous convertObj, convertFile, convertUrl and convertStr functions | ||
```` | ||
@@ -48,7 +51,7 @@ | ||
## Vendor extensions | ||
## Specification extensions | ||
swagger2openapi has support for a limited number of real-world vendor extensions which have a direct bearing on the conversion. All other vendor extensions are left untouched. | ||
swagger2openapi has support for a limited number of real-world specification extensions which have a direct bearing on the conversion. All other specification extensions are left untouched. | ||
Vendor Extension|Vendor|Conversion Performed | ||
Specification Extension|Vendor|Conversion Performed | ||
|---|---|---| | ||
@@ -63,3 +66,3 @@ x-ms-paths|[Microsoft](https://github.com/Azure/autorest/tree/master/docs/extensions)|Treated as an analogue of the `openapi.paths` object | ||
See also [Amazon API Gateway vendor extensions](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions.html) | ||
See also [Amazon API Gateway specification extensions](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions.html) | ||
@@ -66,0 +69,0 @@ It is expected to be able to configure the process of vendor-extension modification using options or a plugin |
@@ -6,2 +6,3 @@ #!/usr/bin/env node | ||
var fs = require('fs'); | ||
var url = require('url'); | ||
@@ -15,2 +16,6 @@ var yaml = require('js-yaml'); | ||
.describe('debug','enable debug mode, adds specification-extensions') | ||
.string('encoding') | ||
.alias('e','encoding') | ||
.default('encoding','utf8') | ||
.describe('encoding','encoding for input/output files') | ||
.help('help') | ||
@@ -31,33 +36,39 @@ .alias('h','help') | ||
var s = fs.readFileSync(argv._[0],'utf8'); | ||
var swagger; | ||
if (argv.yaml) { | ||
swagger = yaml.safeLoad(s); | ||
} | ||
else { | ||
swagger = JSON.parse(s); | ||
} | ||
argv.origin = argv.url; | ||
function process(err, openapi, options) { | ||
if (err) { | ||
console.log(err.message); | ||
return process.exitCode = 1; | ||
} | ||
if (options.yaml && options.outfile && options.outfile.indexOf('.json') > 0) { | ||
options.yaml = false; | ||
} | ||
if (!options.yaml && options.outfile && options.outfile.indexOf('.yaml') > 0) { | ||
options.yaml = true; | ||
} | ||
var openapi = converter.convert(swagger, argv); | ||
var s; | ||
if (options.yaml) { | ||
s = options.debug ? yaml.dump(openapi) : yaml.safeDump(openapi); | ||
} | ||
else { | ||
s = JSON.stringify(openapi, null, 2); | ||
} | ||
if (argv.yaml && argv.outfile && argv.outfile.indexOf('.json') > 0) { | ||
argv.yaml = false; | ||
if (argv.outfile) { | ||
fs.writeFileSync(options.outfile, s, options.encoding||'utf8'); | ||
} | ||
else { | ||
console.log(s); | ||
} | ||
} | ||
if (!argv.yaml && argv.outfile && argv.outfile.indexOf('.yaml') > 0) { | ||
argv.yaml = true; | ||
} | ||
if (argv.yaml) { | ||
s = argv.debug ? yaml.dump(openapi) : yaml.safeDump(openapi); | ||
var source = argv._[0]; | ||
var u = url.parse(source); | ||
if (u.protocol) { | ||
converter.convertUrl(source,argv,process); | ||
} | ||
else { | ||
s = JSON.stringify(openapi, null, 2); | ||
argv.origin = argv.url; | ||
converter.convertFile(source,argv,process); | ||
} | ||
if (argv.outfile) { | ||
fs.writeFileSync(argv.outfile, s, 'utf8'); | ||
} | ||
else { | ||
console.log(s); | ||
} |
@@ -45,3 +45,4 @@ var url = require('url'); | ||
if (header.$ref) { | ||
header = common.resolve(openapi,header.$ref); | ||
should(Object.keys(header).length).be.exactly(1,'Reference object cannot be extended'); | ||
header = common.resolveSync(openapi,header.$ref); | ||
header.should.not.be.exactly(false,'Could not resolve reference'); | ||
@@ -60,3 +61,4 @@ } | ||
if (response.$ref) { | ||
response = common.resolve(openapi,response.$ref); | ||
should(Object.keys(response).length).be.exactly(1,'Reference object cannot be extended'); | ||
response = common.resolveSync(openapi,response.$ref); | ||
response.should.not.be.exactly(false,'Could not resolve reference'); | ||
@@ -75,2 +77,15 @@ } | ||
} | ||
if (response.content) { | ||
for (var ct in response.content) { | ||
var responseType = response.content[ct]; | ||
if (responseType.example) { | ||
responseType.should.not.have.property('examples'); | ||
} | ||
if (responseType.examples) { | ||
responseType.should.not.have.property('example'); | ||
responseType.examples.should.be.an.Array(); | ||
} | ||
} | ||
} | ||
} | ||
@@ -81,3 +96,4 @@ | ||
if (param.$ref) { | ||
param = common.resolve(openapi,param.$ref); | ||
should(Object.keys(param).length).be.exactly(1,'Reference object cannot be extended'); | ||
param = common.resolveSync(openapi,param.$ref); | ||
param.should.not.be.exactly(false,'Could not resolve reference'); | ||
@@ -91,2 +107,3 @@ } | ||
} | ||
if (typeof param.required !== 'undefined') should(param.required).have.type('boolean'); | ||
param.should.not.have.property('items'); | ||
@@ -119,2 +136,8 @@ param.should.not.have.property('collectionFormat'); | ||
} | ||
else if (o == 'summary') { | ||
pathItem.summary.should.have.type('string'); | ||
} | ||
else if (o == 'description') { | ||
pathItem.description.should.have.type('string'); | ||
} | ||
else if (common.httpVerbs.indexOf(o)>=0) { | ||
@@ -125,2 +148,4 @@ op.should.not.have.property('consumes'); | ||
op.responses.should.not.be.empty(); | ||
if (op.summary) op.summary.should.have.type('string'); | ||
if (op.description) op.description.should.have.type('string'); | ||
@@ -127,0 +152,0 @@ contextAppend(options,'responses'); |
Sorry, the diff of this file is not supported yet
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
Network access
Supply chain riskThis module accesses the network.
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
102569
1275
86
7
10
1
+ Addednode-fetch@^1.6.3
+ Addedencoding@0.1.13(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addednode-fetch@1.7.3(transitive)
+ Addedsafer-buffer@2.1.2(transitive)