Skip to content

Commit

Permalink
add transitive conversion to Open API 3.0 via Swagger 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rbren committed Oct 30, 2017
1 parent 7328305 commit 11174a3
Show file tree
Hide file tree
Showing 25 changed files with 20,311 additions and 681 deletions.
48 changes: 48 additions & 0 deletions dist/api-spec-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ BaseFormat.prototype.convertTo = function (format, callback) {
}).asCallback(callback);
};

BaseFormat.prototype.convertTransitive = function (intermediaries) {
var prom = Promise.resolve(this);
intermediaries.forEach(function (intermediary) {
prom = prom.then(function (spec) {
return spec.convertTo(intermediary);
});
});
return prom.then(function (spec) {
return spec.spec;
});
};

},{"./formats.js":2,"./util.js":11,"bluebird":82,"composite-error":127,"deep-sort-object":139,"js-yaml":1229,"lodash":1321}],2:[function(require,module,exports){
'use strict';

Expand All @@ -187,12 +199,18 @@ var BPToSwagger = require('apib2swagger');
var BaseFormat = require('../base_format.js');

var APIBlueprint = module.exports = function () {
var _this = this;

APIBlueprint.super_.apply(this, arguments);
this.format = 'api_blueprint';

this.converters.swagger_2 = Promise.method(function (apibp) {
return BPToSwagger.convertParsed(apibp.spec);
});

this.converters.openapi_3 = Promise.method(function (apibp) {
return _this.convertTransitive(['swagger_2', 'openapi_3']);
});
};

Inherits(APIBlueprint, BaseFormat);
Expand Down Expand Up @@ -225,12 +243,18 @@ var Util = require('../util.js');
var Google2Swagger = require('google-discovery-to-swagger');

var Google = module.exports = function () {
var _this = this;

Google.super_.apply(this, arguments);
this.format = 'google';

this.converters.swagger_2 = Promise.method(function (google) {
return Google2Swagger.convert(google.spec);
});

this.converters.openapi_3 = Promise.method(function (google) {
return _this.convertTransitive(['swagger_2', 'openapi_3']);
});
};

Inherits(Google, BaseFormat);
Expand Down Expand Up @@ -262,11 +286,17 @@ var BaseFormat = require('../base_format.js');
var Util = require('../util.js');

var IODocs = module.exports = function () {
var _this = this;

IODocs.super_.apply(this, arguments);
this.type = 'io_docs';
this.converters.swagger_2 = Promise.method(function (iodocs) {
return convertToSwagger(iodocs.spec);
});

this.converters.openapi_3 = Promise.method(function (iodocs) {
return _this.convertTransitive(['swagger_2', 'openapi_3']);
});
};

Inherits(IODocs, BaseFormat);
Expand Down Expand Up @@ -394,12 +424,18 @@ var BaseFormat = require('../base_format.js');
var Util = require('../util.js');

var Raml = module.exports = function () {
var _this = this;

Raml.super_.apply(this, arguments);
this.format = 'raml';

this.converters.swagger_2 = Promise.method(function (raml) {
return Raml2Swagger.convert(raml.spec);
});

this.converters.openapi_3 = Promise.method(function (raml) {
return _this.convertTransitive(['swagger_2', 'openapi_3']);
});
};

Inherits(Raml, BaseFormat);
Expand Down Expand Up @@ -439,6 +475,8 @@ var BaseFormat = require('../base_format.js');
var Util = require('../util.js');

var Swagger1 = module.exports = function () {
var _this = this;

Swagger1.super_.apply(this, arguments);
this.format = 'swagger_1';

Expand All @@ -448,6 +486,10 @@ var Swagger1 = module.exports = function () {
if (swagger2.info.title === 'Title was not specified') swagger2.info.title = swagger2.host;
return swagger2;
});

this.converters.openapi_3 = Promise.method(function (swagger1) {
return _this.convertTransitive(['swagger_2', 'openapi_3']);
});
};

Inherits(Swagger1, BaseFormat);
Expand Down Expand Up @@ -584,12 +626,18 @@ var BaseFormat = require('../base_format.js');
var Util = require('../util.js');

var WADL = module.exports = function () {
var _this = this;

WADL.super_.apply(this, arguments);
this.format = 'wadl';

this.converters.swagger_2 = Promise.method(function (wadl) {
return convertToSwagger(wadl.spec);
});

this.converters.openapi_3 = Promise.method(function (swagger1) {
return _this.convertTransitive(['swagger_2', 'openapi_3']);
});
};

Inherits(WADL, BaseFormat);
Expand Down
10 changes: 10 additions & 0 deletions lib/base_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,13 @@ BaseFormat.prototype.convertTo = function(format, callback) {
});
}).asCallback(callback);
}

