Skip to content

Commit 85af4ab

Browse files
committed
handle unknown parameter type
1 parent 6aee9a7 commit 85af4ab

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ class ApiConverter {
131131
case 'cookie':
132132
return new CookieParameter (name: parameter.name, required: parameter.required, dataType: dataType)
133133
default:
134-
// todo throw
135-
null
134+
// should not reach this, the openapi parser ignores parameters with unknown type.
135+
throw new UnknownParameterTypeException(parameter.name, parameter.in)
136136
}
137137
}
138138

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2019 the original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.github.hauner.openapi.spring.converter
18+
19+
/**
20+
* thrown when the ApiConverter hits an unknown parameter type.
21+
*
22+
* @author Martin Hauner
23+
*/
24+
class UnknownParameterTypeException extends RuntimeException {
25+
26+
String name
27+
String type
28+
29+
UnknownParameterTypeException(String name, String type) {
30+
super()
31+
this.name = name
32+
this.type = type
33+
}
34+
35+
@Override
36+
String getMessage () {
37+
"unknown parameter type: $type for parameter $name"
38+
}
39+
40+
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.github.hauner.openapi.spring.converter
1818

19+
import spock.lang.Ignore
1920
import spock.lang.Specification
2021

2122
import static com.github.hauner.openapi.spring.support.OpenApiParser.parse
@@ -182,4 +183,38 @@ paths:
182183
param.annotationWithPackage == 'org.springframework.web.bind.annotation.CookieValue'
183184
}
184185

186+
@Ignore("the openapi parser ignores parameters with unknown types")
187+
void "throws on unknown parameter"() {
188+
def openApi = parse (
189+
"""\
190+
openapi: 3.0.2
191+
info:
192+
title: test unknown parameter type
193+
version: 1.0.0
194+
195+
paths:
196+
/endpoint:
197+
198+
get:
199+
tags:
200+
- endpoint
201+
parameters:
202+
- name: foo
203+
description: unknown, required, string
204+
in: unknown
205+
schema:
206+
type: string
207+
responses:
208+
'204':
209+
description: empty
210+
""")
211+
212+
when:
213+
new ApiConverter ().convert (openApi)
214+
215+
then:
216+
def e = thrown (UnknownParameterTypeException)
217+
e.name == 'foo'
218+
e.type == 'unknown'
219+
}
185220
}

0 commit comments

Comments
 (0)