-
Notifications
You must be signed in to change notification settings - Fork 48
/
preprocessor.js
65 lines (59 loc) · 2.29 KB
/
preprocessor.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
var _ = require('lodash')
var httpMethods = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch']
// Preprocessor for the swagger-json, so that some of the logic can be taken out of the
// template
module.exports = function (swaggerJson) {
var copy = _.cloneDeep(swaggerJson)
var tagsByName = _.indexBy(copy.tags, 'name')
copy.tags = copy.tags || []
// The "body"-parameter in each operation is stored in a
// separate field "_request_body".
if (copy.paths) {
Object.keys(copy.paths).forEach(function (pathName) {
var path = copy.paths[pathName]
var pathParameters = path.parameters || []
Object.keys(path).forEach(function (method) {
if (httpMethods.indexOf(method) < 0) {
delete path[method]
return
}
var operation = path[method]
operation.path = pathName
operation.method = method
// Draw links from tags to operations referencing them
var operationTags = operation.tags || ['default']
operationTags.forEach(function (tag) {
if (!tagsByName[tag]) {
// New implicit declaration of tag not defined in global "tags"-object
// https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#user-content-swaggerTags
var tagDefinition = {
name: tag,
operations: []
}
tagsByName[tag] = tagDefinition
copy.tags.push(tagDefinition)
}
if (tagsByName[tag]) {
tagsByName[tag].operations = tagsByName[tag].operations || []
tagsByName[tag].operations.push(operation)
}
})
// Join parameters with path-parameters
operation.parameters = (operation.parameters || [])
.concat(pathParameters)
.filter(function (param) {
if (param.in === 'body') {
operation._request_body = param
return false
}
return true
})
// Show body section, if either a body-parameter or a consumes-property is present.
operation._show_requst_body_section = operation._request_body || operation.consumes
})
})
// If there are multiple tags, we show the tag-based summary
copy.showTagSummary = copy.tags.length > 1
}
return copy
}