Skip to content

Commit 30bcb88

Browse files
committed
🔧 fix(type generator): try loose matching for schema type
1 parent 0e21f06 commit 30bcb88

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 1.3.6 - 3 Sep 2025
22
Improvement:
33
- type generator: add loose path type matching
4+
- type generator: try loose matching for schema type
45

56
# 1.3.5 - 3 Sep 2025
67
Bug fix:

example/gen.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ export const app = new Elysia()
2828
}
2929
}
3030
)
31-
.post(
32-
'/json',
33-
({ body, status }) => (Math.random() > 0.5 ? status(418) : body),
34-
{
35-
body: t.Object({
36-
hello: t.String()
37-
})
38-
}
39-
)
40-
.get('/id/:id/name/:name', ({ params }) => params)
31+
// .post(
32+
// '/json',
33+
// ({ body, status }) => (Math.random() > 0.5 ? status(418) : body),
34+
// {
35+
// body: t.Object({
36+
// hello: t.String()
37+
// })
38+
// }
39+
// )
40+
// .get('/id/:id/name/:name', ({ params }) => params)
4141
.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.3.5",
3+
"version": "1.3.6",
44
"description": "Plugin for Elysia to auto-generate API documentation",
55
"author": {
66
"name": "saltyAom",

src/gen/index.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { InternalRoute } from 'elysia'
1+
import type { InputSchema, InternalRoute, TSchema } from 'elysia'
22
import {
33
readFileSync,
44
mkdirSync,
@@ -14,6 +14,7 @@ import { tmpdir } from 'os'
1414
import { join } from 'path'
1515
import { spawnSync } from 'child_process'
1616
import { AdditionalReference, AdditionalReferences } from '../types'
17+
import { Kind, TObject } from '@sinclair/typebox/type'
1718

1819
const matchRoute = /: Elysia<(.*)>/gs
1920
const matchStatus = /(\d{3}):/gs
@@ -165,17 +166,24 @@ export const fromTypes =
165166

166167
const routes: AdditionalReference = {}
167168

168-
for (let route of routesString.slice(1).split('} & {')) {
169-
route = '{' + route + '}'
170-
let schema = TypeBox(route)
171-
172-
if (schema.type !== 'object') continue
169+
// Treaty is a collection of { ... } & { ... } & { ... }
170+
// Each route will be intersected with each other
171+
// instead of being nested in a route object
172+
for (const route of routesString.slice(1).split('} & {')) {
173+
// as '} & {' is removed, we need to add it back
174+
let schema = TypeBox(`{${route}}}`)
175+
if (schema.type !== 'object') {
176+
// just in case
177+
schema = TypeBox(`{${route}}`)
178+
179+
if (schema.type !== 'object') continue
180+
}
173181

174182
const paths = []
175183

176184
while (true) {
177185
const keys = Object.keys(schema.properties)
178-
if (!keys.length || keys.length > 1) break
186+
if (keys.length !== 1) break
179187

180188
paths.push(keys[0])
181189

0 commit comments

Comments
 (0)