Skip to content

Commit 55c8fb2

Browse files
author
Kai Gritun
committed
fix: include body schema in OpenAPI docs when parse is 'none'
When using `parse: 'none'` to preserve raw request bodies, the body schema was being omitted from generated OpenAPI documentation because the 'none' case was not handled in the content type switch statement. This fix adds a case for 'none' that includes all common content types (application/json, application/x-www-form-urlencoded, multipart/form-data, text/plain) since the raw body could be any format. Fixes #1720
1 parent 9d23f7b commit 55c8fb2

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

bun.lock

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/openapi.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,17 @@ export function toOpenAPISchema(
936936
schema: body
937937
}
938938
continue
939+
940+
case 'none':
941+
// When parse is "none", include all common content types
942+
// since the raw body could be any format
943+
content['application/json'] = { schema: body }
944+
content['application/x-www-form-urlencoded'] = {
945+
schema: body
946+
}
947+
content['multipart/form-data'] = { schema: body }
948+
content['text/plain'] = { schema: body }
949+
continue
939950
}
940951
}
941952

test/openapi/to-openapi-schema.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,4 +1276,32 @@ describe('OpenAPI > toOpenAPISchema', () => {
12761276
}
12771277
})
12781278
})
1279+
1280+
it('include body schema when parse is "none"', () => {
1281+
const app = new Elysia().post(
1282+
'/echo',
1283+
({ request }) => request,
1284+
{
1285+
body: t.Object({ input: t.String() }),
1286+
parse: 'none'
1287+
}
1288+
)
1289+
1290+
const schema = JSON.parse(JSON.stringify(toOpenAPISchema(app)))
1291+
1292+
expect(schema.paths['/echo'].post.requestBody).toBeDefined()
1293+
expect(schema.paths['/echo'].post.requestBody.content).toBeDefined()
1294+
expect(
1295+
schema.paths['/echo'].post.requestBody.content['application/json']
1296+
).toBeDefined()
1297+
expect(
1298+
schema.paths['/echo'].post.requestBody.content['application/json'].schema
1299+
).toEqual({
1300+
type: 'object',
1301+
properties: {
1302+
input: { type: 'string' }
1303+
},
1304+
required: ['input']
1305+
})
1306+
})
12791307
})

0 commit comments

Comments
 (0)