@@ -15,12 +15,31 @@ type Config = Options & {
15
15
} ;
16
16
17
17
export const generateCustomSpec = async ( config : Config ) => {
18
+ const getNameFromRef = ( ref : string ) : string => {
19
+ return ref . split ( '/' ) . slice ( - 1 ) [ 0 ] ;
20
+ } ;
21
+
18
22
const getSchemaRefFromContent = ( content : OpenApiMediaType ) : string => {
19
23
let ref : string = '' ;
20
24
21
25
ref = content . $ref || content . schema ?. $ref || content . schema ?. items ?. $ref || '' ;
22
26
23
- return ref . split ( '/' ) . slice ( - 1 ) [ 0 ] ;
27
+ return getNameFromRef ( ref ) ;
28
+ } ;
29
+
30
+ const recursiveAddAllUnknownModels = ( modelName : string ) : void => {
31
+ const model = list . components ?. schemas ? list . components . schemas [ modelName ] : undefined ;
32
+ if ( model === undefined ) return ;
33
+
34
+ for ( const property in model . properties ) {
35
+ const ref = model . properties [ property ] . $ref || model . properties [ property ] . items ?. $ref || '' ;
36
+ const modelName = getNameFromRef ( ref ) ;
37
+
38
+ if ( ! requiredSchemasSet . has ( modelName ) ) {
39
+ requiredSchemasSet . add ( modelName ) ;
40
+ recursiveAddAllUnknownModels ( modelName ) ;
41
+ }
42
+ }
24
43
} ;
25
44
26
45
const list : OpenApi = await getOpenApiSpec ( config . input ) ;
@@ -57,9 +76,12 @@ export const generateCustomSpec = async (config: Config) => {
57
76
if ( ! ( 'url' in requestMethodData ) ) {
58
77
if ( 'parameters' in requestMethodData ) {
59
78
// add schemas from {apiPath}/{method}/parameters
60
- requestMethodData . parameters ?. forEach ( parameter =>
61
- requiredSchemasSet . add ( getSchemaRefFromContent ( parameter ) )
62
- ) ;
79
+ requestMethodData . parameters ?. forEach ( parameter => {
80
+ const modelName = getSchemaRefFromContent ( parameter ) ;
81
+
82
+ requiredSchemasSet . add ( modelName ) ;
83
+ recursiveAddAllUnknownModels ( modelName ) ;
84
+ } ) ;
63
85
}
64
86
if ( 'responses' in requestMethodData ) {
65
87
const responsesCodeData = Object . values ( requestMethodData . responses ) ;
@@ -69,7 +91,10 @@ export const generateCustomSpec = async (config: Config) => {
69
91
70
92
// add schemas from {apiPath}/{method}/responses/{responseType}/content
71
93
contentTypeData . forEach ( content => {
94
+ const modelName = getSchemaRefFromContent ( content ) ;
95
+
72
96
requiredSchemasSet . add ( getSchemaRefFromContent ( content ) ) ;
97
+ recursiveAddAllUnknownModels ( modelName ) ;
73
98
} ) ;
74
99
} ) ;
75
100
}
0 commit comments