1717package com.github.hauner.openapi.spring.converter
1818
1919import com.github.hauner.openapi.spring.converter.mapping.AddParameterTypeMapping
20- import com.github.hauner.openapi.spring.converter.mapping.AmbiguousTypeMappingException
21- import com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping
2220import com.github.hauner.openapi.spring.converter.mapping.Mapping
23- import com.github.hauner.openapi.spring.converter.mapping.MappingSchema
2421import com.github.hauner.openapi.spring.converter.mapping.TargetType
2522import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
26- import com.github.hauner.openapi.spring.converter.schema.SchemaInfo
2723import com.github.hauner.openapi.spring.model.Api
2824import com.github.hauner.openapi.spring.model.DataTypes
2925import com.github.hauner.openapi.spring.model.Endpoint
3026import com.github.hauner.openapi.spring.model.Interface
3127import com.github.hauner.openapi.spring.model.RequestBody as ModelRequestBody
3228import com.github.hauner.openapi.spring.model.datatypes.MappedDataType
29+ import com.github.hauner.openapi.spring.model.datatypes.NoneDataType
3330import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
3431import com.github.hauner.openapi.spring.model.parameters.AdditionalParameter
3532import com.github.hauner.openapi.spring.model.parameters.CookieParameter
@@ -63,27 +60,12 @@ class ApiConverter {
6360 public static final String INTERFACE_DEFAULT_NAME = ' '
6461
6562 private DataTypeConverter dataTypeConverter
63+ private ResultDataTypeWrapper dataTypeWrapper
64+ private SingleDataTypeWrapper singleDataTypeWrapper
65+ private MultiDataTypeWrapper multiDataTypeWrapper
66+ private MappingFinder mappingFinder
6667 private ApiOptions options
6768
68- class MappingSchemaEndpoint implements MappingSchema {
69- String path
70-
71- @Override
72- String getPath () {
73- path
74- }
75-
76- @Override
77- String getName () {
78- null
79- }
80-
81- @Override
82- String getContentType () {
83- null
84- }
85- }
86-
8769 ApiConverter (ApiOptions options ) {
8870 this . options = options
8971
@@ -92,6 +74,10 @@ class ApiConverter {
9274 }
9375
9476 dataTypeConverter = new DataTypeConverter (this . options)
77+ dataTypeWrapper = new ResultDataTypeWrapper (this . options)
78+ singleDataTypeWrapper = new SingleDataTypeWrapper (this . options)
79+ multiDataTypeWrapper = new MultiDataTypeWrapper (this . options)
80+ mappingFinder = new MappingFinder (typeMappings : this . options. typeMappings)
9581 }
9682
9783 /**
@@ -164,7 +150,7 @@ class ApiConverter {
164150 ep. parameters. add (createParameter (ep. path, parameter, dataTypes, resolver))
165151 }
166152
167- List<Mapping > addMappings = findAdditionalParameter (ep)
153+ List<Mapping > addMappings = mappingFinder . findAdditionalEndpointParameter (ep. path )
168154 addMappings. each {
169155 ep. parameters. add (createAdditionalParameter (ep. path, it as AddParameterTypeMapping , dataTypes, resolver))
170156 }
@@ -200,18 +186,14 @@ class ApiConverter {
200186 def httpStatus = responseEntry. key
201187 def httpResponse = responseEntry. value
202188
203- if (! httpResponse. content) {
204- ep. addResponses (httpStatus, [ModelResponse . EMPTY ])
205- } else {
206- List<ModelResponse > results = createResponses (
207- ep. path,
208- httpStatus,
209- httpResponse,
210- dataTypes,
211- resolver)
212-
213- ep. addResponses (httpStatus, results)
214- }
189+ List<ModelResponse > results = createResponses (
190+ ep. path,
191+ httpStatus,
192+ httpResponse,
193+ dataTypes,
194+ resolver)
195+
196+ ep. addResponses (httpStatus, results)
215197 }
216198
217199 }
@@ -255,10 +237,16 @@ class ApiConverter {
255237
256238 private ModelRequestBody createRequestBody (String contentType , SchemaInfo info , boolean required , DataTypes dataTypes ) {
257239 DataType dataType = dataTypeConverter. convert (info, dataTypes)
240+ DataType changedType
241+ if (! info. isArray ()) {
242+ changedType = singleDataTypeWrapper. wrap (dataType, info)
243+ } else {
244+ changedType = multiDataTypeWrapper. wrap (dataType, info)
245+ }
258246
259247 new ModelRequestBody (
260248 contentType : contentType,
261- requestBodyType : dataType ,
249+ requestBodyType : changedType ,
262250 required : required)
263251 }
264252
@@ -274,8 +262,20 @@ class ApiConverter {
274262 }
275263
276264 private List<ModelResponse > createResponses (String path , String httpStatus , Response response , DataTypes dataTypes , RefResolver resolver ) {
277- def responses = []
265+ if (! response. content) {
266+ def info = new SchemaInfo (path : path)
278267
268+ DataType dataType = new NoneDataType ()
269+ DataType singleDataType = singleDataTypeWrapper. wrap (dataType, info)
270+ DataType resultDataType = dataTypeWrapper. wrap (singleDataType, info)
271+
272+ def resp = new ModelResponse (
273+ responseType : resultDataType)
274+
275+ return [resp]
276+ }
277+
278+ def responses = []
279279 response. content. each { Map.Entry <String , MediaType > contentEntry ->
280280 def contentType = contentEntry. key
281281 def mediaType = contentEntry. value
@@ -289,28 +289,24 @@ class ApiConverter {
289289 resolver : resolver)
290290
291291 DataType dataType = dataTypeConverter. convert (info, dataTypes)
292+ DataType changedType
293+ if (! info. isArray ()) {
294+ changedType = singleDataTypeWrapper. wrap (dataType, info)
295+ } else {
296+ changedType = multiDataTypeWrapper. wrap (dataType, info)
297+ }
298+ DataType resultDataType = dataTypeWrapper. wrap (changedType, info)
292299
293300 def resp = new ModelResponse (
294301 contentType : contentType,
295- responseType : dataType )
302+ responseType : resultDataType )
296303
297304 responses. add (resp)
298305 }
299306
300307 responses
301308 }
302309
303- private List<Mapping > findAdditionalParameter (Endpoint ep ) {
304- def addMappings = options. typeMappings. findAll {
305- it. matches (Mapping.Level . ENDPOINT , new MappingSchemaEndpoint (path : ep. path))
306- }. collectMany {
307- it. childMappings
308- }. findAll {
309- it. matches (Mapping.Level . ADD , null as MappingSchema )
310- }
311- addMappings as List<Mapping >
312- }
313-
314310 private String getInlineRequestBodyName (String path ) {
315311 Identifier . toClass (path) + ' RequestBody'
316312 }
@@ -319,22 +315,8 @@ class ApiConverter {
319315 Identifier . toClass (path) + ' Response' + httpStatus
320316 }
321317
322-
323318 private boolean isExcluded (String path ) {
324- def endpointMatches = options. typeMappings. findAll {
325- it. matches (Mapping.Level . ENDPOINT , new MappingSchemaEndpoint (path : path))
326- }
327-
328- if (! endpointMatches. empty) {
329- if (endpointMatches. size () != 1 ) {
330- throw new AmbiguousTypeMappingException (endpointMatches)
331- }
332-
333- def match = endpointMatches. first () as EndpointTypeMapping
334- return match. exclude
335- }
336-
337- false
319+ mappingFinder. isExcludedEndpoint (path)
338320 }
339321
340322 private String getInterfaceName (def op , boolean excluded ) {
0 commit comments