Skip to content

Commit

Permalink
fix: openapi/swagger options path (#768)
Browse files Browse the repository at this point in the history
* fix(spec/openapi): 🐛 add opts.paths in prepareOpenapiObject return

* fix(spec/swagger): 🐛 add opts.paths in prepareDefaultOptions/prepareSwaggerObject and remove swaggerObject.paths empty object assignment
  • Loading branch information
gmatheus authored Nov 27, 2023
1 parent 1b53e37 commit 5277980
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function prepareOpenapiObject (opts) {
if (opts.info) openapiObject.info = opts.info
if (opts.servers) openapiObject.servers = opts.servers
if (opts.components) openapiObject.components = Object.assign({}, opts.components, { schemas: Object.assign({}, opts.components.schemas) })
if (opts.paths) openapiObject.paths = opts.paths
if (opts.webhooks) openapiObject.webhooks = opts.webhooks
if (opts.security) openapiObject.security = opts.security
if (opts.tags) openapiObject.tags = opts.tags
Expand Down
1 change: 0 additions & 1 deletion lib/spec/swagger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ module.exports = function (opts, cache, routes, Ref, done) {
...(ref.definitions().definitions)
}, ref)

swaggerObject.paths = {}
for (const route of routes) {
const transformResult = route.config?.swaggerTransform !== undefined
? route.config.swaggerTransform
Expand Down
3 changes: 3 additions & 0 deletions lib/spec/swagger/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function prepareDefaultOptions (opts) {
const consumes = swagger.consumes || null
const produces = swagger.produces || null
const definitions = swagger.definitions || null
const paths = swagger.paths || null
const basePath = swagger.basePath || null
const securityDefinitions = swagger.securityDefinitions || null
const security = swagger.security || null
Expand All @@ -40,6 +41,7 @@ function prepareDefaultOptions (opts) {
consumes,
produces,
definitions,
paths,
basePath,
securityDefinitions,
security,
Expand Down Expand Up @@ -73,6 +75,7 @@ function prepareSwaggerObject (opts) {
if (opts.consumes) swaggerObject.consumes = opts.consumes
if (opts.produces) swaggerObject.produces = opts.produces
if (opts.definitions) swaggerObject.definitions = opts.definitions
if (opts.paths) swaggerObject.paths = opts.paths
if (opts.securityDefinitions) swaggerObject.securityDefinitions = opts.securityDefinitions
if (opts.security) swaggerObject.security = opts.security
if (opts.tags) swaggerObject.tags = opts.tags
Expand Down
50 changes: 50 additions & 0 deletions test/spec/openapi/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,56 @@ test('openapi components', async (t) => {
delete openapiOption.openapi.components.schemas // remove what we just added
})

test('openapi paths', async (t) => {
t.plan(1)
const fastify = Fastify()

openapiOption.openapi.paths = {
'/status': {
get: {
description: 'Status route, so we can check if server is alive',
tags: [
'Status'
],
responses: {
200: {
description: 'Server is alive',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
health: {
type: 'boolean'
},
date: {
type: 'string'
}
},
example: {
health: true,
date: '2018-02-19T15:36:46.758Z'
}
}
}
}
}
}
}
}
}

await fastify.register(fastifySwagger, openapiOption)

fastify.get('/status', () => {})

await fastify.ready()

const openapiObject = fastify.swagger()
t.same(openapiObject.paths, openapiOption.openapi.paths)
delete openapiOption.openapi.paths // remove what we just added
})

test('hide support when property set in transform() - property', async (t) => {
t.plan(1)
const fastify = Fastify()
Expand Down
48 changes: 48 additions & 0 deletions test/spec/swagger/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,54 @@ test('swagger definitions', async (t) => {
delete swaggerOption.swagger.definitions // remove what we just added
})

test('swagger paths', async (t) => {
t.plan(1)
const fastify = Fastify()

swaggerOption.swagger.paths = {
'/status': {
get: {
description: 'Status route, so we can check if server is alive',
tags: [
'Status'
],
responses: {
200: {
description: 'Server is alive',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
health: {
type: 'boolean'
},
date: {
type: 'string'
}
},
example: {
health: true,
date: '2018-02-19T15:36:46.758Z'
}
}
}
}
}
}
}
}
}

await fastify.register(fastifySwagger, swaggerOption)

await fastify.ready()

const swaggerObject = fastify.swagger()
t.same(swaggerObject.paths, swaggerOption.swagger.paths)
delete swaggerOption.swagger.paths // remove what we just added
})

test('swagger tags', async (t) => {
t.plan(1)
const fastify = Fastify()
Expand Down

0 comments on commit 5277980

Please sign in to comment.