Skip to content

Commit 27cafe9

Browse files
committed
error on duplicate type mappings
1 parent 826b3e9 commit 27cafe9

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ class DataTypeConverter {
208208
// check endpoint mappings
209209
List<TypeMappingX> endpointMatches = schemaType.matchEndpointMapping (options.typeMappings)
210210
if (!endpointMatches.empty) {
211+
212+
if (endpointMatches.size () != 1) {
213+
throw new AmbiguousTypeMappingException (endpointMatches)
214+
}
215+
211216
TargetType target = endpointMatches.first().targetType
212217
if (target) {
213218
return target
@@ -217,6 +222,11 @@ class DataTypeConverter {
217222
// check global io (parameter & response) mappings
218223
List<TypeMappingX> ioMatches = schemaType.matchIoMapping (options.typeMappings)
219224
if (!ioMatches.empty) {
225+
226+
if (ioMatches.size () != 1) {
227+
throw new AmbiguousTypeMappingException (ioMatches)
228+
}
229+
220230
TargetType target = ioMatches.first().targetType
221231
if (target) {
222232
return target

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,90 @@ paths:
109109
e.typeMappings == options.typeMappings
110110
}
111111

112+
113+
@Unroll
114+
void "throws when there are multiple mappings on the same level: #type" () {
115+
def openApi = parse ("""\
116+
openapi: 3.0.2
117+
info:
118+
title: API
119+
version: 1.0.0
120+
121+
paths:
122+
/foo:
123+
get:
124+
parameters:
125+
- in: query
126+
name: param
127+
required: false
128+
schema:
129+
type: array
130+
items:
131+
type: string
132+
responses:
133+
'204':
134+
description: none
135+
""")
136+
137+
when:
138+
def options = new ApiOptions(
139+
packageName: 'pkg',
140+
typeMappings: mappings)
141+
new ApiConverter (options).convert (openApi)
142+
143+
then:
144+
def e = thrown (AmbiguousTypeMappingException)
145+
146+
where:
147+
type << [
148+
'global type mappings',
149+
'global io mappings',
150+
'endpoint mappings'
151+
]
152+
153+
mappings << [
154+
[
155+
new TypeMapping (
156+
sourceTypeName: 'array',
157+
targetTypeName: 'java.util.Collection'),
158+
new TypeMapping (
159+
sourceTypeName: 'array',
160+
targetTypeName: 'java.util.Collection')
161+
],
162+
[
163+
new ParameterTypeMapping (
164+
parameterName: 'param',
165+
mapping: new TypeMapping (
166+
sourceTypeName: 'array',
167+
targetTypeName: 'java.util.Collection')
168+
),
169+
new ParameterTypeMapping (
170+
parameterName: 'param',
171+
mapping: new TypeMapping (
172+
sourceTypeName: 'array',
173+
targetTypeName: 'java.util.Collection')
174+
)
175+
],
176+
[
177+
new EndpointTypeMapping (path: '/foo',
178+
typeMappings: [
179+
new ParameterTypeMapping (
180+
parameterName: 'param',
181+
mapping: new TypeMapping (
182+
sourceTypeName: 'array',
183+
targetTypeName: 'java.util.Collection')
184+
),
185+
new ParameterTypeMapping (
186+
parameterName: 'param',
187+
mapping: new TypeMapping (
188+
sourceTypeName: 'array',
189+
targetTypeName: 'java.util.Collection')
190+
)
191+
])
192+
]
193+
]
194+
}
195+
112196
void "converts array response schema to #responseTypeName via endpoint type mapping" () {
113197
def openApi = parse ("""\
114198
openapi: 3.0.2

0 commit comments

Comments
 (0)