-
Notifications
You must be signed in to change notification settings - Fork 63
/
swagger-spec-generator.js
141 lines (123 loc) · 4.28 KB
/
swagger-spec-generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict';
/**
* Module dependencies.
*/
var path = require('path');
var _ = require('lodash');
var routeHelper = require('./route-helper');
var modelHelper = require('./model-helper');
var typeConverter = require('./type-converter');
var tagBuilder = require('./tag-builder');
var TypeRegistry = require('./type-registry');
/**
* Create Swagger Object describing the API provided by loopbacApplication.
*
* @param {Application} loopbackApplication The application to document.
* @param {Object} opts Options.
* @returns {Object}
*/
module.exports = function createSwaggerObject(loopbackApplication, opts) {
opts = _.defaults(opts || {}, {
basePath: loopbackApplication.get('restApiRoot') || '/api',
// Default consumes/produces
consumes: [
'application/json',
'application/x-www-form-urlencoded',
'application/xml', 'text/xml'
],
produces: [
'application/json',
'application/xml', 'text/xml',
// JSONP content types
'application/javascript', 'text/javascript'
],
version: getPackagePropertyOrDefault('version', '1.0.0'),
});
// 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();
// Generate fixed fields like info and basePath
var swaggerObject = generateSwaggerObjectBase(opts);
var typeRegistry = new TypeRegistry();
var loopbackRegistry = loopbackApplication.registry ||
loopbackApplication.loopback.registry ||
loopbackApplication.loopback;
var models = loopbackRegistry.modelBuilder.models;
for (var modelName in models) {
modelHelper.registerModelDefinition(models[modelName], typeRegistry);
}
// A class is an endpoint root; e.g. /users, /products, and so on.
// In Swagger 2.0, there is no endpoint roots, but one can group endpoints
// using tags.
classes.forEach(function(aClass) {
if (!aClass.name) return;
var hasDocumentedMethods = aClass.methods().some(function(m) {
return m.documented;
});
if (!hasDocumentedMethods) return;
swaggerObject.tags.push(tagBuilder.buildTagFromClass(aClass));
});
// A route is an endpoint, such as /users/findOne.
routes.forEach(function(route) {
if (!route.documented) return;
// Get the class definition matching this route.
var className = route.method.split('.')[0];
var classDef = classes.filter(function(item) {
return item.name === className;
})[0];
if (!classDef) {
console.error('Route exists with no class: %j', route);
return;
}
routeHelper.addRouteToSwaggerPaths(route, classDef, typeRegistry,
swaggerObject.paths);
});
_.assign(swaggerObject.definitions, typeRegistry.getDefinitions());
loopbackApplication.emit('swaggerResources', swaggerObject);
return swaggerObject;
};
/**
* Generate a top-level resource doc. This is the entry point for swagger UI
* and lists all of the available APIs.
* @param {Object} opts Swagger options.
* @return {Object} Resource doc.
*/
function generateSwaggerObjectBase(opts) {
var apiInfo = _.cloneDeep(opts.apiInfo) || {};
for (var propertyName in apiInfo) {
var property = apiInfo[propertyName];
apiInfo[propertyName] = typeConverter.convertText(property);
}
apiInfo.version = String(apiInfo.version || opts.version);
if (!apiInfo.title) {
apiInfo.title = getPackagePropertyOrDefault('name', 'LoopBack Application');
}
var basePath = opts.basePath;
if (basePath && /\/$/.test(basePath))
basePath = basePath.slice(0, -1);
return {
swagger: '2.0',
// See swagger-spec/2.0.md#infoObject
info: apiInfo,
host: opts.host,
basePath: basePath,
schemes: opts.protocol ? [opts.protocol] : undefined,
consumes: opts.consumes,
produces: opts.produces,
paths: {},
definitions: opts.models || {},
// TODO Authorizations (security, securityDefinitions)
// TODO: responses, externalDocs
tags: []
};
}
function getPackagePropertyOrDefault(name, defautValue) {
try {
var pkg = require(path.join(process.cwd(), 'package.json'));
return pkg[name] || defautValue;
} catch(e) {
return defautValue;
}
}