@@ -18,6 +18,7 @@ package com.github.hauner.openapi.spring.converter
1818
1919import com.github.hauner.openapi.spring.converter.mapping.AmbiguousTypeMappingException
2020import com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping
21+ import com.github.hauner.openapi.spring.converter.mapping.ParameterTypeMapping
2122import com.github.hauner.openapi.spring.converter.mapping.ResponseTypeMapping
2223import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
2324import com.github.hauner.openapi.spring.model.Api
@@ -29,7 +30,7 @@ import static com.github.hauner.openapi.spring.support.OpenApiParser.parse
2930class DataTypeConverterArrayTypeMappingSpec extends Specification {
3031
3132 @Unroll
32- void " maps simple array schema to #responseTypeName via global array mapping" () {
33+ void " maps array schema to #responseTypeName via global type mapping" () {
3334 def openApi = parse (""" \
3435openapi: 3.0.2
3536info:
@@ -108,15 +109,15 @@ paths:
108109 e. typeMappings == options. typeMappings
109110 }
110111
111- void " converts simple array response schema to Collection<> via endpoint response type mapping" () {
112+ void " converts array response schema to #responseTypeName via endpoint type mapping" () {
112113 def openApi = parse (""" \
113114openapi: 3.0.2
114115info:
115116 title: API
116117 version: 1.0.0
117118
118119paths:
119- /array-string :
120+ /foo :
120121 get:
121122 responses:
122123 '200':
@@ -126,36 +127,98 @@ paths:
126127 type: array
127128 items:
128129 type: string
129- description: none
130+ description: none
130131""" )
131132
132133 when :
133- def options = new ApiOptions (
134- packageName : ' pkg' ,
135- typeMappings : [
136- new EndpointTypeMapping (path : ' /array-string' ,
137- typeMappings : [
138- // new ResponseTypeMapping (
139- // contentType: 'application/vnd.any',
140- // mapping: new TypeMapping (
141- // targetTypeName: 'pkg.TargetClass')
142- // ),
143- new ResponseTypeMapping (
144- contentType : ' application/vnd.any' ,
145- mapping : new TypeMapping (
146- targetTypeName : ' java.util.Collection' )
147- )
134+ def options = new ApiOptions (packageName : ' pkg' , typeMappings : [
135+ new EndpointTypeMapping (path : ' /foo' ,
136+ typeMappings : [
137+ new TypeMapping (
138+ sourceTypeName : ' array' ,
139+ targetTypeName : targetTypeName)
148140 ])
149- ])
141+ ])
150142 Api api = new ApiConverter (options). convert (openApi)
151143
152144 then :
153145 def itf = api. interfaces. first ()
154146 def ep = itf. endpoints. first ()
155- ep. response. responseType. name == ' Collection<String>'
147+ ep. response. responseType. name == responseTypeName
148+ ep. response. responseType. packageName == ' java.util'
149+
150+ where :
151+ targetTypeName | responseTypeName
152+ ' java.util.Collection' | ' Collection<String>'
153+ ' java.util.List' | ' List<String>'
154+ ' java.util.Set' | ' Set<String>'
155+ }
156+
157+ @Unroll
158+ void " converts array parameter schema to java type via #type" () {
159+ def openApi = parse (""" \
160+ openapi: 3.0.2
161+ info:
162+ title: API
163+ version: 1.0.0
164+
165+ paths:
166+ /foobar:
167+ get:
168+ parameters:
169+ - in: query
170+ name: foobar
171+ required: false
172+ schema:
173+ type: array
174+ items:
175+ type: string
176+ responses:
177+ '204':
178+ description: empty
179+ """ )
180+
181+ when :
182+ def options = new ApiOptions (packageName : ' pkg' , typeMappings : mappings)
183+ Api api = new ApiConverter (options). convert (openApi)
184+
185+ then :
186+ def itf = api. interfaces. first ()
187+ def ep = itf. endpoints. first ()
188+ def p = ep. parameters. first ()
189+ p. dataType. name == ' Collection<String>'
190+ p. dataType. packageName == ' java.util'
191+
192+ where :
193+ type << [
194+ ' endpoint parameter mapping' ,
195+ ' global parameter mapping'
196+ ]
197+
198+ mappings << [
199+ [
200+ new EndpointTypeMapping (path : ' /foobar' ,
201+ typeMappings : [
202+ new ParameterTypeMapping (
203+ parameterName : ' foobar' ,
204+ mapping : new TypeMapping (
205+ sourceTypeName : ' array' ,
206+ targetTypeName : ' java.util.Collection' )
207+ )
208+ ])
209+ ], [
210+ new ParameterTypeMapping (
211+ parameterName : ' foobar' ,
212+ mapping : new TypeMapping (
213+ sourceTypeName : ' array' ,
214+ targetTypeName : ' java.util.Collection' )
215+ )
216+ ]
217+ ]
156218 }
157219
158- void " converts simple array response schema to Collection<> via global response type array mapping" () {
220+ @Unroll
221+ void " converts array response schema to Collection<> via type" () {
159222 def openApi = parse (""" \
160223openapi: 3.0.2
161224info:
@@ -177,21 +240,66 @@ paths:
177240""" )
178241
179242 when :
180- def options = new ApiOptions (
181- packageName : ' pkg' ,
182- typeMappings : [
183- new ResponseTypeMapping (
184- contentType : ' application/vnd.any' ,
185- mapping : new TypeMapping (
186- targetTypeName : ' java.util.Collection' )
187- )
188- ])
243+ def options = new ApiOptions (packageName : ' pkg' , typeMappings : mappings)
189244 Api api = new ApiConverter (options). convert (openApi)
190245
191246 then :
192247 def itf = api. interfaces. first ()
193248 def ep = itf. endpoints. first ()
194249 ep. response. responseType. name == ' Collection<String>'
250+ ep. response. responseType. imports == [' java.util.Collection' , ' java.lang.String' ] as Set
251+
252+ where :
253+ type << [
254+ ' endpoint response mapping' ,
255+ ' global response mapping' ,
256+ ' endpoint response mapping over endpoint type mapping' ,
257+ ' endpoint type mapping'
258+ ]
259+
260+ mappings << [
261+ [
262+ new EndpointTypeMapping (path : ' /array-string' ,
263+ typeMappings : [
264+ new ResponseTypeMapping (
265+ contentType : ' application/vnd.any' ,
266+ mapping : new TypeMapping (
267+ sourceTypeName : ' array' ,
268+ targetTypeName : ' java.util.Collection' )
269+ )
270+ ]
271+ )
272+ ], [
273+ new ResponseTypeMapping (
274+ contentType : ' application/vnd.any' ,
275+ mapping : new TypeMapping (
276+ sourceTypeName : ' array' ,
277+ targetTypeName : ' java.util.Collection' )
278+ )
279+ ], [
280+ new EndpointTypeMapping (path : ' /array-string' ,
281+ typeMappings : [
282+ new ResponseTypeMapping (
283+ contentType : ' application/vnd.any' ,
284+ mapping : new TypeMapping (
285+ sourceTypeName : ' array' ,
286+ targetTypeName : ' java.util.Collection' )
287+ ),
288+ new TypeMapping (
289+ sourceTypeName : ' array' ,
290+ targetTypeName : ' java.util.Collection' )
291+ ]
292+ )
293+ ], [
294+ new EndpointTypeMapping (path : ' /array-string' ,
295+ typeMappings : [
296+ new TypeMapping (
297+ sourceTypeName : ' array' ,
298+ targetTypeName : ' java.util.Collection' )
299+ ]
300+ )
301+ ]
302+ ]
195303 }
196304
197305}
0 commit comments