@@ -11,6 +11,7 @@ import {
1111 getTypeFromSchema ,
1212} from './generate-schema'
1313import {
14+ collectSchemaNames ,
1415 extractSchemaNameFromRef ,
1516 getRequestBodyContent ,
1617 isErrorStatusCode ,
@@ -145,73 +146,10 @@ function generateSchemaInterface(
145146 } as const
146147 const convertCaseType = options ?. convertCase ?? 'camel'
147148
148- // Helper function to collect schema names from a schema object
149- // Recursively traverses into referenced schemas to find all nested $refs
150- const collectSchemaNames = (
151- schemaObj : OpenAPIV3_1 . SchemaObject | OpenAPIV3_1 . ReferenceObject ,
152- targetSet : Set < string > ,
153- visited : Set < string > = new Set ( ) ,
154- ) : void => {
155- if ( '$ref' in schemaObj ) {
156- const schemaName = extractSchemaNameFromRef ( schemaObj . $ref )
157- if ( schemaName ) {
158- // Avoid infinite recursion for circular references
159- if ( visited . has ( schemaName ) ) {
160- return
161- }
162- targetSet . add ( schemaName )
163- visited . add ( schemaName )
164-
165- // Recursively collect from the referenced schema
166- const referencedSchema = schema . components ?. schemas ?. [ schemaName ]
167- if ( referencedSchema ) {
168- collectSchemaNames (
169- referencedSchema as
170- | OpenAPIV3_1 . SchemaObject
171- | OpenAPIV3_1 . ReferenceObject ,
172- targetSet ,
173- visited ,
174- )
175- }
176- }
177- return
178- }
179-
180- const schemaObjTyped = schemaObj as OpenAPIV3_1 . SchemaObject
181-
182- // Check allOf, anyOf, oneOf
183- if ( schemaObjTyped . allOf ) {
184- schemaObjTyped . allOf . forEach ( ( s ) => {
185- collectSchemaNames ( s , targetSet , visited )
186- } )
187- }
188- if ( schemaObjTyped . anyOf ) {
189- schemaObjTyped . anyOf . forEach ( ( s ) => {
190- collectSchemaNames ( s , targetSet , visited )
191- } )
192- }
193- if ( schemaObjTyped . oneOf ) {
194- schemaObjTyped . oneOf . forEach ( ( s ) => {
195- collectSchemaNames ( s , targetSet , visited )
196- } )
197- }
198-
199- // Check properties
200- if ( schemaObjTyped . properties ) {
201- Object . values ( schemaObjTyped . properties ) . forEach ( ( prop ) => {
202- collectSchemaNames ( prop , targetSet , visited )
203- } )
204- }
205-
206- // Check items (for arrays)
207- if (
208- schemaObjTyped . type === 'array' &&
209- 'items' in schemaObjTyped &&
210- schemaObjTyped . items
211- ) {
212- collectSchemaNames ( schemaObjTyped . items , targetSet , visited )
213- }
214- }
149+ const collectOpts = {
150+ followComponentRefs : true ,
151+ document : schema ,
152+ } as const
215153
216154 // Track which schemas are used in request body and responses
217155 const requestSchemaNames = new Set < string > ( )
@@ -242,7 +180,11 @@ function generateSchemaInterface(
242180 const content = operation . requestBody . content
243181 const bodyContent = getRequestBodyContent ( content )
244182 if ( bodyContent && 'schema' in bodyContent && bodyContent . schema ) {
245- collectSchemaNames ( bodyContent . schema , requestSchemaNames )
183+ collectSchemaNames (
184+ bodyContent . schema ,
185+ requestSchemaNames ,
186+ collectOpts ,
187+ )
246188 }
247189 }
248190 }
@@ -272,9 +214,17 @@ function generateSchemaInterface(
272214 jsonContent . schema
273215 ) {
274216 if ( isError ) {
275- collectSchemaNames ( jsonContent . schema , errorSchemaNames )
217+ collectSchemaNames (
218+ jsonContent . schema ,
219+ errorSchemaNames ,
220+ collectOpts ,
221+ )
276222 } else {
277- collectSchemaNames ( jsonContent . schema , responseSchemaNames )
223+ collectSchemaNames (
224+ jsonContent . schema ,
225+ responseSchemaNames ,
226+ collectOpts ,
227+ )
278228 }
279229 }
280230 }
0 commit comments