Skip to content

Commit ef764db

Browse files
committed
🔧 fix: reference model
1 parent 128cff3 commit ef764db

File tree

5 files changed

+57
-124
lines changed

5 files changed

+57
-124
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# 1.4.5 - 20 Sep 2025
2+
Improvement:
3+
- reference model now handle content type correctly
4+
- type doesn't show up when body is primitive type
5+
6+
Bug fix:
7+
- remove unintentional console.log
8+
- reference model doesn't show up when using as response
9+
110
# 1.4.4 - 20 Sep 2025
211
Improvement:
312
- cast `exclude.methods` to lowercase when checking for method exclusion

example/gen.d.ts

Lines changed: 0 additions & 106 deletions
This file was deleted.

example/gen.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export const app = new Elysia()
1010
})
1111
})
1212
)
13+
.model({
14+
'character.name': t.String(),
15+
'character.thing': t.Object({
16+
name: t.String()
17+
})
18+
})
1319
.get(
1420
'/',
1521
() =>
@@ -40,5 +46,16 @@ export const app = new Elysia()
4046
}
4147
)
4248
.get('/id/:id/name/:name', ({ params }) => params)
43-
.get('/a', () => 'hello')
49+
.post(
50+
'/character',
51+
() => ({
52+
name: 'Elysia'
53+
}),
54+
{
55+
body: 'character.name',
56+
response: {
57+
200: 'character.thing'
58+
}
59+
}
60+
)
4461
.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.4",
3+
"version": "1.4.5",
44
"description": "Plugin for Elysia to auto-generate API documentation",
55
"author": {
66
"name": "saltyAom",

src/openapi.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,23 @@ openapi({
106106

107107
const warned = {} as Record<keyof typeof warnings, boolean | undefined>
108108

109-
const unwrapReference = (schema: any, definitions: Record<string, unknown>) => {
110-
if (!schema?.$ref) return schema
109+
const unwrapReference = <T extends OpenAPIV3.SchemaObject | undefined>(
110+
schema: T,
111+
definitions: Record<string, unknown>
112+
):
113+
| Exclude<T, OpenAPIV3.SchemaObject>
114+
| (Omit<NonNullable<T>, 'type'> & {
115+
$ref: string
116+
type: string | undefined
117+
}) => {
118+
// @ts-ignore
119+
const ref = schema?.$ref
120+
if (!ref) return schema as any
111121

112-
const name = schema.$ref.slice(schema.$ref.lastIndexOf('/') + 1)
113-
if (schema.$ref && definitions[name]) schema = definitions[name]
122+
const name = ref.slice(ref.lastIndexOf('/') + 1)
123+
if (ref && definitions[name]) schema = definitions[name] as T
114124

115-
return schema
125+
return schema as any
116126
}
117127

118128
export const unwrapSchema = (
@@ -252,10 +262,6 @@ export function toOpenAPISchema(
252262
detail: Partial<OpenAPIV3.OperationObject>
253263
} = route.hooks ?? {}
254264

255-
if (route.path === '/a') {
256-
console.log('H')
257-
}
258-
259265
if (references?.length)
260266
for (const reference of references as AdditionalReference[]) {
261267
if (!reference) continue
@@ -404,8 +410,10 @@ export function toOpenAPISchema(
404410

405411
if (body) {
406412
// @ts-ignore
407-
const { type: _type, description, ...options } = body
408-
const type = _type as string | undefined
413+
const { type, description, $ref, ...options } = unwrapReference(
414+
body,
415+
definitions
416+
)
409417

410418
// @ts-ignore
411419
if (hooks.parse) {
@@ -461,7 +469,9 @@ export function toOpenAPISchema(
461469
type === 'integer' ||
462470
type === 'boolean'
463471
? {
464-
'text/plain': body
472+
'text/plain': {
473+
schema: body
474+
}
465475
}
466476
: {
467477
'application/json': {
@@ -495,13 +505,12 @@ export function toOpenAPISchema(
495505
if (!response) continue
496506

497507
// @ts-ignore Must exclude $ref from root options
498-
const { type: _type, description, ...options } = response
499-
const type = _type as string | undefined
508+
const { type, description, $ref, ...options } =
509+
unwrapReference(response, definitions)
500510

501511
operation.responses[status] = {
502512
description:
503513
description ?? `Response for status ${status}`,
504-
...options,
505514
content:
506515
type === 'void' ||
507516
type === 'null' ||
@@ -528,7 +537,11 @@ export function toOpenAPISchema(
528537

529538
if (response) {
530539
// @ts-ignore
531-
const { type: _type, description, ...options } = response
540+
const {
541+
type: _type,
542+
description,
543+
...options
544+
} = unwrapReference(response, definitions)
532545
const type = _type as string | undefined
533546

534547
// It's a single schema, default to 200

0 commit comments

Comments
 (0)