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,11 @@ 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 MappingFinder mappingFinder
6666 private ApiOptions options
6767
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-
8768 ApiConverter (ApiOptions options ) {
8869 this . options = options
8970
@@ -92,6 +73,9 @@ class ApiConverter {
9273 }
9374
9475 dataTypeConverter = new DataTypeConverter (this . options)
76+ dataTypeWrapper = new ResultDataTypeWrapper (this . options)
77+ singleDataTypeWrapper = new SingleDataTypeWrapper (this . options)
78+ mappingFinder = new MappingFinder (typeMappings : this . options. typeMappings)
9579 }
9680
9781 /**
@@ -164,7 +148,7 @@ class ApiConverter {
164148 ep. parameters. add (createParameter (ep. path, parameter, dataTypes, resolver))
165149 }
166150
167- List<Mapping > addMappings = findAdditionalParameter (ep)
151+ List<Mapping > addMappings = mappingFinder . findAdditionalEndpointParameter (ep. path )
168152 addMappings. each {
169153 ep. parameters. add (createAdditionalParameter (ep. path, it as AddParameterTypeMapping , dataTypes, resolver))
170154 }
@@ -200,18 +184,14 @@ class ApiConverter {
200184 def httpStatus = responseEntry. key
201185 def httpResponse = responseEntry. value
202186
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- }
187+ List<ModelResponse > results = createResponses (
188+ ep. path,
189+ httpStatus,
190+ httpResponse,
191+ dataTypes,
192+ resolver)
193+
194+ ep. addResponses (httpStatus, results)
215195 }
216196
217197 }
@@ -255,10 +235,11 @@ class ApiConverter {
255235
256236 private ModelRequestBody createRequestBody (String contentType , SchemaInfo info , boolean required , DataTypes dataTypes ) {
257237 DataType dataType = dataTypeConverter. convert (info, dataTypes)
238+ DataType singleDataType = singleDataTypeWrapper. wrap (dataType, info)
258239
259240 new ModelRequestBody (
260241 contentType : contentType,
261- requestBodyType : dataType ,
242+ requestBodyType : singleDataType ,
262243 required : required)
263244 }
264245
@@ -274,8 +255,20 @@ class ApiConverter {
274255 }
275256
276257 private List<ModelResponse > createResponses (String path , String httpStatus , Response response , DataTypes dataTypes , RefResolver resolver ) {
277- def responses = []
258+ if (! response. content) {
259+ def info = new SchemaInfo (path : path)
260+
261+ DataType dataType = new NoneDataType ()
262+ DataType singleDataType = singleDataTypeWrapper. wrap (dataType, info)
263+ DataType resultDataType = dataTypeWrapper. wrap (singleDataType, info)
278264
265+ def resp = new ModelResponse (
266+ responseType : resultDataType)
267+
268+ return [resp]
269+ }
270+
271+ def responses = []
279272 response. content. each { Map.Entry <String , MediaType > contentEntry ->
280273 def contentType = contentEntry. key
281274 def mediaType = contentEntry. value
@@ -289,28 +282,19 @@ class ApiConverter {
289282 resolver : resolver)
290283
291284 DataType dataType = dataTypeConverter. convert (info, dataTypes)
285+ DataType singleDataType = singleDataTypeWrapper. wrap (dataType, info)
286+ DataType resultDataType = dataTypeWrapper. wrap (singleDataType, info)
292287
293288 def resp = new ModelResponse (
294289 contentType : contentType,
295- responseType : dataType )
290+ responseType : resultDataType )
296291
297292 responses. add (resp)
298293 }
299294
300295 responses
301296 }
302297
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-
314298 private String getInlineRequestBodyName (String path ) {
315299 Identifier . toClass (path) + ' RequestBody'
316300 }
@@ -319,22 +303,8 @@ class ApiConverter {
319303 Identifier . toClass (path) + ' Response' + httpStatus
320304 }
321305
322-
323306 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
307+ mappingFinder. isExcludedEndpoint (path)
338308 }
339309
340310 private String getInterfaceName (def op , boolean excluded ) {
0 commit comments