Skip to content

Commit 85bcc9b

Browse files
committed
fix: support setting body fields manually in request schema
1 parent b01b108 commit 85bcc9b

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

lib/spec/openapi/utils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@ function resolveBodyParams (body, schema, consumes, ref) {
269269
for (const contentType in schema.content) {
270270
body.content[contentType] = schemaToMediaRecursive(resolved.content[contentType].schema)
271271
}
272+
if (resolved.required) {
273+
body.required = true
274+
}
275+
if (resolved.description) {
276+
body.description = resolved.description
277+
}
272278
} else {
273279
if ((Array.isArray(consumes) && consumes.length === 0) || consumes === undefined) {
274280
consumes = ['application/json']

test/spec/openapi/schema.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,57 @@ test('avoid overwriting params when schema.params is provided', async t => {
11271127
})
11281128
})
11291129

1130+
test('supports setting body fields manually on request', async t => {
1131+
const opt = {
1132+
schema: {
1133+
body: {
1134+
content: {
1135+
'application/json': {
1136+
schema: {
1137+
type: 'object',
1138+
properties: {
1139+
jsonProperty: {
1140+
type: 'string'
1141+
}
1142+
}
1143+
}
1144+
}
1145+
},
1146+
description: 'My body description',
1147+
required: true
1148+
}
1149+
}
1150+
}
1151+
1152+
const fastify = Fastify()
1153+
await fastify.register(fastifySwagger, {
1154+
openapi: true
1155+
})
1156+
fastify.post('/', opt, () => { })
1157+
await fastify.ready()
1158+
1159+
const swaggerObject = fastify.swagger()
1160+
const api = await Swagger.validate(swaggerObject)
1161+
1162+
const definedPath = api.paths['/'].post
1163+
t.assert.deepStrictEqual(definedPath.requestBody, {
1164+
content: {
1165+
'application/json': {
1166+
schema: {
1167+
type: 'object',
1168+
properties: {
1169+
jsonProperty: {
1170+
type: 'string'
1171+
}
1172+
}
1173+
}
1174+
}
1175+
},
1176+
description: 'My body description',
1177+
required: true
1178+
})
1179+
})
1180+
11301181
test('support multiple content types as request', async t => {
11311182
const opt = {
11321183
schema: {

0 commit comments

Comments
 (0)