@@ -106,13 +106,23 @@ openapi({
106
106
107
107
const warned = { } as Record < keyof typeof warnings , boolean | undefined >
108
108
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
111
121
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
114
124
115
- return schema
125
+ return schema as any
116
126
}
117
127
118
128
export const unwrapSchema = (
@@ -252,10 +262,6 @@ export function toOpenAPISchema(
252
262
detail : Partial < OpenAPIV3 . OperationObject >
253
263
} = route . hooks ?? { }
254
264
255
- if ( route . path === '/a' ) {
256
- console . log ( 'H' )
257
- }
258
-
259
265
if ( references ?. length )
260
266
for ( const reference of references as AdditionalReference [ ] ) {
261
267
if ( ! reference ) continue
@@ -404,8 +410,10 @@ export function toOpenAPISchema(
404
410
405
411
if ( body ) {
406
412
// @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
+ )
409
417
410
418
// @ts -ignore
411
419
if ( hooks . parse ) {
@@ -461,7 +469,9 @@ export function toOpenAPISchema(
461
469
type === 'integer' ||
462
470
type === 'boolean'
463
471
? {
464
- 'text/plain' : body
472
+ 'text/plain' : {
473
+ schema : body
474
+ }
465
475
}
466
476
: {
467
477
'application/json' : {
@@ -495,13 +505,12 @@ export function toOpenAPISchema(
495
505
if ( ! response ) continue
496
506
497
507
// @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 )
500
510
501
511
operation . responses [ status ] = {
502
512
description :
503
513
description ?? `Response for status ${ status } ` ,
504
- ...options ,
505
514
content :
506
515
type === 'void' ||
507
516
type === 'null' ||
@@ -528,7 +537,11 @@ export function toOpenAPISchema(
528
537
529
538
if ( response ) {
530
539
// @ts -ignore
531
- const { type : _type , description, ...options } = response
540
+ const {
541
+ type : _type ,
542
+ description,
543
+ ...options
544
+ } = unwrapReference ( response , definitions )
532
545
const type = _type as string | undefined
533
546
534
547
// It's a single schema, default to 200
0 commit comments