Skip to content

Commit 8a775ca

Browse files
committed
🔧 fix: #262 when failed to convert type to OpenAPI, log the error and continue
1 parent 6d4efbb commit 8a775ca

File tree

3 files changed

+72
-74
lines changed

3 files changed

+72
-74
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Improvement:
44
- accept operationId from detail
55
- type gen: accept number as path segment
66
- add test case for type gen, and OpenAPI schema
7+
- when failed to convert type to OpenAPI, log the error and continue
78

89
Bug fix:
910
- [#226](https://github.com/elysiajs/elysia-openapi/issues/266) accept operationId

example/index.ts

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,19 @@ import { JSONSchema, Schema } from 'effect'
44

55
import { openapi, withHeaders } from '../src/index'
66

7-
const schema = t.Object({
8-
test: t.Literal('hello')
9-
})
10-
11-
const schema2 = t.Object({
12-
test: t.Literal('world')
13-
})
14-
15-
const user = t.Object({
16-
name: t.String({
17-
example: 'saltyaom'
18-
})
19-
})
20-
21-
const model = new Elysia().model(
22-
'body',
23-
t.Object({
24-
name: t.Literal('Lilith')
25-
})
26-
)
7+
// const a = z.toJSONSchema(z.void())
278

289
const app = new Elysia()
29-
.use(openapi())
30-
.use(model)
31-
.post('/user', () => 'hello', {
32-
body: 'body'
10+
.use(
11+
openapi({
12+
mapJsonSchema: {
13+
zod: z.toJSONSchema
14+
}
15+
})
16+
)
17+
.get('/test', ({ status }) => status(204, undefined), {
18+
response: {
19+
204: z.void()
20+
}
3321
})
3422
.listen(3000)

src/openapi.ts

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -139,69 +139,78 @@ export const unwrapSchema = (
139139
// @ts-ignore
140140
const vendor = schema['~standard'].vendor
141141

142-
if (mapJsonSchema?.[vendor] && typeof mapJsonSchema[vendor] === 'function')
143-
return enumToOpenApi(mapJsonSchema[vendor](schema))
142+
try {
143+
if (
144+
mapJsonSchema?.[vendor] &&
145+
typeof mapJsonSchema[vendor] === 'function'
146+
)
147+
return enumToOpenApi(mapJsonSchema[vendor](schema))
144148

145-
switch (vendor) {
146-
case 'zod':
147-
if (warned.zod4 || warned.zod3) break
149+
switch (vendor) {
150+
case 'zod':
151+
if (warned.zod4 || warned.zod3) break
148152

149-
console.warn(
150-
"[@elysiajs/openapi] Zod doesn't provide JSON Schema method on the schema"
151-
)
153+
console.warn(
154+
"[@elysiajs/openapi] Zod doesn't provide JSON Schema method on the schema"
155+
)
156+
157+
if ('_zod' in schema) {
158+
warned.zod4 = true
152159

153-
if ('_zod' in schema) {
154-
warned.zod4 = true
160+
console.warn(
161+
'For Zod v4, please provide z.toJSONSchema as follows:\n'
162+
)
163+
console.warn(warnings.zod4)
164+
} else {
165+
warned.zod3 = true
166+
167+
console.warn(
168+
'For Zod v3, please install zod-to-json-schema package and use it like this:\n'
169+
)
170+
console.warn(warnings.zod3)
171+
}
172+
break
173+
174+
case 'valibot':
175+
if (warned.valibot) break
176+
warned.valibot = true
155177

156178
console.warn(
157-
'For Zod v4, please provide z.toJSONSchema as follows:\n'
179+
'[@elysiajs/openapi] Valibot require a separate package for JSON Schema conversion'
158180
)
159-
console.warn(warnings.zod4)
160-
} else {
161-
warned.zod3 = true
162-
163181
console.warn(
164-
'For Zod v3, please install zod-to-json-schema package and use it like this:\n'
182+
'Please install @valibot/to-json-schema package and use it like this:\n'
165183
)
166-
console.warn(warnings.zod3)
167-
}
168-
break
184+
console.warn(warnings.valibot)
185+
break
169186

170-
case 'valibot':
171-
if (warned.valibot) break
172-
warned.valibot = true
187+
case 'effect':
188+
// Effect does not support toJsonSchema method
189+
// Users have to use third party library like effect-zod
190+
if (warned.effect) break
191+
warned.effect = true
173192

174-
console.warn(
175-
'[@elysiajs/openapi] Valibot require a separate package for JSON Schema conversion'
176-
)
177-
console.warn(
178-
'Please install @valibot/to-json-schema package and use it like this:\n'
179-
)
180-
console.warn(warnings.valibot)
181-
break
193+
console.warn(
194+
"[@elysiajs/openapi] Effect Schema doesn't provide JSON Schema method on the schema"
195+
)
196+
console.warn(
197+
"please provide JSONSchema from 'effect' package as follows:\n"
198+
)
199+
console.warn(warnings.effect)
200+
break
201+
}
182202

183-
case 'effect':
184-
// Effect does not support toJsonSchema method
185-
// Users have to use third party library like effect-zod
186-
if (warned.effect) break
187-
warned.effect = true
203+
if (vendor === 'arktype')
204+
// @ts-ignore
205+
return enumToOpenApi(schema?.toJsonSchema?.())
188206

189-
console.warn(
190-
"[@elysiajs/openapi] Effect Schema doesn't provide JSON Schema method on the schema"
191-
)
192-
console.warn(
193-
"please provide JSONSchema from 'effect' package as follows:\n"
194-
)
195-
console.warn(warnings.effect)
196-
break
207+
return enumToOpenApi(
208+
// @ts-ignore
209+
schema.toJSONSchema?.() ?? schema?.toJsonSchema?.()
210+
)
211+
} catch (error) {
212+
console.warn(error)
197213
}
198-
199-
if (vendor === 'arktype')
200-
// @ts-ignore
201-
return enumToOpenApi(schema?.toJsonSchema?.())
202-
203-
// @ts-ignore
204-
return enumToOpenApi(schema.toJSONSchema?.() ?? schema?.toJsonSchema?.())
205214
}
206215

207216
export const enumToOpenApi = <

0 commit comments

Comments
 (0)