8000 fix: soft fail fallback response description lookups (#832) · fastify/fastify-swagger@e35d2f6 · GitHub
[go: up one dir, main page]

Skip to content

Commit e35d2f6

Browse files
authored
fix: soft fail fallback response description lookups (#832)
1 parent 36a543c commit e35d2f6

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

lib/util/resolve-schema-reference.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function resolveSchemaReference (rawSchema, ref) {
1010
return undefined
1111
}
1212

13-
return resolvedReference.definitions[schemaId]
13+
return resolvedReference.definitions?.[schemaId]
1414
}
1515

1616
module.exports = {

test/spec/openapi/schema.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,63 @@ test('add default properties for url params when missing schema.params', async t
10221022
})
10231023
})
10241024

1025+
test('support custom transforms which returns $ref in the response', async t => {
1026+
const customObject = {}
1027+
const opt = {
1028+
schema: {
1029+
response: {
1030+
200: customObject
1031+
}
1032+
}
1033+
}
1034+
1035+
const fastify = Fastify()
1036+
await fastify.register(fastifySwagger, {
1037+
openapi: true,
1038+
transform: ({ schema, ...rest }) => {
1039+
schema.response['200'] = {
1040+
$ref: '#/components/schemas/CustomObject'
1041+
}
1042+
return {
1043+
schema,
1044+
...rest
1045+
}
1046+
},
1047+
transformObject: ({ openapiObject }) => {
1048+
openapiObject.components.schemas.CustomObject = {
1049+
type: 'object',
1050+
properties: {
1051+
hello: {
1052+
type: 'string'
1053+
}
1054+
}
1055+
}
1056+
return openapiObject
1057+
}
1058+
})
1059+
fastify.post('/', opt, () => { })
1060+
await fastify.ready()
1061+
1062+
const swaggerObject = fastify.swagger()
1063+
1064+
const swaggerPath = swaggerObject.paths['/'].post
1065+
t.has(swaggerPath.responses['200'].content['application/json'].schema, {
1066+
$ref: '#/components/schemas/CustomObject'
1067+
})
1068+
1069+
// validate seems to mutate the swaggerPath object
1070+
const api = await Swagger.validate(swaggerObject)
1071+
const definedPath = api.paths['/'].post
1072+
t.same(definedPath.responses['200'].content['application/json'].schema, {
1073+
type: 'object',
1074+
properties: {
1075+
hello: {
1076+
type: 'string'
1077+
}
1078+
}
1079+
})
1080+
})
1081+
10251082
test('avoid overwriti 42C1 ng params when schema.params is provided', async t => {
10261083
const opt = {
10271084
schema: {

0 commit comments

Comments
 (0)
0