|
1 | 1 | /* eslint-disable @typescript-eslint/ban-ts-comment */ |
2 | 2 | /* eslint-disable @typescript-eslint/no-unused-vars */ |
3 | 3 | import { normalize } from 'pathe' |
4 | | -import type { HTTPMethod, LocalHook } from 'elysia' |
| 4 | +import { replaceSchemaType, t, type HTTPMethod, type LocalHook } from 'elysia' |
5 | 5 |
|
6 | 6 | import { Kind, type TSchema } from '@sinclair/typebox' |
7 | 7 | import type { OpenAPIV3 } from 'openapi-types' |
@@ -70,15 +70,39 @@ const mapTypesResponse = ( |
70 | 70 | const responses: Record<string, OpenAPIV3.MediaTypeObject> = {} |
71 | 71 |
|
72 | 72 | for (const type of types) { |
73 | | - // console.log(schema) |
74 | | - |
75 | 73 | responses[type] = { |
76 | 74 | schema: |
77 | 75 | typeof schema === 'string' |
78 | 76 | ? { |
79 | 77 | $ref: `#/components/schemas/${schema}` |
80 | 78 | } |
81 | | - : { ...(schema as any) } |
| 79 | + : '$ref' in schema && |
| 80 | + Kind in schema && |
| 81 | + schema[Kind] === 'Ref' |
| 82 | + ? { |
| 83 | + ...schema, |
| 84 | + $ref: `#/components/schemas/${schema.$ref}` |
| 85 | + } |
| 86 | + : replaceSchemaType( |
| 87 | + { ...(schema as any) }, |
| 88 | + { |
| 89 | + from: t.Ref(''), |
| 90 | + // @ts-expect-error |
| 91 | + to: ({ $ref, ...options }) => { |
| 92 | + if ( |
| 93 | + !$ref.startsWith( |
| 94 | + '#/components/schemas/' |
| 95 | + ) |
| 96 | + ) |
| 97 | + return t.Ref( |
| 98 | + `#/components/schemas/${$ref}`, |
| 99 | + options |
| 100 | + ) |
| 101 | + |
| 102 | + return t.Ref($ref, options) |
| 103 | + } |
| 104 | + } |
| 105 | + ) |
82 | 106 | } |
83 | 107 | } |
84 | 108 |
|
@@ -154,6 +178,7 @@ export const registerSchemaPath = ({ |
154 | 178 | required, |
155 | 179 | additionalProperties, |
156 | 180 | patternProperties, |
| 181 | + $ref, |
157 | 182 | ...rest |
158 | 183 | } = responseSchema as typeof responseSchema & { |
159 | 184 | type: string |
@@ -246,6 +271,7 @@ export const registerSchemaPath = ({ |
246 | 271 | type, |
247 | 272 | properties, |
248 | 273 | required, |
| 274 | + $ref, |
249 | 275 | additionalProperties: _1, |
250 | 276 | patternProperties: _2, |
251 | 277 | ...rest |
|
0 commit comments