Skip to content

Commit

Permalink
feat: support description field on openapi and swagger bodies (#725)
Browse files Browse the repository at this point in the history
  • Loading branch information
steckhelena authored May 24, 2023
1 parent 4ea925c commit 61229a8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ function resolveBodyParams (body, schema, consumes, ref) {
if (resolved && resolved.required && resolved.required.length) {
body.required = true
}

if (resolved && resolved.description) {
body.description = resolved.description
}
}

function resolveCommonParams (container, parameters, schema, ref, sharedSchemas, securityIgnores) {
Expand Down
1 change: 1 addition & 0 deletions lib/spec/swagger/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ function resolveBodyParams (parameters, schema, ref) {
parameters.push({
name: 'body',
in: 'body',
description: resolved && resolved.description ? resolved.description : undefined,
schema: resolved
})
}
Expand Down
44 changes: 44 additions & 0 deletions test/spec/openapi/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,50 @@ test('support object properties with special names', async t => {
})
})

test('support "description" keyword', async t => {
const opt = {
schema: {
body: {
type: 'object',
description: 'Body description',
properties: {
foo: {
type: 'number'
}
}
}
}
}

const fastify = Fastify()
await fastify.register(fastifySwagger, {
openapi: true
})
fastify.post('/', opt, () => { })
await fastify.ready()

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

const definedPath = api.paths['/'].post
t.same(definedPath.requestBody, {
description: 'Body description',
content: {
'application/json': {
schema: {
description: 'Body description',
type: 'object',
properties: {
foo: {
type: 'number'
}
}
}
}
}
})
})

test('support query serialization params', async t => {
const opt = {
schema: {
Expand Down
36 changes: 36 additions & 0 deletions test/spec/swagger/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,42 @@ test('support "const" keyword', async t => {
})
})

test('support "description" keyword', async t => {
const opt = {
schema: {
body: {
type: 'object',
description: 'Body description',
properties: {
foo: {
type: 'number'
}
}
}
}
}

const fastify = Fastify()
await fastify.register(fastifySwagger)
fastify.post('/', opt, () => { })
await fastify.ready()

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

const definedPath = api.paths['/'].post
t.same(definedPath.parameters[0].description, 'Body description')
t.same(definedPath.parameters[0].schema, {
type: 'object',
description: 'Body description',
properties: {
foo: {
type: 'number'
}
}
})
})

test('no head routes by default', async (t) => {
const fastify = Fastify({ exposeHeadRoutes: true })
await fastify.register(fastifySwagger, {
Expand Down

0 comments on commit 61229a8

Please sign in to comment.