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
8 changes: 3 additions & 5 deletions lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,6 @@ function resolveBodyParams (opts, body, schema, consumes, ref) {
body.content[consume] = media
})

if (resolved?.required?.length) {
body.required = true
}

if (resolved?.description) {
body.description = resolved.description
}
Expand Down Expand Up @@ -475,7 +471,9 @@ function prepareOpenapiMethod (opts, schema, ref, openapiObject, url) {
if (schema.externalDocs) openapiMethod.externalDocs = schema.externalDocs
if (schema.querystring) resolveCommonParams(opts, 'query', parameters, schema.querystring, ref, openapiObject.definitions, securityIgnores.query)
if (schema.body) {
openapiMethod.requestBody = { content: {} }
// Fastify requires a body (at least {}) when schema.body is defined,
// so requestBody.required should always be true
openapiMethod.requestBody = { required: true, content: {} }
resolveBodyParams(opts, openapiMethod.requestBody, schema.body, schema.consumes, ref)
}
if (schema.params) resolveCommonParams(opts, 'path', parameters, schema.params, ref, openapiObject.definitions)
Expand Down
32 changes: 32 additions & 0 deletions test/spec/openapi/option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,38 @@ test('marks request body as required', async (t) => {
t.assert.deepStrictEqual(requestBody.required, true)
})

test('marks request body as required even when all properties are optional', async (t) => {
t.plan(3)
const fastify = Fastify()

await fastify.register(fastifySwagger, openapiOption)

const body = {
type: 'object',
properties: {
hello: {
type: 'string'
}
}
}

const opts = {
schema: {
body
}
}

fastify.put('/', opts, () => {})

await fastify.ready()
const openapiObject = fastify.swagger()
const requestBody = openapiObject.paths['/'].put.requestBody

t.assert.ok(requestBody)
t.assert.strictEqual(requestBody.required, true)
t.assert.ok(requestBody.content['application/json'].schema)
})

test('openapi webhooks properties', async (t) => {
t.plan(1)
const fastify = Fastify()
Expand Down
6 changes: 6 additions & 0 deletions test/spec/openapi/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ test('support "const" keyword', async t => {

const definedPath = api.paths['/'].post
t.assert.deepStrictEqual(JSON.parse(JSON.stringify(definedPath.requestBody)), {
required: true,
content: {
'application/json': {
schema: {
Expand Down Expand Up @@ -793,6 +794,7 @@ test('convert "const" to "enum"', async t => {

const definedPath = api.paths['/'].post
t.assert.deepStrictEqual(JSON.parse(JSON.stringify(definedPath.requestBody)), {
required: true,
content: {
'application/json': {
schema: {
Expand Down Expand Up @@ -855,6 +857,7 @@ test('support object properties named "const"', async t => {

const definedPath = api.paths['/'].post
t.assert.deepStrictEqual(JSON.parse(JSON.stringify(definedPath.requestBody)), {
required: true,
content: {
'application/json': {
schema: {
Expand Down Expand Up @@ -914,6 +917,7 @@ test('support object properties with special names', async t => {

const definedPath = api.paths['/'].post
t.assert.deepStrictEqual(JSON.parse(JSON.stringify(definedPath.requestBody)), {
required: true,
content: {
'application/json': {
schema: {
Expand Down Expand Up @@ -968,6 +972,7 @@ test('support "description" keyword', async t => {

const definedPath = api.paths['/'].post
t.assert.deepStrictEqual(JSON.parse(JSON.stringify(definedPath.requestBody)), {
required: true,
description: 'Body description',
content: {
'application/json': {
Expand Down Expand Up @@ -1239,6 +1244,7 @@ test('support multiple content types as request', async t => {

const definedPath = api.paths['/'].post
t.assert.deepStrictEqual(definedPath.requestBody, {
required: true,
content: {
'application/json': {
schema: {
Expand Down
Loading