BaseFormat.prototype.convertTransitive = function(intermediaries) {
let prom = Promise.resolve(this);
intermediaries.forEach(intermediary => {
prom = prom.then(spec => {
return spec.convertTo(intermediary);
})
});
return prom.then(spec => spec.spec);
}
3 changes: 3 additions & 0 deletions lib/formats/api_blueprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ var APIBlueprint = module.exports = function() {
this.converters.swagger_2 = Promise.method(apibp => {
return BPToSwagger.convertParsed(apibp.spec);
});

this.converters.openapi_3 =
Promise.method(apibp => this.convertTransitive(['swagger_2', 'openapi_3']));
}

Inherits(APIBlueprint, BaseFormat);
Expand Down
3 changes: 3 additions & 0 deletions lib/formats/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var Google = module.exports = function() {

this.converters.swagger_2 =
Promise.method(google => Google2Swagger.convert(google.spec));

this.converters.openapi_3 =
Promise.method(google => this.convertTransitive(['swagger_2', 'openapi_3']));
}

Inherits(Google, BaseFormat);
Expand Down
3 changes: 3 additions & 0 deletions lib/formats/io_docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ var IODocs = module.exports = function() {
this.type = 'io_docs';
this.converters.swagger_2 =
Promise.method(iodocs => convertToSwagger(iodocs.spec));

this.converters.openapi_3 =
Promise.method(iodocs => this.convertTransitive(['swagger_2', 'openapi_3']));
}

Inherits(IODocs, BaseFormat);
Expand Down
3 changes: 3 additions & 0 deletions lib/formats/raml.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ var Raml = module.exports = function() {

this.converters.swagger_2 =
Promise.method(raml => Raml2Swagger.convert(raml.spec));

this.converters.openapi_3 =
Promise.method(raml => this.convertTransitive(['swagger_2', 'openapi_3']));
}

Inherits(Raml, BaseFormat);
Expand Down
3 changes: 3 additions & 0 deletions lib/formats/swagger_1.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var Swagger1 = module.exports = function() {
swagger2.info.title = swagger2.host;
return swagger2;
});

this.converters.openapi_3 =
Promise.method(swagger1 => this.convertTransitive(['swagger_2', 'openapi_3']));
}

Inherits(Swagger1, BaseFormat);
Expand Down
3 changes: 3 additions & 0 deletions lib/formats/wadl.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ var WADL = module.exports = function() {

this.converters.swagger_2 =
Promise.method(wadl => convertToSwagger(wadl.spec));

this.converters.openapi_3 =
Promise.method(swagger1 => this.convertTransitive(['swagger_2', 'openapi_3']));
}

Inherits(WADL, BaseFormat);
Expand Down
2 changes: 1 addition & 1 deletion test/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('Converter', function() {
var outfile = getFileName('output', testCase.out);
var order = testCase.out.order || 'alpha';
if (WRITE_GOLDEN)
FS.writeFileSync(outfile, spec.stringify({order}) + '\n');
FS.writeFileSync(outfile, spec.stringify({order: order}) + '\n');

getFile(outfile, function(err, golden) {
try {
Expand Down
118 changes: 118 additions & 0 deletions test/output/openapi_3/OpenStack_example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"components": {
"schemas": {}
},
"info": {
"title": "",
"version": ""
},
"openapi": "3.0.0",
"paths": {
"/": {
"get": {
"operationId": "listVersions-v2",
"responses": {
"200": {
"description": "Successful Response"
}
}
},
"parameters": []
},
"/v2.0": {
"get": {
"operationId": "showVersionInfo-v2.0",
"responses": {
"200": {
"description": "Successful Response"
}
}
},
"parameters": []
},
"/v2.0/extensions": {
"get": {
"operationId": "listExtensions-v2",
"responses": {
"200": {
"description": "Successful Response"
}
}
},
"parameters": []
},
"/v2.0/extensions/{alias}": {
"get": {
"operationId": "showExtension-v2",
"responses": {
"200": {
"description": "Successful Response"
}
}
},
"parameters": [
{
"in": "path",
"name": "alias",
"required": true,
"schema": {
"type": "string"
}
}
]
},
"/v2.0/tenants": {
"get": {
"operationId": "listTenants",
"responses": {
"200": {
"description": "Successful Response"
}
}
},
"parameters": [
{
"in": "header",
"name": "X-Auth-Token",
"required": true,
"schema": {
"type": "string"
}
},
{
"in": "query",
"name": "limit",
"required": false,
"schema": {
"format": "int32",
"type": "integer"
}
},
{
"in": "query",
"name": "marker",
"required": false,
"schema": {
"type": "string"
}
}
]
},
"/v2.0/tokens": {
"parameters": [],
"post": {
"operationId": "authenticate-v2.0",
"responses": {
"200": {
"description": "Successful Response"
}
}
}
}
},
"servers": [
{
"url": "http://localhost:35357/"
}
]
}
Loading

0 comments on commit 11174a3

Please sign in to comment.