11import { OpenAPIV3 } from "openapi-types" ;
2- import { ALLOWED_METHODS } from "../const/openapi.const" ;
2+ import { ALLOWED_METHODS , COMPOSITE_KEYWORDS } from "../const/openapi.const" ;
33import { GenerateOptions } from "../types/options" ;
44import { pick } from "../utils/object.utils" ;
55import {
@@ -155,24 +155,6 @@ export class SchemaResolver {
155155 }
156156
157157 private initializeSchemaTags ( ) {
158- const getSchemaRefs = (
159- schema : OpenAPIV3 . ReferenceObject | OpenAPIV3 . SchemaObject | undefined ,
160- ) : OpenAPIV3 . ReferenceObject [ ] => {
161- const schemaRefObjs : OpenAPIV3 . ReferenceObject [ ] = [ ] ;
162- if ( ! schema ) {
163- return schemaRefObjs ;
164- }
165-
166- if ( isReferenceObject ( schema ) ) {
167- schemaRefObjs . push ( schema ) ;
168- } else if ( schema . allOf || schema . anyOf || schema . oneOf ) {
169- const schemaObjs = schema . allOf ?? schema . anyOf ?? schema . oneOf ?? [ ] ;
170- schemaObjs . forEach ( ( schema ) => schemaRefObjs . push ( ...getSchemaRefs ( schema ) ) ) ;
171- }
172-
173- return schemaRefObjs ;
174- } ;
175-
176158 for ( const path in this . openApiDoc . paths ) {
177159 const pathItemObj = this . openApiDoc . paths [ path ] as OpenAPIV3 . PathItemObject ;
178160
@@ -184,29 +166,30 @@ export class SchemaResolver {
184166 continue ;
185167 }
186168
187- // Collect all parameter objects that are references
188169 const schemaRefObjs = [ ] as OpenAPIV3 . ReferenceObject [ ] ;
170+
171+ // Collect all referenced schemas in parameter objects
189172 operation . parameters ?. map ( ( param ) => {
190- schemaRefObjs . push ( ...getSchemaRefs ( ( param as OpenAPIV3 . ParameterObject ) . schema ) ) ;
173+ schemaRefObjs . push ( ...this . getOperationSchemaRefs ( ( param as OpenAPIV3 . ParameterObject ) . schema ) ) ;
191174 } ) ;
192175
193- // Collect all requestBody objects that are references
176+ // Collect all referenced schemas in requestBody objects
194177 if ( operation . requestBody ) {
195178 const requestBodyObj = this . resolveObject ( operation . requestBody ) ;
196179 const mediaTypes = Object . keys ( requestBodyObj . content ?? { } ) ;
197180 const matchingMediaType = mediaTypes . find ( isParamMediaTypeAllowed ) ;
198181 if ( matchingMediaType ) {
199- schemaRefObjs . push ( ...getSchemaRefs ( requestBodyObj . content ?. [ matchingMediaType ] ?. schema ) ) ;
182+ schemaRefObjs . push ( ...this . getOperationSchemaRefs ( requestBodyObj . content ?. [ matchingMediaType ] ?. schema ) ) ;
200183 }
201184 }
202185
203- // Collect all main response objects that are references
186+ // Collect all referenced schemas in main response objects
204187 for ( const statusCode in operation . responses ) {
205188 const responseObj = < OpenAPIV3 . ResponseObject > this . resolveObject ( operation . responses [ statusCode ] ) ;
206189 const mediaTypes = Object . keys ( responseObj . content ?? { } ) ;
207190 const matchingMediaType = mediaTypes . find ( isMediaTypeAllowed ) ;
208191 if ( matchingMediaType ) {
209- schemaRefObjs . push ( ...getSchemaRefs ( responseObj . content ?. [ matchingMediaType ] ?. schema ) ) ;
192+ schemaRefObjs . push ( ...this . getOperationSchemaRefs ( responseObj . content ?. [ matchingMediaType ] ?. schema ) ) ;
210193 }
211194 }
212195
@@ -228,4 +211,34 @@ export class SchemaResolver {
228211 }
229212 }
230213 }
214+
215+ private getOperationSchemaRefs (
216+ schema : OpenAPIV3 . ReferenceObject | OpenAPIV3 . SchemaObject | undefined ,
217+ ) : OpenAPIV3 . ReferenceObject [ ] {
218+ if ( ! schema ) {
219+ return [ ] ;
220+ }
221+
222+ const schemaRefObjs : OpenAPIV3 . ReferenceObject [ ] = [ ] ;
223+
224+ if ( isReferenceObject ( schema ) ) {
225+ schemaRefObjs . push ( schema ) ;
226+ }
227+
228+ const schemaObj = schema as OpenAPIV3 . SchemaObject ;
229+ if ( COMPOSITE_KEYWORDS . some ( ( prop ) => prop in schemaObj && schemaObj [ prop ] ) ) {
230+ const schemaObjs = schemaObj . allOf ?? schemaObj . anyOf ?? schemaObj . oneOf ?? [ ] ;
231+ schemaObjs . forEach ( ( schema ) => schemaRefObjs . push ( ...this . getOperationSchemaRefs ( schema ) ) ) ;
232+ }
233+ if ( schemaObj . properties ) {
234+ Object . values ( schemaObj . properties ) . forEach ( ( schema ) =>
235+ schemaRefObjs . push ( ...this . getOperationSchemaRefs ( schema ) ) ,
236+ ) ;
237+ }
238+ if ( schemaObj . type === "array" ) {
239+ schemaRefObjs . push ( ...this . getOperationSchemaRefs ( ( schema as OpenAPIV3 . ArraySchemaObject ) . items ) ) ;
240+ }
241+
242+ return schemaRefObjs ;
243+ }
231244}
0 commit comments