Skip to content

Commit fedf75c

Browse files
authored
Resolve examples for shared schemas (#786)
* resolve examples for shared schemas * resolve examples for shared schemas
1 parent 9bcaed4 commit fedf75c

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/spec/openapi/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ function prepareOpenapiSchemas (schemas, ref) {
470470
.reduce((res, [name, schema]) => {
471471
const _ = { ...schema }
472472
const resolved = transformDefsToComponents(ref.resolve(_, { externalSchemas: [schemas] }))
473+
resolveSchemaExamplesRecursive(resolved)
473474

474475
// Swagger doesn't accept $id on /definitions schemas.
475476
// The $ids are needed by Ref() to check the URI so we need

test/spec/openapi/refs.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test('support $ref schema', async (t) => {
1919

2020
await fastify.register(fastifySwagger, openapiOption)
2121
fastify.register(async (instance) => {
22-
instance.addSchema({ $id: 'Order', type: 'object', properties: { id: { type: 'integer' } } })
22+
instance.addSchema({ $id: 'Order', type: 'object', properties: { id: { type: 'integer', examples: [25] } } })
2323
instance.post('/', { schema: { body: { $ref: 'Order#' }, response: { 200: { $ref: 'Order#' } } } }, () => {})
2424
})
2525

@@ -28,6 +28,7 @@ test('support $ref schema', async (t) => {
2828
const openapiObject = fastify.swagger()
2929
t.equal(typeof openapiObject, 'object')
3030
t.match(Object.keys(openapiObject.components.schemas), ['Order'])
31+
t.equal(openapiObject.components.schemas.Order.properties.id.example, 25)
3132

3233
await Swagger.validate(openapiObject)
3334
})
@@ -67,7 +68,7 @@ test('support nested $ref schema : simple test', async (t) => {
6768
const fastify = Fastify()
6869
await fastify.register(fastifySwagger, openapiOption)
6970
fastify.register(async (instance) => {
70-
instance.addSchema({ $id: 'OrderItem', type: 'object', properties: { id: { type: 'integer' } } })
71+
instance.addSchema({ $id: 'OrderItem', type: 'object', properties: { id: { type: 'integer' } }, examples: [{ id: 1 }] })
7172
instance.addSchema({ $id: 'ProductItem', type: 'object', properties: { id: { type: 'integer' } } })
7273
instance.addSchema({ $id: 'Order', type: 'object', properties: { products: { type: 'array', items: { $ref: 'OrderItem' } } } })
7374
instance.post('/', { schema: { body: { $ref: 'Order' }, response: { 200: { $ref: 'Order' } } } }, () => {})
@@ -84,6 +85,7 @@ test('support nested $ref schema : simple test', async (t) => {
8485

8586
// ref must be prefixed by '#/components/schemas/'
8687
t.equal(schemas.Order.properties.products.items.$ref, '#/components/schemas/OrderItem')
88+
t.match(schemas.OrderItem.example, { id: 1 })
8789

8890
await Swagger.validate(openapiObject)
8991
})
@@ -93,7 +95,7 @@ test('support nested $ref schema : complex case', async (t) => {
9395
await fastify.register(fastifySwagger, openapiOption)
9496
fastify.register(async (instance) => {
9597
instance.addSchema({ $id: 'schemaA', type: 'object', properties: { id: { type: 'integer' } } })
96-
instance.addSchema({ $id: 'schemaB', type: 'object', properties: { id: { type: 'string' } } })
98+
instance.addSchema({ $id: 'schemaB', type: 'object', properties: { id: { type: 'string', examples: ['ABC'] } } })
9799
instance.addSchema({ $id: 'schemaC', type: 'object', properties: { a: { type: 'array', items: { $ref: 'schemaA' } } } })
98100
instance.addSchema({ $id: 'schemaD', type: 'object', properties: { b: { $ref: 'schemaB' }, c: { $ref: 'schemaC' } } })
99101
instance.post('/url1', { schema: { body: { $ref: 'schemaD' }, response: { 200: { $ref: 'schemaB' } } } }, () => {})
@@ -112,6 +114,7 @@ test('support nested $ref schema : complex case', async (t) => {
112114
t.equal(schemas.schemaC.properties.a.items.$ref, '#/components/schemas/schemaA')
113115
t.equal(schemas.schemaD.properties.b.$ref, '#/components/schemas/schemaB')
114116
t.equal(schemas.schemaD.properties.c.$ref, '#/components/schemas/schemaC')
117+
t.equal(schemas.schemaB.properties.id.example, 'ABC')
115118

116119
await Swagger.validate(openapiObject)
117120
})

0 commit comments

Comments
 (0)