Skip to content

Commit e721283

Browse files
committed
🔧 fix(type-generator): handle union type
1 parent 29c5bc2 commit e721283

File tree

4 files changed

+56
-13
lines changed

4 files changed

+56
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.3.5 - 3 Sep 2025
2+
Bug fix:
3+
- type generator: merge references with existing response status
4+
- type generator: handle union type
5+
16
# 1.3.4 - 3 Sep 2025
27
Bug fix:
38
- type generator: exclude unknown type

example/gen.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Elysia, t } from 'elysia'
2-
import { openapi } from '../src/index'
2+
import { openapi, withHeaders } from '../src/index'
33
import { fromTypes } from '../src/gen'
44

55
export const app = new Elysia()
@@ -8,7 +8,26 @@ export const app = new Elysia()
88
references: fromTypes('example/gen.ts')
99
})
1010
)
11-
.get('/', { test: 'hello' as const })
11+
.get(
12+
'/',
13+
() =>
14+
({ test: 'hello' as const }) as any as
15+
| { test: 'hello' }
16+
| undefined,
17+
{
18+
response: {
19+
204: withHeaders(
20+
t.Void({
21+
title: 'Thing',
22+
description: 'Void response'
23+
}),
24+
{
25+
'X-Custom-Header': t.Literal('Elysia')
26+
}
27+
)
28+
}
29+
}
30+
)
1231
.post(
1332
'/json',
1433
({ body, status }) => (Math.random() > 0.5 ? status(418) : body),

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.3.4",
3+
"version": "1.3.5",
44
"description": "Plugin for Elysia to auto-generate API documentation",
55
"author": {
66
"name": "saltyAom",

src/openapi.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { t, type AnyElysia, type TSchema, type InputSchema } from 'elysia'
22
import type { HookContainer } from 'elysia/types'
33

44
import type { OpenAPIV3 } from 'openapi-types'
5-
import type { TProperties } from '@sinclair/typebox'
5+
import { Kind, type TProperties } from '@sinclair/typebox'
66

77
import type {
88
AdditionalReference,
@@ -53,6 +53,13 @@ export const getPossiblePath = (path: string): string[] => {
5353
return paths
5454
}
5555

56+
const isValidSchema = (schema: any): schema is TSchema =>
57+
typeof schema === 'object' &&
58+
((Kind in schema && schema[Kind] !== 'Unknown') ||
59+
schema.type ||
60+
schema.properties ||
61+
schema.items)
62+
5663
/**
5764
* Converts Elysia routes to OpenAPI 3.0.3 paths schema
5865
* @param routes Array of Elysia route objects
@@ -111,21 +118,33 @@ export function toOpenAPISchema(
111118
const refer = reference[route.path]?.[method]
112119
if (!refer) continue
113120

114-
if (!hooks.body && refer.body?.type) hooks.body = refer.body
115-
if (!hooks.query && refer.query?.type) hooks.query = refer.query
116-
if (!hooks.params && refer.params?.type)
121+
if (!hooks.body && isValidSchema(refer.body))
122+
hooks.body = refer.body
123+
124+
if (!hooks.query && isValidSchema(refer.query))
125+
hooks.query = refer.query
126+
127+
if (!hooks.params && isValidSchema(refer.params))
117128
hooks.params = refer.params
118-
if (!hooks.headers && refer.headers?.type)
129+
130+
if (!hooks.headers && isValidSchema(refer.headers))
119131
hooks.headers = refer.headers
120-
if (!hooks.response && refer.response) {
121-
hooks.response = {}
122132

133+
if (refer.response)
123134
for (const [status, schema] of Object.entries(
124135
refer.response
125136
))
126-
if (!hooks.response[status as any] && schema?.type)
127-
hooks.response[status as any] = schema
128-
}
137+
if (isValidSchema(schema)) {
138+
if (!hooks.response) hooks.response = {}
139+
140+
if (
141+
!hooks.response[
142+
status as keyof (typeof hooks)['response']
143+
]
144+
)
145+
// @ts-ignore
146+
hooks.response[status] = schema
147+
}
129148
}
130149

131150
if (

0 commit comments

Comments
 (0)