-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathgetSpecificationObject.js
71 lines (58 loc) · 2.08 KB
/
getSpecificationObject.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
const parser = require('swagger-parser');
const createSpecification = require('./createSpecification');
const specHelper = require('./specification');
const parseApiFile = require('./parseApiFile');
const filterJsDocComments = require('./filterJsDocComments');
const convertGlobPaths = require('./convertGlobPaths');
function isEmptyObject(obj) {
// eslint-disable-next-line
Object.keys(obj).forEach(key => {
if (key in obj) return false;
});
return true;
}
/**
* OpenAPI specification validator does not accept empty values for a few properties.
* Solves validator error: "Schema error should NOT have additional properties"
* @function
* @param {object} inputSpec - The swagger/openapi specification
* @param {object} improvedSpec - The cleaned version of the inputSpec
*/
function cleanUselessProperties(inputSpec) {
const improvedSpec = JSON.parse(JSON.stringify(inputSpec));
const toClean = [
'definitions',
'responses',
'parameters',
'securityDefinitions',
];
toClean.forEach(unncessaryProp => {
if (isEmptyObject(improvedSpec[unncessaryProp])) {
delete improvedSpec[unncessaryProp];
}
});
return improvedSpec;
}
function getSpecificationObject(options) {
// Get input definition and prepare the specification's skeleton
const definition = options.swaggerDefinition || options.definition;
let specification = createSpecification(definition);
// Parse the documentation containing information about APIs.
const apiPaths = convertGlobPaths(options.apis);
for (let i = 0; i < apiPaths.length; i += 1) {
const files = parseApiFile(apiPaths[i], options.jsDocFilter);
const swaggerJsDocComments = filterJsDocComments(files.jsdoc);
specHelper.addDataToSwaggerObject(specification, files.yaml);
specHelper.addDataToSwaggerObject(specification, swaggerJsDocComments);
}
parser.parse(specification, (err, api) => {
if (!err) {
specification = api;
}
});
if (specification.openapi) {
specification = cleanUselessProperties(specification);
}
return specification;
}
module.exports = getSpecificationObject;