Skip to content

Commit 03783d5

Browse files
authored
feat: support multiple content type schemas per request (#819)
1 parent f29078c commit 03783d5

File tree

2 files changed

+86
-12
lines changed

2 files changed

+86
-12
lines changed

lib/spec/openapi/utils.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -263,21 +263,28 @@ function schemaToMediaRecursive (schema) {
263263

264264
function resolveBodyParams (body, schema, consumes, ref) {
265265
const resolved = convertJsonSchemaToOpenapi3(ref.resolve(schema))
266-
if ((Array.isArray(consumes) && consumes.length === 0) || consumes === undefined) {
267-
consumes = ['application/json']
268-
}
269266

270-
const media = schemaToMediaRecursive(resolved)
271-
consumes.forEach((consume) => {
272-
body.content[consume] = media
273-
})
267+
if (resolved.content && resolved.content[Object.keys(resolved.content)[0]].schema) {
268+
for (const contentType in schema.content) {
269+
body.content[contentType] = schemaToMediaRecursive(resolved.content[contentType].schema)
270+
}
271+
} else {
272+
if ((Array.isArray(consumes) && consumes.length === 0) || consumes === undefined) {
273+
consumes = ['application/json']
274+
}
274275

275-
if (resolved && resolved.required && resolved.required.length) {
276-
body.required = true
277-
}
276+
const media = schemaToMediaRecursive(resolved)
277+
consumes.forEach((consume) => {
278+
body.content[consume] = media
279+
})
280+
281+
if (resolved && resolved.required && resolved.required.length) {
282+
body.required = true
283+
}
278284

279-
if (resolved && resolved.description) {
280-
body.description = resolved.description
285+
if (resolved && resolved.description) {
286+
body.description = resolved.description
287+
}
281288
}
282289
}
283290

test/spec/openapi/schema.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,3 +1034,70 @@ test('avoid overwriting params when schema.params is provided', async t => {
10341034
}
10351035
})
10361036
})
1037+
1038+
test('support multiple content types as request', async t => {
1039+
const opt = {
1040+
schema: {
1041+
body: {
1042+
content: {
1043+
'application/json': {
1044+
schema: {
1045+
type: 'object',
1046+
properties: {
1047+
jsonProperty: {
1048+
type: 'string'
1049+
}
1050+
}
1051+
}
1052+
},
1053+
'application/xml': {
1054+
schema: {
1055+
type: 'object',
1056+
properties: {
1057+
xmlProperty: {
1058+
type: 'string'
1059+
}
1060+
}
1061+
}
1062+
}
1063+
}
1064+
}
1065+
}
1066+
}
1067+
1068+
const fastify = Fastify()
1069+
await fastify.register(fastifySwagger, {
1070+
openapi: true
1071+
})
1072+
fastify.post('/', opt, () => { })
1073+
await fastify.ready()
1074+
1075+
const swaggerObject = fastify.swagger()
1076+
const api = await Swagger.validate(swaggerObject)
1077+
1078+
const definedPath = api.paths['/'].post
1079+
t.match(definedPath.requestBody, {
1080+
content: {
1081+
'application/json': {
1082+
schema: {
1083+
type: 'object',
1084+
properties: {
1085+
jsonProperty: {
1086+
type: 'string'
1087+
}
1088+
}
1089+
}
1090+
},
1091+
'application/xml': {
1092+
schema: {
1093+
type: 'object',
1094+
properties: {
1095+
xmlProperty: {
1096+
type: 'string'
1097+
}
1098+
}
1099+
}
1100+
}
1101+
}
1102+
})
1103+
})

0 commit comments

Comments
 (0)