@@ -23,7 +23,6 @@ import com.github.hauner.openapi.spring.converter.mapping.Mapping
23
23
import com.github.hauner.openapi.spring.converter.mapping.MappingSchema
24
24
import com.github.hauner.openapi.spring.converter.mapping.TargetType
25
25
import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
26
- import com.github.hauner.openapi.spring.converter.schema.RefResolver
27
26
import com.github.hauner.openapi.spring.converter.schema.SchemaInfo
28
27
import com.github.hauner.openapi.spring.model.Api
29
28
import com.github.hauner.openapi.spring.model.DataTypes
@@ -39,17 +38,18 @@ import com.github.hauner.openapi.spring.model.parameters.MultipartParameter
39
38
import com.github.hauner.openapi.spring.model.parameters.Parameter as ModelParameter
40
39
import com.github.hauner.openapi.spring.model.parameters.PathParameter
41
40
import com.github.hauner.openapi.spring.model.parameters.QueryParameter
42
- import com.github.hauner.openapi.spring.model.Response
41
+ import com.github.hauner.openapi.spring.model.Response as ModelResponse
43
42
import com.github.hauner.openapi.spring.model.datatypes.DataType
43
+ import com.github.hauner.openapi.spring.parser.OpenApi
44
+ import com.github.hauner.openapi.spring.parser.MediaType
45
+ import com.github.hauner.openapi.spring.parser.Operation
46
+ import com.github.hauner.openapi.spring.parser.Parameter
47
+ import com.github.hauner.openapi.spring.parser.Path
48
+ import com.github.hauner.openapi.spring.parser.RefResolver
49
+ import com.github.hauner.openapi.spring.parser.Response
50
+ import com.github.hauner.openapi.spring.parser.RequestBody
44
51
import com.github.hauner.openapi.support.Identifier
45
52
import groovy.util.logging.Slf4j
46
- import io.swagger.v3.oas.models.OpenAPI
47
- import io.swagger.v3.oas.models.PathItem
48
- import io.swagger.v3.oas.models.media.MediaType
49
- import io.swagger.v3.oas.models.parameters.Parameter
50
- import io.swagger.v3.oas.models.parameters.RequestBody
51
- import io.swagger.v3.oas.models.responses.ApiResponse
52
- import io.swagger.v3.oas.models.responses.ApiResponses
53
53
54
54
/**
55
55
* Converts the open api model to a new model that is better suited for generating source files
@@ -100,26 +100,24 @@ class ApiConverter {
100
100
* @param api the open api model
101
101
* @return source generation model
102
102
*/
103
- Api convert (OpenAPI api ) {
103
+ Api convert (OpenApi api ) {
104
104
def target = new Api ()
105
105
createInterfaces (api, target)
106
106
target
107
107
}
108
108
109
- private void createInterfaces (OpenAPI api , Api target ) {
110
- def resolver = new RefResolver (api. components)
109
+ private void createInterfaces (OpenApi api , Api target ) {
111
110
Map<String , Interface > interfaces = new HashMap<> ()
112
111
113
- api. paths. each { Map.Entry <String , PathItem > pathEntry ->
112
+ api. paths. each { Map.Entry <String , Path > pathEntry ->
114
113
String path = pathEntry. key
115
- PathItem pathItem = pathEntry. value
114
+ Path pathValue = pathEntry. value
116
115
117
- def operations = new OperationCollector ()
118
- .collect (pathItem)
116
+ def operations = pathValue. operations
117
+ operations. each { Operation op ->
118
+ Interface itf = createInterface (path, op, interfaces)
119
119
120
- operations. each { httpOperation ->
121
- Interface itf = createInterface (path, httpOperation, interfaces)
122
- Endpoint ep = createEndpoint (path, httpOperation, target. models, resolver)
120
+ Endpoint ep = createEndpoint (path, op, target. models, api. refResolver)
123
121
if (ep) {
124
122
itf. endpoints. add (ep)
125
123
}
@@ -129,7 +127,7 @@ class ApiConverter {
129
127
target. interfaces = interfaces. values () as List<Interface >
130
128
}
131
129
132
- private Interface createInterface (String path , def operation , Map<String , Interface > interfaces ) {
130
+ private Interface createInterface (String path , Operation operation , Map<String , Interface > interfaces ) {
133
131
def targetInterfaceName = getInterfaceName (operation, isExcluded (path))
134
132
135
133
def itf = interfaces. get (targetInterfaceName)
@@ -146,8 +144,8 @@ class ApiConverter {
146
144
itf
147
145
}
148
146
149
- private Endpoint createEndpoint (String path , def operation , DataTypes dataTypes , RefResolver resolver ) {
150
- Endpoint ep = new Endpoint (path : path, method : ( operation as HttpMethodTrait ) . httpMethod )
147
+ private Endpoint createEndpoint (String path , Operation operation , DataTypes dataTypes , RefResolver resolver ) {
148
+ Endpoint ep = new Endpoint (path : path, method : operation. method )
151
149
152
150
try {
153
151
collectParameters (operation. parameters, ep, dataTypes, resolver)
@@ -181,12 +179,12 @@ class ApiConverter {
181
179
182
180
requestBody. content. each { Map.Entry <String , MediaType > requestBodyEntry ->
183
181
def contentType = requestBodyEntry. key
184
- def reqBody = requestBodyEntry. value
182
+ def mediaType = requestBodyEntry. value
185
183
186
184
def info = new SchemaInfo (
187
185
path : ep. path,
188
186
name : getInlineRequestBodyName (ep. path),
189
- schema : reqBody . schema,
187
+ schema : mediaType . schema,
190
188
resolver : resolver)
191
189
192
190
if (contentType == MULTIPART ) {
@@ -197,15 +195,15 @@ class ApiConverter {
197
195
}
198
196
}
199
197
200
- private collectResponses (ApiResponses responses , Endpoint ep , DataTypes dataTypes , RefResolver resolver ) {
201
- responses. each { Map.Entry <String , ApiResponse > responseEntry ->
198
+ private collectResponses (Map< String , Response > responses , Endpoint ep , DataTypes dataTypes , RefResolver resolver ) {
199
+ responses. each { Map.Entry <String , Response > responseEntry ->
202
200
def httpStatus = responseEntry. key
203
201
def httpResponse = responseEntry. value
204
202
205
203
if (! httpResponse. content) {
206
- ep. addResponses (httpStatus, [Response . EMPTY ])
204
+ ep. addResponses (httpStatus, [ModelResponse . EMPTY ])
207
205
} else {
208
- List<Response > results = createResponses (
206
+ List<ModelResponse > results = createResponses (
209
207
ep. path,
210
208
httpStatus,
211
209
httpResponse,
@@ -215,9 +213,10 @@ class ApiConverter {
215
213
ep. addResponses (httpStatus, results)
216
214
}
217
215
}
216
+
218
217
}
219
218
220
- private ModelParameter createParameter (String path , Parameter parameter , DataTypes dataTypes , resolver ) {
219
+ private ModelParameter createParameter (String path , Parameter parameter , DataTypes dataTypes , RefResolver resolver ) {
221
220
def info = new SchemaInfo (
222
221
path : path,
223
222
name : parameter. name,
@@ -274,10 +273,10 @@ class ApiConverter {
274
273
}
275
274
}
276
275
277
- private List<Response > createResponses (String path , String httpStatus , ApiResponse apiResponse , DataTypes dataTypes , RefResolver resolver ) {
276
+ private List<ModelResponse > createResponses (String path , String httpStatus , Response response , DataTypes dataTypes , RefResolver resolver ) {
278
277
def responses = []
279
278
280
- apiResponse . content. each { Map.Entry <String , MediaType > contentEntry ->
279
+ response . content. each { Map.Entry <String , MediaType > contentEntry ->
281
280
def contentType = contentEntry. key
282
281
def mediaType = contentEntry. value
283
282
def schema = mediaType. schema
@@ -291,11 +290,11 @@ class ApiConverter {
291
290
292
291
DataType dataType = dataTypeConverter. convert (info, dataTypes)
293
292
294
- def response = new Response (
293
+ def resp = new ModelResponse (
295
294
contentType : contentType,
296
295
responseType : dataType)
297
296
298
- responses. add (response )
297
+ responses. add (resp )
299
298
}
300
299
301
300
responses
@@ -341,8 +340,8 @@ class ApiConverter {
341
340
private String getInterfaceName (def op , boolean excluded ) {
342
341
String targetInterfaceName = INTERFACE_DEFAULT_NAME
343
342
344
- if (hasTags (op)) {
345
- targetInterfaceName = op. tags . first ()
343
+ if ((op. hasTags() )) {
344
+ targetInterfaceName = op. firstTag
346
345
}
347
346
348
347
if (excluded) {
@@ -352,8 +351,4 @@ class ApiConverter {
352
351
targetInterfaceName
353
352
}
354
353
355
- private boolean hasTags (op ) {
356
- op. tags && ! op. tags. empty
357
- }
358
-
359
354
}
0 commit comments