Skip to content

Commit

Permalink
feat: rendering the syntax of different content types responses (#665)
Browse files Browse the repository at this point in the history
* Supporting different content types responses

Signed-off-by: iifawzi <[email protected]>

* updating the docs

Signed-off-by: iifawzi <[email protected]>

* Refactoring for-loop and update the tests

Signed-off-by: iifawzi <[email protected]>

* update package.json to use repo

Signed-off-by: iifawzi <[email protected]>

* update the rendering logic according to fastify changes

Signed-off-by: iifawzi <[email protected]>

* update the ducktyping

Signed-off-by: iifawzi <[email protected]>

* Apply suggestions from code review

Co-authored-by: Uzlopak <[email protected]>

* revert fastify deleted by mistake

Signed-off-by: iifawzi <[email protected]>

* Update test/spec/openapi/schema.js

Co-authored-by: Uzlopak <[email protected]>

* Using strictSame and temporary updat packagejson to check ci

Signed-off-by: iifawzi <[email protected]>

* update deps

Signed-off-by: iifawzi <[email protected]>

Signed-off-by: iifawzi <[email protected]>
Co-authored-by: Uzlopak <[email protected]>
  • Loading branch information
iifawzi and Uzlopak authored Oct 17, 2022
1 parent c4c8317 commit fe779d2
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 10 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,35 @@ 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.empty_body"></a>
##### Different content types responses
**Note:** not supported by Swagger (OpenAPI v2), [only OpenAPI v3](https://swagger.io/docs/specification/describing-responses/)
Different content types responses are supported by `@fastify/swagger` and `@fastify`.
Please use `content` for the response otherwise Fastify itself will fail to compile the schema:
```js
{
response: {
200: {
description: 'Description and all status-code based properties are working',
content: {
'application/json': {
schema: {
name: { type: 'string' },
image: { type: 'string' },
address: { type: 'string' }
}
},
'application/vnd.v1+json': {
schema: {
fullName: { type: 'string' },
phone: { type: 'string' }
}
}
}
}
}
}
```
##### 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:
Expand Down
25 changes: 15 additions & 10 deletions lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,20 +323,25 @@ function resolveResponse (fastifyResponseJson, produces, ref) {

// add schema when type is not 'null'
if (rawJsonSchema.type !== 'null') {
const content = {}
if (resolved.content && resolved.content[Object.keys(resolved.content)[0]].schema) {
response.content = resolved.content
} else {
const content = {}

if ((Array.isArray(produces) && produces.length === 0) || typeof produces === 'undefined') {
produces = ['application/json']
}
if ((Array.isArray(produces) && produces.length === 0) || typeof produces === 'undefined') {
produces = ['application/json']
}

delete resolved[xResponseDescription]
delete resolved[xResponseDescription]

const media = schemaToMedia(resolved)
produces.forEach((produce) => {
content[produce] = media
})
const media = schemaToMedia(resolved)

response.content = content
for (const produce of produces) {
content[produce] = media
}

response.content = content
}
}

responsesContainer[statusCode] = response
Expand Down
90 changes: 90 additions & 0 deletions test/spec/openapi/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,96 @@ test('support 2xx response', async t => {
t.same(definedPath.responses['3XX'].description, 'Default Response')
})

test('support multiple content types as response', async t => {
const fastify = Fastify()
await fastify.register(fastifySwagger, {
openapi: true,
routePrefix: '/docs',
exposeRoute: true
})

const opt = {
schema: {
response: {
200: {
description: 'Description and all status-code based properties are working',
content: {
'application/json': {
schema: {
name: { type: 'string' },
image: { type: 'string' },
address: { type: 'string' }
}
},
'application/vnd.v1+json': {
schema: {
fullName: { type: 'string' },
phone: { type: 'string' }
}
}
}
},
'4xx': {
type: 'object',
properties: {
name: { type: 'string' }
}
},
300: {
age: { type: 'number' }
}
}
}
}
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['200'].description, 'Description and all status-code based properties are working')
t.strictSame(definedPath.responses['200'].content, {
'application/json': {
schema: {
type: 'object',
properties: {
name: { type: 'string' }, image: { type: 'string' }, address: { type: 'string' }
}
}
},
'application/vnd.v1+json': {
schema: {
type: 'object',
properties: {
fullName: { type: 'string' }, phone: { type: 'string' }
}
}
}
})
t.same(definedPath.responses['4XX'].description, 'Default Response')
t.strictSame(definedPath.responses['4XX'].content, {
'application/json': {
schema: {
type: 'object',
properties: {
name: { type: 'string' }
}
}
}
})
t.strictSame(definedPath.responses[300].content, {
'application/json': {
schema: {
type: 'object',
properties: {
age: { type: 'number' }
}
}
}
})
})

test('support status code 204', async t => {
const opt = {
schema: {
Expand Down

0 comments on commit fe779d2

Please sign in to comment.