forked from fastify/fastify-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic.js
396 lines (329 loc) · 11.6 KB
/
dynamic.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
'use strict'
const fs = require('fs')
const path = require('path')
const yaml = require('js-yaml')
const Ref = require('json-schema-resolver')
module.exports = function (fastify, opts, next) {
fastify.decorate('swagger', swagger)
const routes = []
const sharedSchemasMap = new Map()
let ref
fastify.addHook('onRoute', (routeOptions) => {
routes.push(routeOptions)
})
fastify.addHook('onRegister', async (instance) => {
// we need to wait the ready event to get all the .getSchemas()
// otherwise it will be empty
instance.addHook('onReady', (done) => {
const allSchemas = instance.getSchemas()
for (const schemaId of Object.keys(allSchemas)) {
if (!sharedSchemasMap.has(schemaId)) {
sharedSchemasMap.set(schemaId, allSchemas[schemaId])
}
}
done()
})
})
opts = opts || {}
opts.swagger = opts.swagger || {}
const info = opts.swagger.info || null
const host = opts.swagger.host || null
const schemes = opts.swagger.schemes || null
const consumes = opts.swagger.consumes || null
const produces = opts.swagger.produces || null
const definitions = opts.swagger.definitions || null
const basePath = opts.swagger.basePath || null
const securityDefinitions = opts.swagger.securityDefinitions || null
const security = opts.swagger.security || null
const tags = opts.swagger.tags || null
const externalDocs = opts.swagger.externalDocs || null
const transform = opts.transform || null
const hiddenTag = opts.hiddenTag || 'X-HIDDEN'
if (opts.exposeRoute === true) {
const prefix = opts.routePrefix || '/documentation'
fastify.register(require('./routes'), { prefix })
}
const cache = {
swaggerObject: null,
swaggerString: null
}
function swagger (opts) {
if (opts && opts.yaml) {
if (cache.swaggerString) return cache.swaggerString
} else {
if (cache.swaggerObject) return cache.swaggerObject
}
const swaggerObject = {}
var pkg
try {
pkg = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json')))
} catch (err) {
return next(err)
}
// Base swagger info
// this info is displayed in the swagger file
// in the same order as here
swaggerObject.swagger = '2.0'
if (info) {
swaggerObject.info = info
} else {
swaggerObject.info = {
version: '1.0.0',
title: pkg.name || ''
}
}
if (host) swaggerObject.host = host
if (schemes) swaggerObject.schemes = schemes
if (basePath) swaggerObject.basePath = basePath
if (consumes) swaggerObject.consumes = consumes
if (produces) swaggerObject.produces = produces
if (definitions) swaggerObject.definitions = definitions
else swaggerObject.definitions = {}
if (securityDefinitions) {
swaggerObject.securityDefinitions = securityDefinitions
}
if (security) {
swaggerObject.security = security
}
if (tags) {
swaggerObject.tags = tags
}
if (externalDocs) {
swaggerObject.externalDocs = externalDocs
}
const externalSchemas = Array.from(sharedSchemasMap.values())
ref = Ref({ clone: true, applicationUri: 'todo.com', externalSchemas })
swaggerObject.definitions = {
...swaggerObject.definitions,
...(ref.definitions().definitions)
}
// Swagger doesn't accept $id on /definitions schemas.
// The $ids are needed by Ref() to check the URI so we need
// to remove them at the end of the process
Object.values(swaggerObject.definitions)
.forEach(_ => { delete _.$id })
swaggerObject.paths = {}
for (var route of routes) {
if (route.schema && route.schema.hide) {
continue
}
if (route.schema && route.schema.tags && route.schema.tags.includes(hiddenTag)) {
continue
}
const schema = transform
? transform(route.schema)
: route.schema
let path = route.url.startsWith(basePath)
? route.url.replace(basePath, '')
: route.url
if (!path.startsWith('/')) {
path = '/' + path
}
const url = formatParamUrl(path)
const swaggerRoute = swaggerObject.paths[url] || {}
const swaggerMethod = {}
const parameters = []
// route.method should be either a String, like 'POST', or an Array of Strings, like ['POST','PUT','PATCH']
const methods = typeof route.method === 'string' ? [route.method] : route.method
for (var method of methods) {
swaggerRoute[method.toLowerCase()] = swaggerMethod
}
// All the data the user can give us, is via the schema object
if (schema) {
// the resulting schema will be in this order
if (schema.operationId) {
swaggerMethod.operationId = schema.operationId
}
if (schema.summary) {
swaggerMethod.summary = schema.summary
}
if (schema.description) {
swaggerMethod.description = schema.description
}
if (schema.tags) {
swaggerMethod.tags = schema.tags
}
if (schema.produces) {
swaggerMethod.produces = schema.produces
}
if (schema.consumes) {
swaggerMethod.consumes = schema.consumes
}
if (schema.querystring) {
getQueryParams(parameters, schema.querystring)
}
if (schema.body) {
const consumesAllFormOnly =
consumesFormOnly(schema) || consumesFormOnly(swaggerObject)
consumesAllFormOnly
? getFormParams(parameters, schema.body)
: getBodyParams(parameters, schema.body)
}
if (schema.params) {
getPathParams(parameters, schema.params)
}
if (schema.headers) {
getHeaderParams(parameters, schema.headers)
}
if (parameters.length) {
swaggerMethod.parameters = parameters
}
if (schema.deprecated) {
swaggerMethod.deprecated = schema.deprecated
}
if (schema.security) {
swaggerMethod.security = schema.security
}
for (const key of Object.keys(schema)) {
if (key.startsWith('x-')) {
swaggerMethod[key] = schema[key]
}
}
}
swaggerMethod.responses = genResponse(schema ? schema.response : null)
swaggerObject.paths[url] = swaggerRoute
}
if (opts && opts.yaml) {
const swaggerString = yaml.safeDump(swaggerObject, { skipInvalid: true })
cache.swaggerString = swaggerString
return swaggerString
}
cache.swaggerObject = swaggerObject
return swaggerObject
function getBodyParams (parameters, body) {
const bodyResolved = ref.resolve(body)
const param = {}
param.name = 'body'
param.in = 'body'
param.schema = bodyResolved
parameters.push(param)
}
function getFormParams (parameters, form) {
const resolved = ref.resolve(form)
const add = plainJsonObjectToSwagger2('formData', resolved, swaggerObject.definitions)
add.forEach(_ => parameters.push(_))
}
function getQueryParams (parameters, query) {
const resolved = ref.resolve(query)
const add = plainJsonObjectToSwagger2('query', resolved, swaggerObject.definitions)
add.forEach(_ => parameters.push(_))
}
function getPathParams (parameters, path) {
const resolved = ref.resolve(path)
const add = plainJsonObjectToSwagger2('path', resolved, swaggerObject.definitions)
add.forEach(_ => parameters.push(_))
}
function getHeaderParams (parameters, headers) {
const resolved = ref.resolve(headers)
const add = plainJsonObjectToSwagger2('header', resolved, swaggerObject.definitions)
add.forEach(_ => parameters.push(_))
}
// https://swagger.io/docs/specification/2-0/describing-responses/
function genResponse (fastifyResponseJson) {
// if the user does not provided an out schema
if (!fastifyResponseJson) {
return { 200: { description: 'Default Response' } }
}
const responsesContainer = {}
Object.keys(fastifyResponseJson).forEach(key => {
// 2xx is not supported by swagger
const rawJsonSchema = fastifyResponseJson[key]
const resolved = ref.resolve(rawJsonSchema)
responsesContainer[key] = {
schema: resolved,
description: 'Default Response'
}
})
return responsesContainer
}
}
next()
}
function consumesFormOnly (schema) {
const consumes = schema.consumes
return (
consumes &&
consumes.length === 1 &&
(consumes[0] === 'application/x-www-form-urlencoded' ||
consumes[0] === 'multipart/form-data')
)
}
// The swagger standard does not accept the url param with ':'
// so '/user/:id' is not valid.
// This function converts the url in a swagger compliant url string
// => '/user/{id}'
function formatParamUrl (url) {
var start = url.indexOf('/:')
if (start === -1) return url
var end = url.indexOf('/', ++start)
if (end === -1) {
return url.slice(0, start) + '{' + url.slice(++start) + '}'
} else {
return formatParamUrl(url.slice(0, start) + '{' + url.slice(++start, end) + '}' + url.slice(end))
}
}
// For supported keys read:
// https://swagger.io/docs/specification/2-0/describing-parameters/
function plainJsonObjectToSwagger2 (container, jsonSchema, externalSchemas) {
const obj = localRefResolve(jsonSchema, externalSchemas)
let toSwaggerProp
switch (container) {
case 'query':
toSwaggerProp = function (properyName, jsonSchemaElement) {
jsonSchemaElement.in = container
jsonSchemaElement.name = properyName
return jsonSchemaElement
}
break
case 'formData':
toSwaggerProp = function (properyName, jsonSchemaElement) {
delete jsonSchemaElement.$id
jsonSchemaElement.in = container
jsonSchemaElement.name = properyName
// https://json-schema.org/understanding-json-schema/reference/non_json_data.html#contentencoding
if (jsonSchemaElement.contentEncoding === 'binary') {
delete jsonSchemaElement.contentEncoding // Must be removed
jsonSchemaElement.type = 'file'
}
return jsonSchemaElement
}
break
case 'path':
toSwaggerProp = function (properyName, jsonSchemaElement) {
jsonSchemaElement.in = container
jsonSchemaElement.name = properyName
jsonSchemaElement.required = true
return jsonSchemaElement
}
break
case 'header':
toSwaggerProp = function (properyName, jsonSchemaElement) {
return {
in: 'header',
name: properyName,
required: jsonSchemaElement.required,
description: jsonSchemaElement.description,
type: jsonSchemaElement.type
}
}
break
}
return Object.keys(obj).reduce((acc, propKey) => {
acc.push(toSwaggerProp(propKey, obj[propKey]))
return acc
}, [])
}
function localRefResolve (jsonSchema, externalSchemas) {
if (jsonSchema.type && jsonSchema.properties) {
// for the shorthand querystring/params/headers declaration
const propertiesMap = Object.keys(jsonSchema.properties).reduce((acc, h) => {
const required = (jsonSchema.required && jsonSchema.required.indexOf(h) >= 0) || false
const newProps = Object.assign({}, jsonSchema.properties[h], { required })
return Object.assign({}, acc, { [h]: newProps })
}, {})
return propertiesMap
}
// $ref is in the format: #/definitions/<resolved definition>/<optional fragment>
const localReference = jsonSchema.$ref.split('/')[2]
return localRefResolve(externalSchemas[localReference], externalSchemas)
}