Skip to content

Commit f19c4ea

Browse files
committed
🎉 feat: support standalone schema
1 parent ded6cf8 commit f19c4ea

File tree

5 files changed

+19
-34
lines changed

5 files changed

+19
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# 1.4.12 - 18 Dec 2025
2+
- support standard json schema conversion
23
- [#300](https://github.com/elysiajs/elysia-openapi/pull/300) support standalone schema by @MarcelOlsen
34

45
# 1.4.11 - 25 Sep 2025

bun.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/index.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,13 @@ import { Elysia, t } from 'elysia'
22
import z from 'zod'
33
import { JSONSchema, Schema } from 'effect'
44

5-
import { openapi, withHeaders } from '../src/index'
5+
import { openapi } from '../src/index'
66

77
const app = new Elysia()
8-
.use(
9-
openapi({
10-
mapJsonSchema: {
11-
zod: z.toJSONSchema
12-
}
13-
})
14-
)
15-
.guard({
16-
schema: 'standalone',
17-
response: {
18-
403: t.Object({
19-
age: t.Number()
20-
})
21-
}
22-
})
23-
// .guard({
24-
// schema: 'standalone',
25-
// response: {
26-
// 403: z.object({
27-
// code: z.string()
28-
// })
29-
// }
30-
// })
8+
.use(openapi())
319
.post('/', ({ body }) => body, {
32-
body: t.Object({
33-
name: t.String()
10+
body: z.object({
11+
name: z.string()
3412
})
3513
})
3614
.get('/test', ({ status }) => {}, {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@elysiajs/openapi",
3-
"version": "1.4.11",
3+
"version": "1.4.12",
44
"description": "Plugin for Elysia to auto-generate API documentation",
55
"author": {
66
"name": "saltyAom",
@@ -85,7 +85,7 @@
8585
"openapi-types": "^12.1.3",
8686
"tsup": "^8.5.0",
8787
"typescript": "^5.9.2",
88-
"zod": "^4.1.5"
88+
"zod": "^4.2.1"
8989
},
9090
"peerDependencies": {
9191
"elysia": ">= 1.4.0"

src/openapi.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,15 @@ const unwrapResponseSchema = (
298298
? schema
299299
: // @ts-ignore
300300
schema['~standard']
301-
? unwrapSchema(schema as any, vendors)
301+
? unwrapSchema(schema as any, vendors, 'output')
302302
: Object.fromEntries(
303303
Object.entries(schema).map(([status, schema]) => [
304304
status,
305305
typeof schema === 'string'
306306
? normalizeSchemaReference(schema)
307307
: isTSchema(schema)
308308
? schema
309-
: unwrapSchema(schema as any, vendors)
309+
: unwrapSchema(schema as any, vendors, 'output')
310310
])
311311
)
312312

@@ -498,7 +498,8 @@ const unwrapReference = <T extends OpenAPIV3.SchemaObject | undefined>(
498498

499499
export const unwrapSchema = (
500500
schema: InputSchema['body'],
501-
mapJsonSchema?: MapJsonSchema
501+
mapJsonSchema?: MapJsonSchema,
502+
io: 'input' | 'output' = 'input'
502503
): OpenAPIV3.SchemaObject | undefined => {
503504
if (!schema) return
504505

@@ -511,6 +512,11 @@ export const unwrapSchema = (
511512
const vendor = schema['~standard'].vendor
512513

513514
try {
515+
// @ts-ignore
516+
if (schema['~standard']?.jsonSchema?.[io])
517+
// @ts-ignore
518+
return schema['~standard']?.jsonSchema?.[io]?.()
519+
514520
if (
515521
mapJsonSchema?.[vendor] &&
516522
typeof mapJsonSchema[vendor] === 'function'
@@ -954,7 +960,7 @@ export function toOpenAPISchema(
954960
!(hooks.response as any)['~standard']
955961
) {
956962
for (let [status, schema] of Object.entries(hooks.response)) {
957-
const response = unwrapSchema(schema, vendors)
963+
const response = unwrapSchema(schema, vendors, 'output')
958964

959965
if (!response) continue
960966

@@ -987,7 +993,7 @@ export function toOpenAPISchema(
987993
}
988994
}
989995
} else {
990-
const response = unwrapSchema(hooks.response as any, vendors)
996+
const response = unwrapSchema(hooks.response as any, vendors, 'output')
991997

992998
if (response) {
993999
// @ts-ignore

0 commit comments

Comments
 (0)