Skip to content
Open
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
6 changes: 6 additions & 0 deletions lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ function resolveBodyParams (body, schema, consumes, ref) {
for (const contentType in schema.content) {
body.content[contentType] = schemaToMediaRecursive(resolved.content[contentType].schema)
}
if (resolved.required) {
body.required = true
}
if (resolved.description) {
body.description = resolved.description
}
} else {
if ((Array.isArray(consumes) && consumes.length === 0) || consumes === undefined) {
consumes = ['application/json']
Expand Down
51 changes: 51 additions & 0 deletions test/spec/openapi/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,57 @@ test('avoid overwriting params when schema.params is provided', async t => {
})
})

test('supports setting body fields manually on request', async t => {
const opt = {
schema: {
body: {
content: {
'application/json': {
schema: {
type: 'object',
properties: {
jsonProperty: {
type: 'string'
}
}
}
}
},
description: 'My body description',
required: true
}
}
}

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.assert.deepStrictEqual(definedPath.requestBody, {
content: {
'application/json': {
schema: {
type: 'object',
properties: {
jsonProperty: {
type: 'string'
}
}
}
}
},
description: 'My body description',
required: true
})
})

test('support multiple content types as request', async t => {
const opt = {
schema: {
Expand Down
Loading