@@ -106,13 +106,23 @@ openapi({
106106
107107const 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
118128export 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