Skip to content

Commit

Permalink
fastify#462 Provide more complex test
Browse files Browse the repository at this point in the history
 rewrite test using await/async  to be more readable
  • Loading branch information
briceruzand committed Sep 24, 2021
1 parent c4dd960 commit 2f28ad6
Showing 1 changed file with 56 additions and 37 deletions.
93 changes: 56 additions & 37 deletions test/spec/openapi/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,83 @@ const { test } = require('tap')
const Fastify = require('fastify')
const Swagger = require('swagger-parser')
const fastifySwagger = require('../../../index')
const { openapiOption } = require('../../../examples/options')

test('support $ref schema', (t) => {
t.plan(4)
const openapiOption = {
openapi: {},
refResolver: {
buildLocalReference: (json, baseUri, fragment, i) => {
return json.$id || `def-${i}`
}
}
}

test('support $ref schema', async (t) => {
const fastify = Fastify()

fastify.register(fastifySwagger, openapiOption)
fastify.register(function (instance, _, done) {
fastify.register(async (instance) => {
instance.addSchema({ $id: 'Order', type: 'object', properties: { id: { type: 'integer' } } })
instance.post('/', { schema: { body: { $ref: 'Order#' }, response: { 200: { $ref: 'Order#' } } } }, () => {})
done()
})

fastify.ready((err) => {
t.error(err)
await fastify.ready()

const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
t.match(Object.keys(openapiObject.components.schemas), ['def-0'])
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
t.match(Object.keys(openapiObject.components.schemas), ['Order'])

Swagger.validate(openapiObject)
.then(function (api) {
t.pass('valid swagger object')
})
.catch(function (err) {
t.fail(err)
})
})
await Swagger.validate(openapiObject)
})

test('support nested $ref schema', (t) => {
t.plan(5)
test('support nested $ref schema : simple test', async (t) => {
const fastify = Fastify()

fastify.register(fastifySwagger, openapiOption)
fastify.register(function (instance, _, done) {
fastify.register(async (instance) => {
instance.addSchema({ $id: 'OrderItem', type: 'object', properties: { id: { type: 'integer' } } })
instance.addSchema({ $id: 'ProductItem', type: 'object', properties: { id: { type: 'integer' } } })
instance.addSchema({ $id: 'Order', type: 'object', properties: { products: { type: 'array', items: { $ref: 'OrderItem' } } } })
instance.post('/', { schema: { body: { $ref: 'Order' }, response: { 200: { $ref: 'Order' } } } }, () => {})
done()
instance.post('/other', { schema: { body: { $ref: 'ProductItem' } } }, () => {})
})

fastify.ready((err) => {
t.error(err)
await fastify.ready()

const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')

const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
t.match(Object.keys(openapiObject.components.schemas), ['def-0', 'def-1'])
const schemas = openapiObject.components.schemas
t.match(Object.keys(schemas), ['OrderItem', 'ProductItem', 'Order'])

// OrderItem ref must be prefixed by '#/components/schemas/'
t.equal(openapiObject.components.schemas['def-1'].properties.products.items.$ref, '#/components/schemas/def-0')
// ref must be prefixed by '#/components/schemas/'
t.equal(schemas.Order.properties.products.items.$ref, '#/components/schemas/OrderItem')

Swagger.validate(openapiObject)
.then(function (api) {
t.pass('valid swagger object')
})
.catch(function (err) {
t.fail(err)
})
await Swagger.validate(openapiObject)
})

test('support nested $ref schema : complex case', async (t) => {
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
fastify.register(async (instance) => {
instance.addSchema({ $id: 'schemaA', type: 'object', properties: { id: { type: 'integer' } } })
instance.addSchema({ $id: 'schemaB', type: 'object', properties: { id: { type: 'string' } } })
instance.addSchema({ $id: 'schemaC', type: 'object', properties: { a: { type: 'array', items: { $ref: 'schemaA' } } } })
instance.addSchema({ $id: 'schemaD', type: 'object', properties: { b: { $ref: 'schemaB' }, c: { $ref: 'schemaC' } } })
instance.post('/url1', { schema: { body: { $ref: 'schemaD' }, response: { 200: { $ref: 'schemaB' } } } }, () => {})
instance.post('/url2', { schema: { body: { $ref: 'schemaC' }, response: { 200: { $ref: 'schemaA' } } } }, () => {})
})

await fastify.ready()

const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')

const schemas = openapiObject.components.schemas
t.match(Object.keys(schemas), ['schemaA', 'schemaB', 'schemaC', 'schemaD'])

// ref must be prefixed by '#/components/schemas/'
t.equal(schemas.schemaC.properties.a.items.$ref, '#/components/schemas/schemaA')
t.equal(schemas.schemaD.properties.b.$ref, '#/components/schemas/schemaB')
t.equal(schemas.schemaD.properties.c.$ref, '#/components/schemas/schemaC')

await Swagger.validate(openapiObject)
})

0 comments on commit 2f28ad6

Please sign in to comment.