Skip to content

Commit 1709d4e

Browse files
committed
re-use object mapping
1 parent ca157b4 commit 1709d4e

4 files changed

Lines changed: 143 additions & 40 deletions

File tree

src/main/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverter.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class DataTypeConverter {
214214
}
215215
}
216216

217-
// check global parameter & response mappings
217+
// check global io (parameter & response) mappings
218218
List<TypeMappingX> ioMatches = schemaType.matchIoMapping (options.typeMappings)
219219
if (!ioMatches.empty) {
220220
TargetType target = ioMatches.first().targetType

src/main/groovy/com/github/hauner/openapi/spring/converter/schema/SchemaType.groovy

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ abstract class BaseSchemaType implements SchemaType {
5757
}
5858

5959
// type mappings
60-
endpoint.findAll {
61-
it.isLevel (MappingLevel.TYPE) && it.matches (info)
62-
}.collect {
63-
it.childMappings
64-
}.flatten () as List<TypeMappingX>
60+
matchTypeMapping (endpoint)
6561
}
6662

6763
List<TypeMappingX> matchIoMapping (List<TypeMappingX> typeMappings) {
@@ -98,8 +94,9 @@ class ArraySchemaType extends BaseSchemaType {
9894

9995
@Override
10096
List<TypeMappingX> matchTypeMapping (List<TypeMappingX> typeMappings) {
97+
def array = new SchemaInfo (null, null,'array')
10198
typeMappings.findAll () {
102-
it.isLevel (MappingLevel.TYPE) && it.matches (new SchemaInfo (null, null,'array'))
99+
it.isLevel (MappingLevel.TYPE) && it.matches (array)
103100
}
104101
}
105102

src/test/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverterArrayTypeMappingSpec.groovy

Lines changed: 139 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.github.hauner.openapi.spring.converter
1818

1919
import com.github.hauner.openapi.spring.converter.mapping.AmbiguousTypeMappingException
2020
import com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping
21+
import com.github.hauner.openapi.spring.converter.mapping.ParameterTypeMapping
2122
import com.github.hauner.openapi.spring.converter.mapping.ResponseTypeMapping
2223
import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
2324
import com.github.hauner.openapi.spring.model.Api
@@ -29,7 +30,7 @@ import static com.github.hauner.openapi.spring.support.OpenApiParser.parse
2930
class 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 ("""\
3435
openapi: 3.0.2
3536
info:
@@ -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 ("""\
113114
openapi: 3.0.2
114115
info:
115116
title: API
116117
version: 1.0.0
117118
118119
paths:
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 ("""\
160223
openapi: 3.0.2
161224
info:
@@ -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
}

src/test/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverterObjectTypeMappingSpec.groovy

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,13 @@ paths:
285285
new ParameterTypeMapping (
286286
parameterName: 'foobar',
287287
mapping: new TypeMapping (
288-
sourceTypeName: 'object',
289288
targetTypeName: 'pkg.TargetClass')
290289
)
291290
])
292291
], [
293292
new ParameterTypeMapping (
294293
parameterName: 'foobar',
295294
mapping: new TypeMapping (
296-
sourceTypeName: 'object',
297295
targetTypeName: 'pkg.TargetClass')
298296
)
299297
]

0 commit comments

Comments
 (0)