@@ -12,7 +12,7 @@ import {
12
12
Response ,
13
13
} from './openapi.types' ;
14
14
import { OpenAPIOptions } from './types' ;
15
- import { createPathTemplateLiteral , applyJsonPatch } from './utils' ;
15
+ import { createPathTemplateLiteral , applyJsonPatch , resolveMaybeRef } from './utils' ;
16
16
17
17
/**
18
18
includes: {
@@ -106,12 +106,13 @@ export const getApiTypeNameSafe = (
106
106
107
107
export const getOperationReturnType = (
108
108
options : OpenAPIOptions ,
109
+ schema : OpenAPISpec ,
109
110
operation : Operation ,
110
111
method : string
111
112
) => {
112
113
if ( operation . responses ) {
113
114
if ( operation . responses [ '200' ] ) {
114
- const prop = operation . responses [ '200' ] ;
115
+ const prop = resolveMaybeRef < Response > ( schema , operation . responses [ '200' ] as any ) ;
115
116
return getResponseType ( options , prop ) ;
116
117
}
117
118
}
@@ -218,42 +219,26 @@ const initParams = (): ParameterInterfaces => {
218
219
} ;
219
220
} ;
220
221
221
- // Resolve a parameter $ref against the spec's global parameters collection
222
- function resolveParameterRef (
223
- schema : OpenAPISpec ,
224
- param : any
225
- ) : Parameter {
226
- if ( param && typeof param === 'object' && ( param as any ) . $ref ) {
227
- const ref : string = ( param as any ) . $ref ;
228
- if ( ref . includes ( '/parameters/' ) ) {
229
- const parts = ref . split ( '/' ) ;
230
- const name = parts [ parts . length - 1 ] ;
231
- const resolved = schema . parameters ?. [ name ] ;
232
- if ( resolved ) return resolved as Parameter ;
233
- }
234
- }
235
- return param as Parameter ;
236
- }
237
-
238
222
export function generateOpenApiParams (
239
223
options : OpenAPIOptions ,
240
224
schema : OpenAPISpec ,
241
225
path : string ,
242
226
pathItem : OpenAPIPathItem
243
227
) : t . TSInterfaceDeclaration [ ] {
228
+ const resolvedPathItem = resolveMaybeRef < OpenAPIPathItem > ( schema , pathItem as any ) ;
244
229
const opParams : OpParameterInterfaces = getOpenApiParams (
245
230
options ,
246
231
schema ,
247
232
path ,
248
- pathItem
233
+ resolvedPathItem
249
234
) ;
250
235
const interfaces : t . TSInterfaceDeclaration [ ] = [ ] ;
251
236
[ 'get' , 'post' , 'put' , 'delete' , 'options' , 'head' , 'patch' ] . forEach (
252
237
( method ) => {
253
- if ( Object . prototype . hasOwnProperty . call ( pathItem , method ) ) {
238
+ if ( Object . prototype . hasOwnProperty . call ( resolvedPathItem , method ) ) {
254
239
// @ts -ignore
255
- const operation : Operation = pathItem [ method ] ;
256
- if ( ! shouldIncludeOperation ( options , pathItem , path , method as any ) )
240
+ const operation : Operation = ( resolvedPathItem as any ) [ method ] ;
241
+ if ( ! shouldIncludeOperation ( options , resolvedPathItem , path , method as any ) )
257
242
return ;
258
243
259
244
// @ts -ignore
@@ -352,7 +337,7 @@ export function getOpenApiParams(
352
337
// BEGIN SANITIZE PARAMS
353
338
pathItem . parameters = pathItem . parameters ?? [ ] ;
354
339
const resolvedPathParams = pathItem . parameters . map ( ( p ) =>
355
- resolveParameterRef ( schema , p as any )
340
+ resolveMaybeRef < Parameter > ( schema , p as any )
356
341
) ;
357
342
const pathParms = resolvedPathParams . filter ( ( param ) => param . in === 'path' ) ?? [ ] ;
358
343
if ( pathParms . length !== pathInfo . params . length ) {
@@ -377,7 +362,7 @@ export function getOpenApiParams(
377
362
378
363
// load Path-Level params
379
364
pathItem . parameters . forEach ( ( param ) => {
380
- const resolved = resolveParameterRef ( schema , param as any ) ;
365
+ const resolved = resolveMaybeRef < Parameter > ( schema , param as any ) ;
381
366
opParams . pathLevel [ resolved . in ] . push ( resolved ) ;
382
367
} ) ;
383
368
@@ -401,7 +386,7 @@ export function getOpenApiParams(
401
386
if ( operation . parameters ) {
402
387
// Categorize parameters by 'in' field
403
388
operation . parameters . forEach ( ( param ) => {
404
- const resolved = resolveParameterRef ( schema , param as any ) ;
389
+ const resolved = resolveMaybeRef < Parameter > ( schema , param as any ) ;
405
390
opParamMethod [ resolved . in ] . push ( resolved ) ;
406
391
} ) ;
407
392
}
@@ -463,6 +448,7 @@ const getOperationTypeName = (
463
448
464
449
export const createOperation = (
465
450
options : OpenAPIOptions ,
451
+ schema : OpenAPISpec ,
466
452
operation : Operation ,
467
453
path : string ,
468
454
method : string ,
@@ -493,7 +479,7 @@ export const createOperation = (
493
479
( param ) => param . in === 'query'
494
480
) ;
495
481
496
- const returnType = getOperationReturnType ( options , operation , method ) ;
482
+ const returnType = getOperationReturnType ( options , schema , operation , method ) ;
497
483
const methodName = getOperationMethodName ( options , operation , method , path ) ;
498
484
499
485
const callMethod = t . callExpression (
@@ -559,10 +545,10 @@ export function generateMethods(
559
545
560
546
if ( alias ) {
561
547
methods . push (
562
- createOperation ( options , operation , path , method , alias )
548
+ createOperation ( options , schema , operation , path , method , alias )
563
549
) ;
564
550
}
565
- methods . push ( createOperation ( options , operation , path , method ) ) ;
551
+ methods . push ( createOperation ( options , schema , operation , path , method ) ) ;
566
552
} ) ;
567
553
} ) ;
568
554
@@ -709,7 +695,7 @@ export function collectReactQueryHookComponents(
709
695
) ,
710
696
] ) ;
711
697
const requestTypeName = getOperationTypeName ( options , operation , method , path ) + 'Request' ;
712
- const returnTypeAST = getOperationReturnType ( options , operation , method ) ;
698
+ const returnTypeAST = getOperationReturnType ( options , schema , operation , method ) ;
713
699
const methodName = opMethodName ;
714
700
715
701
const importDecls : t . ImportDeclaration [ ] = [
0 commit comments