Skip to content

Commit

Permalink
chore: defining empty bodies for any status codes (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopedrocampos authored Jul 12, 2021
1 parent 84f7cfe commit d4f9bb2
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 6 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,9 @@ You can decorate your own response headers by following the below example:
```
Note: You need to specify `type` property when you decorate the response headers, otherwise the schema will be modified by Fastify.
<a name="route.response.204"></a>
##### Status code 204
Status code 204 is supported by `fastify-swagger` and returns an empty body.
<a name="route.response.empty_body"></a>
##### Empty Body Responses
Empty body responses are supported by `fastify-swagger`.
Please specify `type: 'null'` for the response otherwise Fastify itself will fail to compile the schema:
```js
Expand All @@ -399,6 +399,10 @@ Please specify `type: 'null'` for the response otherwise Fastify itself will fai
204: {
type: 'null',
description: 'No Content'
},
503: {
type: 'null',
description: 'Service Unavailable'
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ function resolveResponse (fastifyResponseJson, produces, ref) {
delete resolved.headers
}

if (statusCode.toString() !== '204') {
// add schema when type is not 'null'
if (rawJsonSchema.type !== 'null') {
const content = {}

if ((Array.isArray(produces) && produces.length === 0) || typeof produces === 'undefined') {
Expand Down
4 changes: 2 additions & 2 deletions lib/spec/swagger/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ function resolveResponse (fastifyResponseJson, ref) {
delete resolved.headers
}

// add schema when status code is not 204
if (statusCode.toString() !== '204') {
// add schema when type is not 'null'
if (rawJsonSchema.type !== 'null') {
const schema = { ...resolved }
delete schema[xResponseDescription]
response.schema = schema
Expand Down
38 changes: 38 additions & 0 deletions test/spec/openapi/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,44 @@ test('support status code 204', async t => {
t.notOk(definedPath.responses['204'].content)
})

test('support empty response body for different status than 204', async t => {
const opt = {
schema: {
response: {
204: {
type: 'null',
description: 'No Content'
},
503: {
type: 'null',
description: 'Service Unavailable'
}
}
}
}

const fastify = Fastify()
fastify.register(fastifySwagger, {
openapi: true,
routePrefix: '/docs',
exposeRoute: true
})
fastify.get('/', opt, () => {})

await fastify.ready()

const swaggerObject = fastify.swagger()
const api = await Swagger.validate(swaggerObject)

const definedPath = api.paths['/'].get

t.same(definedPath.responses['204'].description, 'No Content')
t.notOk(definedPath.responses['204'].content)

t.same(definedPath.responses['503'].description, 'Service Unavailable')
t.notOk(definedPath.responses['503'].content)
})

test('support response headers', async t => {
const opt = {
schema: {
Expand Down
39 changes: 39 additions & 0 deletions test/spec/swagger/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,45 @@ test('support status code 204', async t => {
t.notOk(definedPath.responses['204'].schema)
})

test('support empty response body for different status than 204', async t => {
const opt = {
schema: {
response: {
204: {
type: 'null',
description: 'No Content'
},
503: {
type: 'null',
description: 'Service Unavailable'
}
}
}
}

const fastify = Fastify()
fastify.register(fastifySwagger, {
routePrefix: '/docs',
exposeRoute: true
})
fastify.get('/', opt, () => {})

await fastify.ready()

const swaggerObject = fastify.swagger()
const api = await Swagger.validate(swaggerObject)

const definedPath = api.paths['/'].get

t.same(definedPath.responses['204'].description, 'No Content')
t.notOk(definedPath.responses['204'].content)
t.notOk(definedPath.responses['503'].type)

t.same(definedPath.responses['503'].description, 'Service Unavailable')
t.notOk(definedPath.responses['503'].content)
t.notOk(definedPath.responses['503'].type)
})

test('support response headers', async t => {
const opt = {
schema: {
Expand Down

0 comments on commit d4f9bb2

Please sign in to comment.