Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support description field on openapi and swagger bodies #725

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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