1+ import SwaggerParser , { type resolve } from "@apidevtools/swagger-parser" ;
12import { consola } from "consola" ;
23import lodash from "lodash" ;
34import * as typescript from "typescript" ;
@@ -46,9 +47,12 @@ export class CodeGenProcess {
4647 templatesWorker : TemplatesWorker ;
4748 schemaWalker : SchemaWalker ;
4849 javascriptTranslator : JavascriptTranslator ;
50+ swaggerParser : SwaggerParser ;
51+ swaggerRefs : Awaited < ReturnType < typeof resolve > > | undefined | null ;
4952
5053 constructor ( config : Partial < GenerateApiConfiguration [ "config" ] > ) {
5154 this . config = new CodeGenConfig ( config ) ;
55+ this . swaggerParser = new SwaggerParser ( ) ;
5256 this . fileSystem = new FileSystem ( ) ;
5357 this . swaggerSchemaResolver = new SwaggerSchemaResolver (
5458 this . config ,
@@ -58,7 +62,7 @@ export class CodeGenProcess {
5862 this . config ,
5963 this . swaggerSchemaResolver ,
6064 ) ;
61- this . schemaComponentsMap = new SchemaComponentsMap ( this . config ) ;
65+ this . schemaComponentsMap = new SchemaComponentsMap ( this . config , this ) ;
6266 this . typeNameFormatter = new TypeNameFormatter ( this . config ) ;
6367 this . templatesWorker = new TemplatesWorker (
6468 this . config ,
@@ -75,6 +79,7 @@ export class CodeGenProcess {
7579 ) ;
7680 this . schemaRoutes = new SchemaRoutes (
7781 this . config ,
82+ this ,
7883 this . schemaParserFabric ,
7984 this . schemaComponentsMap ,
8085 this . templatesWorker ,
@@ -98,6 +103,42 @@ export class CodeGenProcess {
98103
99104 this . swaggerSchemaResolver . fixSwaggerSchema ( swagger ) ;
100105
106+ try {
107+ this . swaggerRefs = await this . swaggerParser . resolve (
108+ this . config . url || this . config . input || ( this . config . spec as any ) ,
109+ {
110+ continueOnError : true ,
111+ mutateInputSchema : true ,
112+ validate : {
113+ schema : false ,
114+ spec : false ,
115+ } ,
116+ resolve : {
117+ external : true ,
118+ http : {
119+ ...this . config . requestOptions ,
120+ headers : Object . assign (
121+ { } ,
122+ this . config . authorizationToken
123+ ? {
124+ Authorization : this . config . authorizationToken ,
125+ }
126+ : { } ,
127+ this . config . requestOptions ?. headers ?? { } ,
128+ ) ,
129+ } ,
130+ } ,
131+ } ,
132+ ) ;
133+ this . swaggerRefs . set ( "fixed-swagger-schema" , swagger . usageSchema as any ) ;
134+ this . swaggerRefs . set (
135+ "original-swagger-schema" ,
136+ swagger . originalSchema as any ,
137+ ) ;
138+ } catch ( e ) {
139+ consola . error ( e ) ;
140+ }
141+
101142 this . config . update ( {
102143 swaggerSchema : swagger . usageSchema ,
103144 originalSchema : swagger . originalSchema ,
0 commit comments