diff --git a/README.md b/README.md
index 960d8333..3af31062 100644
--- a/README.md
+++ b/README.md
@@ -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.
-
-##### Status code 204
-Status code 204 is supported by `fastify-swagger` and returns an empty body.
+
+##### 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
@@ -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'
}
}
}
diff --git a/lib/spec/openapi/utils.js b/lib/spec/openapi/utils.js
index 47a7b77a..b3a1d68a 100644
--- a/lib/spec/openapi/utils.js
+++ b/lib/spec/openapi/utils.js
@@ -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') {
diff --git a/lib/spec/swagger/utils.js b/lib/spec/swagger/utils.js
index 777b280f..26c6f657 100644
--- a/lib/spec/swagger/utils.js
+++ b/lib/spec/swagger/utils.js
@@ -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
diff --git a/test/spec/openapi/schema.js b/test/spec/openapi/schema.js
index c15da3ac..719f27bb 100644
--- a/test/spec/openapi/schema.js
+++ b/test/spec/openapi/schema.js
@@ -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: {
diff --git a/test/spec/swagger/schema.js b/test/spec/swagger/schema.js
index b1fc9033..11f9e324 100644
--- a/test/spec/swagger/schema.js
+++ b/test/spec/swagger/schema.js
@@ -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: {