1
1
import { generate , Options } from './generate' ;
2
2
import { OpenApi } from './openApi/v3/interfaces/OpenApi' ;
3
+ import { OpenApiOperation } from './openApi/v3/interfaces/OpenApiOperation' ;
4
+ import { OpenApiParameter } from './openApi/v3/interfaces/OpenApiParameter' ;
3
5
import { OpenApiSchema } from './openApi/v3/interfaces/OpenApiSchema' ;
6
+ import { OpenApiServer } from './openApi/v3/interfaces/OpenApiServer' ;
4
7
import { getOpenApiSpec } from './utils/getOpenApiSpec' ;
5
8
import { isString } from './utils/isString' ;
6
9
import { mapSwaggerRef } from './utils/mapSwaggerRef' ;
7
10
import { removeLodashPrefix } from './utils/removeLodashPrefix' ;
8
11
import { removeLodashPrefixFromRef } from './utils/removeLodashPrefixFromRef' ;
12
+ import { getSchemaRefFromContent } from './utils/saddleback/getSchemaRefFromContent' ;
9
13
import { getSwaggerJsonByEnv } from './utils/saddleback/getSwaggerJsonByEnv' ;
10
14
import { Environment , Service } from './utils/saddleback/getUrlByServiceEnv' ;
15
+ import { recursiveAddAllUnknownModels } from './utils/saddleback/recursiveAddAllUnknownModels' ;
11
16
import { Dictionary } from './utils/types' ;
12
17
13
18
type Config = Options & {
@@ -19,6 +24,8 @@ type Config = Options & {
19
24
password : string ;
20
25
useEnvironment ?: Environment ;
21
26
useService ?: Service ;
27
+ filterMethod : 'include' | 'exclude' ;
28
+ filterArray : string [ ] ;
22
29
} ;
23
30
24
31
export const generateSaddlebackSpec = async ( config : Config ) => {
@@ -48,7 +55,95 @@ export const generateSaddlebackSpec = async (config: Config) => {
48
55
49
56
mapSwaggerRef ( openApi , removeLodashPrefixFromRef ) ;
50
57
51
- await generate ( { ...config , input : openApi } ) ;
58
+ const list : OpenApi = openApi ;
59
+
60
+ const requiredPaths : OpenApi [ 'paths' ] = { } ;
61
+
62
+ for ( const path in list . paths ) {
63
+ if ( ! list . paths . hasOwnProperty ( path ) ) return ;
64
+
65
+ if ( config . filterMethod === 'include' ) {
66
+ if ( config . filterArray . some ( it => it === path ) ) requiredPaths [ path ] = list . paths [ path ] ;
67
+ }
68
+ if ( config . filterMethod === 'exclude' ) {
69
+ if ( ! config . filterArray . some ( it => it === path ) ) requiredPaths [ path ] = list . paths [ path ] ;
70
+ }
71
+ }
72
+
73
+ const requiredSchemasSet : Set < string > = new Set ( ) ;
74
+
75
+ for ( const pathName in requiredPaths ) {
76
+ const pathElement = requiredPaths [ pathName ] ;
77
+
78
+ const openApiPathValues = Object . values ( pathElement ) as (
79
+ | OpenApiOperation
80
+ | OpenApiServer
81
+ | OpenApiParameter
82
+ | string
83
+ ) [ ] ;
84
+
85
+ openApiPathValues . forEach ( requestMethodData => {
86
+ if ( typeof requestMethodData !== 'string' ) {
87
+ if ( ! ( 'url' in requestMethodData ) ) {
88
+ if ( 'parameters' in requestMethodData ) {
89
+ // add schemas from {apiPath}/{method}/parameters
90
+ requestMethodData . parameters ?. forEach ( parameter => {
91
+ const modelName = getSchemaRefFromContent ( parameter ) ;
92
+
93
+ requiredSchemasSet . add ( modelName ) ;
94
+ recursiveAddAllUnknownModels ( modelName , openApi , requiredSchemasSet ) ;
95
+ } ) ;
96
+ }
97
+ if ( 'responses' in requestMethodData ) {
98
+ const responsesCodeData = Object . values ( requestMethodData . responses ) ;
99
+
100
+ responsesCodeData . forEach ( response => {
101
+ const contentTypeData = Object . values ( response . content ?? { } ) ;
102
+
103
+ // add schemas from {apiPath}/{method}/responses/{responseType}/content
104
+ contentTypeData . forEach ( content => {
105
+ const modelName = getSchemaRefFromContent ( content ) ;
106
+
107
+ requiredSchemasSet . add ( getSchemaRefFromContent ( content ) ) ;
108
+ recursiveAddAllUnknownModels ( modelName , openApi , requiredSchemasSet ) ;
109
+ } ) ;
110
+ } ) ;
111
+ }
112
+ if ( 'requestBody' in requestMethodData ) {
113
+ const requestBodyContent = Object . values ( requestMethodData . requestBody ?. content ?? { } ) ;
114
+
115
+ // add schemas from {apiPath}/{method}/responses/{responseType}/requestBody/content
116
+ requestBodyContent . forEach ( content => {
117
+ const modelName = getSchemaRefFromContent ( content ) ;
118
+
119
+ requiredSchemasSet . add ( getSchemaRefFromContent ( content ) ) ;
120
+ recursiveAddAllUnknownModels ( modelName , openApi , requiredSchemasSet ) ;
121
+ } ) ;
122
+ }
123
+ }
124
+ }
125
+ } ) ;
126
+ }
127
+
128
+ const requiredSchemas : Dictionary < OpenApiSchema > = { } ;
129
+
130
+ if ( list && list . components && list . components . schemas ) {
131
+ for ( const schema in list . components . schemas ) {
132
+ if ( requiredSchemasSet . has ( schema ) ) {
133
+ requiredSchemas [ schema ] = list . components . schemas [ schema ] ;
134
+ }
135
+ }
136
+ }
137
+
138
+ const listWithRequiredPaths : OpenApi = {
139
+ ...list ,
140
+ paths : requiredPaths ,
141
+ components : {
142
+ schemas : requiredSchemas ,
143
+ } ,
144
+ } ;
145
+
146
+ await generate ( { ...config , input : listWithRequiredPaths } ) ;
52
147
} ;
53
148
54
149
export default generateSaddlebackSpec ;
0 commit comments