Skip to content

Commit 878d379

Browse files
committed
Use dynamic references when resolving schemas
1 parent f882c7c commit 878d379

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

lib/spec/openapi/utils.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,19 @@ function resolveCommonParams (container, parameters, schema, ref, sharedSchemas,
305305

306306
// Keys to references could be different than the name of the $ref,
307307
// 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-
})
308+
function findReferenceDescription (rawSchema, ref) {
309+
// FIXME: Resolve directly?
310+
const resolvedReference = ref.resolve(rawSchema, { externalSchemas: [ref.definitions().definitions] })
311+
312+
// Ref has format `#/definitions/id`
313+
const schemaId = resolvedReference?.$ref?.split('/')[2]
313314

314-
return definitions[schemaKey] ?? {}
315+
if (schemaId === undefined) {
316+
return undefined
317+
}
318+
319+
const schema = resolvedReference.definitions[schemaId]
320+
return schema?.description
315321
}
316322

317323
// https://swagger.io/docs/specification/describing-responses/
@@ -324,7 +330,6 @@ function resolveResponse (fastifyResponseJson, produces, ref) {
324330
const responsesContainer = {}
325331

326332
const statusCodes = Object.keys(fastifyResponseJson)
327-
const definitions = ref.definitions().definitions
328333

329334
statusCodes.forEach(statusCode => {
330335
const rawJsonSchema = fastifyResponseJson[statusCode]
@@ -338,12 +343,10 @@ function resolveResponse (fastifyResponseJson, produces, ref) {
338343
statusCode = statusCode.toUpperCase()
339344
}
340345

341-
const referenceJsonSchema = findReference(definitions, rawJsonSchema.$ref)
342-
343346
const response = {
344347
description: resolved[xResponseDescription] ||
345348
rawJsonSchema.description ||
346-
referenceJsonSchema.description ||
349+
findReferenceDescription(rawJsonSchema, ref) ||
347350
'Default Response'
348351
}
349352

test/spec/openapi/schema.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ test('response: description and x-response-description', async () => {
391391
test('retrieve the response description from its given $ref schema', async t => {
392392
// Given a /description endpoint that also has a |description| field in its response referenced schema
393393
const fastify = Fastify()
394-
await fastify.addSchema({
394+
fastify.addSchema({
395395
$id: 'my-ref',
396396
description,
397397
type: 'string'
@@ -402,7 +402,7 @@ test('response: description and x-response-description', async () => {
402402
schema: {
403403
response: {
404404
200: {
405-
$ref: 'my-ref'
405+
$ref: 'my-ref#'
406406
}
407407
}
408408
}

0 commit comments

Comments
 (0)