Enable transformation of swagger 1.2 to 2.0

This commit is contained in:
Raymond Feng 2015-07-28 16:11:36 -07:00
parent 8f2dc9966e
commit 74ac13dfe3
2 changed files with 25 additions and 13 deletions

View File

@ -16,6 +16,9 @@ var _cloneDeep = require('lodash').cloneDeep;
var modelHelper = require('./model-helper');
var cors = require('cors');
var typeConverter = require('./type-converter');
var swaggerConverter = require('swagger-converter');
var mainModule = require.main;
var pkg = require('pkginfo').read(mainModule).package;
/**
* Create a remotable Swagger module for plugging into `RemoteObjects`.
@ -33,6 +36,7 @@ function Swagger(loopbackApplication, swaggerApp, opts) {
swaggerVersion: '1.2',
basePath: loopbackApplication.get('restApiRoot') || '/api',
resourcePath: 'resources',
v2resourcePath: 'resources/swagger2',
// Default consumes/produces
consumes: [
'application/json',
@ -51,11 +55,12 @@ function Swagger(loopbackApplication, swaggerApp, opts) {
// We need a temporary REST adapter to discover our available routes.
var remotes = loopbackApplication.remotes();
var adapter = remotes.handler('rest').adapter;
var routes = adapter.allRoutes();
var classes = remotes.classes();
setupCors(swaggerApp, remotes);
var routes = adapter.allRoutes();
var classes = remotes.classes();
// These are the docs we will be sending from the /swagger endpoints.
var resourceDoc = generateResourceDoc(opts);
var apiDocs = {};
@ -150,8 +155,15 @@ function Swagger(loopbackApplication, swaggerApp, opts) {
* information about them.
*/
addRoute(swaggerApp, opts.resourcePath, resourceDoc, opts);
loopbackApplication.emit('swaggerResources', resourceDoc);
var apiDocList = [];
for (var i in apiDocs) {
apiDocList.push(apiDocs[i]);
}
var v2doc = swaggerConverter(resourceDoc, apiDocList);
addRoute(swaggerApp, opts.v2resourcePath, v2doc, opts, '2.0');
loopbackApplication.emit('swaggerResourcesV2', v2doc);
}
function setupCors(swaggerApp, remotes) {
@ -167,7 +179,7 @@ function setupCors(swaggerApp, remotes) {
* @param {String} uri Path from which to serve the doc.
* @param {Object} doc Doc to serve.
*/
function addRoute(app, uri, doc, opts) {
function addRoute(app, uri, doc, opts, version) {
var hasBasePath = Object.keys(doc).indexOf('basePath') !== -1;
var initialPath = doc.basePath || '';
@ -188,7 +200,7 @@ function addRoute(app, uri, doc, opts) {
// `X-Forwarded-Host` HTTP headers as the `basePath`.
// Because we pre-build the Swagger data, we don't know that header at
// the time the data is built.
if (hasBasePath) {
if (hasBasePath && version !== '2.0') {
var headers = req.headers;
// NOTE header names (keys) are always all-lowercase
var proto = headers['x-forwarded-proto'] || opts.protocol || req.protocol;
@ -196,6 +208,10 @@ function addRoute(app, uri, doc, opts) {
var host = headers['x-forwarded-host'] || headers.host;
doc.basePath = prefix + host + initialPath;
}
if (version === '2.0') {
doc.host = req.hostname;
doc.info.title = 'REST APIs for ' + pkg.name;
}
res.status(200).send(doc);
});
}
@ -232,11 +248,5 @@ function generateResourceDoc(opts) {
* @return {String} API Version.
*/
function getVersion() {
var version;
try {
version = require(path.join(process.cwd(), 'package.json')).version;
} catch(e) {
version = '1.0.0';
}
return version;
return pkg.version || '1.0.0';
}

View File

@ -35,6 +35,8 @@
"debug": "^2.2.0",
"express": "4.x",
"lodash": "^3.10.0",
"strong-swagger-ui": "^20.0.2"
"strong-swagger-ui": "^20.0.2",
"pkginfo": "^0.3.0",
"swagger-converter": "^0.1.7"
}
}