Skip to content

Commit ded5a66

Browse files
committed
Fallback to reference description for responses
1 parent 1de673f commit ded5a66

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

lib/spec/openapi/utils.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,17 @@ function resolveCommonParams (container, parameters, schema, ref, sharedSchemas,
303303
arr.forEach(swaggerSchema => parameters.push(swaggerSchema))
304304
}
305305

306+
// Keys to references could be different than the name of the $ref,
307+
// for example `def-0`
308+
function findReference (definitions, id) {
309+
const schemaKey = Object.keys(definitions).find(key => {
310+
const schema = definitions[key]
311+
return schema.$id === id
312+
})
313+
314+
return definitions[schemaKey] ?? {}
315+
}
316+
306317
// https://swagger.io/docs/specification/describing-responses/
307318
function resolveResponse (fastifyResponseJson, produces, ref) {
308319
// if the user does not provided an out schema
@@ -313,6 +324,7 @@ function resolveResponse (fastifyResponseJson, produces, ref) {
313324
const responsesContainer = {}
314325

315326
const statusCodes = Object.keys(fastifyResponseJson)
327+
const definitions = ref.definitions().definitions
316328

317329
statusCodes.forEach(statusCode => {
318330
const rawJsonSchema = fastifyResponseJson[statusCode]
@@ -326,8 +338,13 @@ function resolveResponse (fastifyResponseJson, produces, ref) {
326338
statusCode = statusCode.toUpperCase()
327339
}
328340

341+
const referenceJsonSchema = findReference(definitions, rawJsonSchema.$ref)
342+
329343
const response = {
330-
description: resolved[xResponseDescription] || rawJsonSchema.description || 'Default Response'
344+
description: referenceJsonSchema.description ||
345+
resolved[xResponseDescription] ||
346+
rawJsonSchema.description ||
347+
'Default Response'
331348
}
332349

333350
// add headers when there are any.

test/spec/openapi/schema.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,41 @@ test('response: description and x-response-description', async () => {
387387
t.equal(schemaObject.description, description)
388388
t.equal(schemaObject.responseDescription, undefined)
389389
})
390+
391+
test('retrieve the response description from its given $ref schema', async t => {
392+
// Given a /description endpoint that also has a |description| field in its response referenced schema
393+
const fastify = Fastify()
394+
await fastify.addSchema({
395+
$id: 'my-ref',
396+
description,
397+
type: 'string'
398+
})
399+
400+
await fastify.register(fastifySwagger, openapiOption)
401+
fastify.get('/description', {
402+
schema: {
403+
response: {
404+
200: {
405+
$ref: 'my-ref'
406+
}
407+
}
408+
}
409+
}, () => {})
410+
await fastify.ready()
411+
412+
// When the Swagger schema is generated
413+
const swaggerObject = fastify.swagger()
414+
const api = await Swagger.validate(swaggerObject)
415+
416+
const responseObject = api.paths['/description'].get.responses['200']
417+
t.ok(responseObject)
418+
t.equal(responseObject.description, description)
419+
420+
const schemaObject = responseObject.content['application/json'].schema
421+
t.ok(schemaObject)
422+
t.equal(schemaObject.description, description)
423+
t.equal(schemaObject.responseDescription, undefined)
424+
})
390425
})
391426

392427
test('support default=null', async t => {

0 commit comments

Comments
 (0)