-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathswagger-helpers.js
218 lines (203 loc) · 5.99 KB
/
swagger-helpers.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
'use strict';
// Dependencies.
var RecursiveIterator = require('recursive-iterator');
/**
* Adds the tags property to a swagger object.
* @function
* @param {object} conf - Flexible configuration.
*/
function _attachTags(conf) {
var tag = conf.tag;
var swaggerObject = conf.swaggerObject;
var propertyName = conf.propertyName;
// Correct deprecated property.
if (propertyName === 'tag') {
propertyName = 'tags';
}
if (Array.isArray(tag)) {
for (var i = 0; i < tag.length; i = i + 1) {
swaggerObject[propertyName].push(tag[i]);
}
} else {
swaggerObject[propertyName].push(tag);
}
}
/**
* Merges two objects
* @function
* @param {object} obj1 - Object 1
* @param {object} obj2 - Object 2
* @returns {object} Merged Object
*/
function _objectMerge(obj1, obj2) {
var obj3 = {};
for (var attr in obj1) {
if (obj1.hasOwnProperty(attr)) {
obj3[attr] = obj1[attr];
}
}
for (var name in obj2) {
if (obj2.hasOwnProperty(name)) {
obj3[name] = obj2[name];
}
}
return obj3;
}
/**
* Adds necessary swagger schema object properties.
* @see https://goo.gl/Eoagtl
* @function
* @param {object} swaggerObject - The object to receive properties.
* @returns {object} swaggerObject - The updated object.
*/
function swaggerizeObj(swaggerObject) {
swaggerObject.swagger = '2.0';
swaggerObject.paths = swaggerObject.paths || {};
swaggerObject.definitions = swaggerObject.definitions || {};
swaggerObject.responses = swaggerObject.responses || {};
swaggerObject.parameters = swaggerObject.parameters || {};
swaggerObject.securityDefinitions = swaggerObject.securityDefinitions || {};
swaggerObject.tags = swaggerObject.tags || [];
return swaggerObject;
}
/**
* List of deprecated or wrong swagger schema properties in singular.
* @function
* @returns {array} The list of deprecated property names.
*/
function _getSwaggerSchemaWrongProperties() {
return [
'consume',
'produce',
'path',
'tag',
'definition',
'securityDefinition',
'scheme',
'response',
'parameter',
];
}
/**
* Makes a deprecated property plural if necessary.
* @function
* @param {string} propertyName - The swagger property name to check.
* @returns {string} The updated propertyName if neccessary.
*/
function _correctSwaggerKey(propertyName) {
var wrong = _getSwaggerSchemaWrongProperties();
if (wrong.indexOf(propertyName) > 0) {
// Returns the corrected property name.
return propertyName + 's';
}
return propertyName;
}
/**
* Handles swagger propertyName in pathObject context for swaggerObject.
* @function
* @param {object} swaggerObject - The swagger object to update.
* @param {object} pathObject - The input context of an item for swaggerObject.
* @param {string} propertyName - The property to handle.
*/
function _organizeSwaggerProperties(swaggerObject, pathObject, propertyName) {
var simpleProperties = [
'consume',
'consumes',
'produce',
'produces',
'path',
'paths',
'schema',
'schemas',
'securityDefinition',
'securityDefinitions',
'response',
'responses',
'parameter',
'parameters',
'definition',
'definitions',
];
// Common properties.
if (simpleProperties.indexOf(propertyName) !== -1) {
var keyName = _correctSwaggerKey(propertyName);
var definitionNames = Object
.getOwnPropertyNames(pathObject[propertyName]);
for (var k = 0; k < definitionNames.length; k = k + 1) {
var definitionName = definitionNames[k];
swaggerObject[keyName][definitionName] =
pathObject[propertyName][definitionName];
}
// Tags.
} else if (propertyName === 'tag' || propertyName === 'tags') {
var tag = pathObject[propertyName];
_attachTags({
tag: tag,
swaggerObject: swaggerObject,
propertyName: propertyName,
});
// Paths.
} else {
swaggerObject.paths[propertyName] = _objectMerge(
swaggerObject.paths[propertyName], pathObject[propertyName]
);
}
}
/**
* Adds the data in to the swagger object.
* @function
* @param {object} swaggerObject - Swagger object which will be written to
* @param {object[]} data - objects of parsed swagger data from yml or jsDoc
* comments
*/
function addDataToSwaggerObject(swaggerObject, data) {
if (!swaggerObject || !data) {
throw new Error('swaggerObject and data are required!');
}
for (var i = 0; i < data.length; i = i + 1) {
var pathObject = data[i];
var propertyNames = Object.getOwnPropertyNames(pathObject);
// Iterating the properties of the a given pathObject.
for (var j = 0; j < propertyNames.length; j = j + 1) {
var propertyName = propertyNames[j];
// Do what's necessary to organize the end specification.
_organizeSwaggerProperties(swaggerObject, pathObject, propertyName);
}
}
}
/**
* Aggregates a list of wrong properties in problems.
* Searches in object based on a list of wrongSet.
* @param {Array|object} list - a list to iterate
* @param {Array} wrongSet - a list of wrong properties
* @param {Array} problems - aggregate list of found problems
*/
function seekWrong(list, wrongSet, problems) {
var iterator = new RecursiveIterator(list, 0, false);
for (var item = iterator.next(); !item.done; item = iterator.next()) {
if (wrongSet.indexOf(item.value.key) > 0) {
problems.push(item.value.key);
}
}
}
/**
* Returns a list of problematic tags if any.
* @function
* @param {Array} sources - a list of objects to iterate and check
* @returns {Array} problems - a list of problems encountered
*/
function findDeprecated(sources) {
var wrong = _getSwaggerSchemaWrongProperties();
// accumulate problems encountered
var problems = [];
sources.forEach(function(source) {
// Iterate through `source`, search for `wrong`, accumulate in `problems`.
seekWrong(source, wrong, problems);
});
return problems;
}
module.exports = {
addDataToSwaggerObject: addDataToSwaggerObject,
swaggerizeObj: swaggerizeObj,
findDeprecated: findDeprecated,
};