1616
1717package com.github.hauner.openapi.spring.converter
1818
19- import com.github.hauner.openapi.spring.converter.schema.ParameterSchemaInfo
19+ import com.github.hauner.openapi.spring.converter.mapping.AddParameterTypeMapping
20+ import com.github.hauner.openapi.spring.converter.mapping.Mapping
21+ import com.github.hauner.openapi.spring.converter.mapping.MappingSchema
22+ import com.github.hauner.openapi.spring.converter.mapping.TargetType
23+ import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
2024import com.github.hauner.openapi.spring.converter.schema.RefResolver
21- import com.github.hauner.openapi.spring.converter.schema.ResponseSchemaInfo
2225import com.github.hauner.openapi.spring.converter.schema.SchemaInfo
2326import com.github.hauner.openapi.spring.model.Api
2427import com.github.hauner.openapi.spring.model.DataTypes
2528import com.github.hauner.openapi.spring.model.Endpoint
2629import com.github.hauner.openapi.spring.model.RequestBody as ModelRequestBody
30+ import com.github.hauner.openapi.spring.model.datatypes.MappedDataType
2731import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
32+ import com.github.hauner.openapi.spring.model.parameters.AdditionalParameter
2833import com.github.hauner.openapi.spring.model.parameters.CookieParameter
2934import com.github.hauner.openapi.spring.model.parameters.HeaderParameter
3035import com.github.hauner.openapi.spring.model.parameters.MultipartParameter
@@ -56,6 +61,25 @@ class ApiConverter {
5661 private DataTypeConverter dataTypeConverter
5762 private ApiOptions options
5863
64+ class MappingSchemaEndpoint implements MappingSchema {
65+ Endpoint endpoint
66+
67+ @Override
68+ String getPath () {
69+ endpoint. path
70+ }
71+
72+ @Override
73+ String getName () {
74+ null
75+ }
76+
77+ @Override
78+ String getContentType () {
79+ null
80+ }
81+ }
82+
5983 ApiConverter (ApiOptions options ) {
6084 this . options = options
6185
@@ -91,7 +115,8 @@ class ApiConverter {
91115 operations. each { httpOperation ->
92116 def itf = target. getInterface (getInterfaceName (httpOperation))
93117
94- Endpoint ep = new Endpoint (path : path, method : httpOperation. httpMethod)
118+ Endpoint ep = new Endpoint (path : path,
119+ method : (httpOperation as HttpMethodTrait ). httpMethod)
95120
96121 try {
97122 def resolver = new RefResolver (api. components)
@@ -103,7 +128,8 @@ class ApiConverter {
103128 itf. endpoints. add (ep)
104129
105130 } catch (UnknownDataTypeException e) {
106- log. error (" failed to parse endpoint {} {} because of: '{}'" , ep. path, ep. method, e. message)
131+ log. error (" failed to parse endpoint {} {} because of: '{}'" ,
132+ ep. path, ep. method, e. message)
107133 }
108134 }
109135 }
@@ -113,6 +139,11 @@ class ApiConverter {
113139 parameters. each { Parameter parameter ->
114140 ep. parameters. add (createParameter (ep. path, parameter, dataTypes, resolver))
115141 }
142+
143+ List<Mapping > addMappings = findAdditionalParameter (ep)
144+ addMappings. each {
145+ ep. parameters. add (createAdditionalParameter (ep. path, it as AddParameterTypeMapping , dataTypes, resolver))
146+ }
116147 }
117148
118149 private void collectRequestBody (RequestBody requestBody , Endpoint ep , DataTypes dataTypes , RefResolver resolver ) {
@@ -161,8 +192,11 @@ class ApiConverter {
161192 }
162193
163194 private ModelParameter createParameter (String path , Parameter parameter , DataTypes dataTypes , resolver ) {
164- def info = new ParameterSchemaInfo (path, parameter. schema, parameter. name)
165- info. resolver = resolver
195+ def info = new SchemaInfo (
196+ path : path,
197+ name : parameter. name,
198+ schema : parameter. schema,
199+ resolver : resolver)
166200
167201 DataType dataType = dataTypeConverter. convert (info, dataTypes)
168202
@@ -181,6 +215,19 @@ class ApiConverter {
181215 }
182216 }
183217
218+ private ModelParameter createAdditionalParameter (String path , AddParameterTypeMapping mapping , DataTypes dataTypes , RefResolver resolver ) {
219+ TypeMapping tm = mapping. childMappings. first ()
220+ TargetType tt = tm. targetType
221+
222+ def addType = new MappedDataType (
223+ type : tt. name,
224+ pkg : tt. pkg,
225+ genericTypes : tt. genericNames
226+ )
227+
228+ new AdditionalParameter (name : mapping. parameterName, required : true , dataType : addType)
229+ }
230+
184231 private ModelRequestBody createRequestBody (String contentType , SchemaInfo info , boolean required , DataTypes dataTypes ) {
185232 DataType dataType = dataTypeConverter. convert (info, dataTypes)
186233
@@ -209,12 +256,12 @@ class ApiConverter {
209256 def mediaType = contentEntry. value
210257 def schema = mediaType. schema
211258
212- def info = new ResponseSchemaInfo (
213- path,
214- contentType,
215- schema ,
216- inlineName)
217- info . resolver = resolver
259+ def info = new SchemaInfo (
260+ path : path ,
261+ contentType : contentType ,
262+ name : inlineName ,
263+ schema : schema,
264+ resolver : resolver)
218265
219266 DataType dataType = dataTypeConverter. convert (info, dataTypes)
220267
@@ -228,6 +275,17 @@ class ApiConverter {
228275 responses
229276 }
230277
278+ private List<Mapping > findAdditionalParameter (Endpoint ep ) {
279+ def addMappings = options. typeMappings. findAll {
280+ it. matches (Mapping.Level . ENDPOINT , new MappingSchemaEndpoint (endpoint : ep))
281+ }. collectMany {
282+ it. childMappings
283+ }. findAll {
284+ it. matches (Mapping.Level . ADD , null as MappingSchema )
285+ }
286+ addMappings
287+ }
288+
231289 private String getInlineRequestBodyName (String path ) {
232290 Identifier . toClass (path) + ' RequestBody'
233291 }
0 commit comments