@@ -2,7 +2,7 @@ import { t, type AnyElysia, type TSchema, type InputSchema } from 'elysia'
22import type { HookContainer } from 'elysia/types'
33
44import type { OpenAPIV3 } from 'openapi-types'
5- import type { TProperties } from '@sinclair/typebox'
5+ import { Kind , type TProperties } from '@sinclair/typebox'
66
77import type {
88 AdditionalReference ,
@@ -53,6 +53,13 @@ export const getPossiblePath = (path: string): string[] => {
5353 return paths
5454}
5555
56+ const isValidSchema = ( schema : any ) : schema is TSchema =>
57+ typeof schema === 'object' &&
58+ ( ( Kind in schema && schema [ Kind ] !== 'Unknown' ) ||
59+ schema . type ||
60+ schema . properties ||
61+ schema . items )
62+
5663/**
5764 * Converts Elysia routes to OpenAPI 3.0.3 paths schema
5865 * @param routes Array of Elysia route objects
@@ -111,21 +118,33 @@ export function toOpenAPISchema(
111118 const refer = reference [ route . path ] ?. [ method ]
112119 if ( ! refer ) continue
113120
114- if ( ! hooks . body && refer . body ?. type ) hooks . body = refer . body
115- if ( ! hooks . query && refer . query ?. type ) hooks . query = refer . query
116- if ( ! hooks . params && refer . params ?. type )
121+ if ( ! hooks . body && isValidSchema ( refer . body ) )
122+ hooks . body = refer . body
123+
124+ if ( ! hooks . query && isValidSchema ( refer . query ) )
125+ hooks . query = refer . query
126+
127+ if ( ! hooks . params && isValidSchema ( refer . params ) )
117128 hooks . params = refer . params
118- if ( ! hooks . headers && refer . headers ?. type )
129+
130+ if ( ! hooks . headers && isValidSchema ( refer . headers ) )
119131 hooks . headers = refer . headers
120- if ( ! hooks . response && refer . response ) {
121- hooks . response = { }
122132
133+ if ( refer . response )
123134 for ( const [ status , schema ] of Object . entries (
124135 refer . response
125136 ) )
126- if ( ! hooks . response [ status as any ] && schema ?. type )
127- hooks . response [ status as any ] = schema
128- }
137+ if ( isValidSchema ( schema ) ) {
138+ if ( ! hooks . response ) hooks . response = { }
139+
140+ if (
141+ ! hooks . response [
142+ status as keyof ( typeof hooks ) [ 'response' ]
143+ ]
144+ )
145+ // @ts -ignore
146+ hooks . response [ status ] = schema
147+ }
129148 }
130149
131150 if (
0 commit comments