Skip to content

Commit 8169471

Browse files
committed
🔧 fix(type-gen): handle inline 200 response schema assignment
1 parent 364ec57 commit 8169471

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.4.6 - 21 Sep 2025
2+
Bug fix:
3+
- type gen: handle inline 200 response schema assignment
4+
15
# 1.4.5 - 20 Sep 2025
26
Improvement:
37
- reference model now handle content type correctly

example/gen.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { Elysia, t } from 'elysia'
22
import { openapi, withHeaders } from '../src/index'
33
import { fromTypes } from '../src/gen'
4+
import z from 'zod'
45

5-
export const app = new Elysia({ prefix: '/id' })
6+
export const app = new Elysia()
67
.use(
78
openapi({
9+
mapJsonSchema: {
10+
zod: z.toJSONSchema
11+
},
812
references: fromTypes('example/gen.ts', {
913
debug: true
1014
})
@@ -49,13 +53,13 @@ export const app = new Elysia({ prefix: '/id' })
4953
.post(
5054
'/character',
5155
() => ({
52-
name: 'Elysia'
56+
name: 'Lilith' as const
5357
}),
5458
{
5559
body: 'character.name',
56-
response: {
57-
200: 'character.thing'
58-
}
60+
response: z.object({
61+
name: z.literal('Lilith')
62+
})
5963
}
6064
)
6165
.listen(3000)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@elysiajs/openapi",
3-
"version": "1.4.5",
3+
"version": "1.4.6",
44
"description": "Plugin for Elysia to auto-generate API documentation",
55
"author": {
66
"name": "saltyAom",

src/openapi.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,31 @@ export function toOpenAPISchema(
290290
))
291291
if (isValidSchema(schema)) {
292292
if (!hooks.response) hooks.response = {}
293+
else if (
294+
typeof hooks.response !== 'object' ||
295+
(hooks.response as TSchema).type ||
296+
(hooks.response as TSchema).$ref ||
297+
(hooks.response as any)['~standard']
298+
)
299+
// @ts-ignore
300+
hooks.response = {
301+
200: hooks.response as any
302+
}
293303

294304
if (
295305
!hooks.response[
296306
status as keyof (typeof hooks)['response']
297307
]
298308
)
299-
// @ts-ignore
300-
hooks.response[status] = schema
309+
try {
310+
// @ts-ignore
311+
hooks.response[status] = schema
312+
} catch (error) {
313+
console.log(
314+
'[@elysiajs/openapi/gen] Failed to assigned response schema'
315+
)
316+
console.log(error)
317+
}
301318
}
302319
}
303320

@@ -496,8 +513,10 @@ export function toOpenAPISchema(
496513

497514
if (
498515
typeof hooks.response === 'object' &&
516+
// TypeBox
499517
!(hooks.response as TSchema).type &&
500-
!(hooks.response as TSchema).$ref
518+
!(hooks.response as TSchema).$ref &&
519+
!(hooks.response as any)['~standard']
501520
) {
502521
for (let [status, schema] of Object.entries(hooks.response)) {
503522
const response = unwrapSchema(schema, vendors)

0 commit comments

Comments
 (0)