diff --git a/lib/spec/openapi/utils.js b/lib/spec/openapi/utils.js index b20200eb..a53660b1 100644 --- a/lib/spec/openapi/utils.js +++ b/lib/spec/openapi/utils.js @@ -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'] diff --git a/test/spec/openapi/schema.js b/test/spec/openapi/schema.js index 1ac109b9..1c8d885c 100644 --- a/test/spec/openapi/schema.js +++ b/test/spec/openapi/schema.js @@ -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: {