Skip to content

Commit db9d7c6

Browse files
authored
Merge pull request #26 from hauner/#22
resolves #22
2 parents 1c97f94 + f05ae83 commit db9d7c6

4 files changed

Lines changed: 150 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 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.model.Api
2020
import com.github.hauner.openapi.spring.model.Endpoint
21+
import com.github.hauner.openapi.spring.model.parameters.HeaderParameter
2122
import com.github.hauner.openapi.spring.model.parameters.Parameter as ModelParameter
2223
import com.github.hauner.openapi.spring.model.parameters.PathParameter
2324
import com.github.hauner.openapi.spring.model.parameters.QueryParameter
@@ -124,6 +125,8 @@ class ApiConverter {
124125
return new QueryParameter (name: parameter.name, required: parameter.required, dataType: dataType)
125126
case 'path':
126127
return new PathParameter (name: parameter.name, required: parameter.required, dataType: dataType)
128+
case 'header':
129+
return new HeaderParameter (name: parameter.name, required: parameter.required, dataType: dataType)
127130
default:
128131
// todo throw
129132
null
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.model.parameters
18+
19+
import com.github.hauner.openapi.spring.model.datatypes.DataType
20+
21+
/**
22+
* OpenAPI header parameter.
23+
*
24+
* @author Martin Hauner
25+
*/
26+
class HeaderParameter implements Parameter {
27+
String name
28+
boolean required
29+
DataType dataType
30+
31+
String getAnnotationName () {
32+
"RequestHeader"
33+
}
34+
35+
String getAnnotationWithPackage () {
36+
"org.springframework.web.bind.annotation.${annotationName}"
37+
}
38+
39+
String getAnnotation () {
40+
"@${annotationName}"
41+
}
42+
43+
Set<String> getDataTypeImports () {
44+
dataType.imports
45+
}
46+
47+
/**
48+
* Is the parameter required?
49+
*
50+
* @return true if the parameter is required, otherwise false.
51+
*/
52+
boolean isRequired () {
53+
required
54+
}
55+
56+
/**
57+
* Create annotation?
58+
*/
59+
boolean withAnnotation () {
60+
true
61+
}
62+
63+
/**
64+
* Create annotation with parameters?
65+
*
66+
* @return true if the annotation should have parameters, false otherwise
67+
*/
68+
boolean withParameters () {
69+
true
70+
}
71+
72+
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,44 @@ paths:
102102
param.annotationWithPackage == 'org.springframework.web.bind.annotation.PathVariable'
103103
}
104104

105+
void "converts simple header parameter"() {
106+
def openApi = parse (
107+
"""\
108+
openapi: 3.0.2
109+
info:
110+
title: test simple header parameter
111+
version: 1.0.0
112+
113+
paths:
114+
/endpoint:
115+
116+
get:
117+
tags:
118+
- endpoint
119+
parameters:
120+
- name: x-foo
121+
description: header, required, string
122+
in: header
123+
required: true
124+
schema:
125+
type: string
126+
responses:
127+
'204':
128+
description: empty
129+
""")
130+
131+
when:
132+
def api = new ApiConverter ().convert (openApi)
133+
134+
then:
135+
def itf = api.interfaces.first ()
136+
def ep = itf.endpoints.first ()
137+
def param = ep.parameters.first ()
138+
param.name == 'x-foo'
139+
param.required
140+
param.dataType.name == 'String'
141+
param.annotation == '@RequestHeader'
142+
param.annotationWithPackage == 'org.springframework.web.bind.annotation.RequestHeader'
143+
}
144+
105145
}

src/test/groovy/com/github/hauner/openapi/spring/writer/MethodWriterSpec.groovy

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import com.github.hauner.openapi.spring.model.datatypes.NoneDataType
3232
import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
3333
import com.github.hauner.openapi.spring.model.datatypes.SetDataType
3434
import com.github.hauner.openapi.spring.model.datatypes.StringDataType
35+
import com.github.hauner.openapi.spring.model.parameters.HeaderParameter
3536
import com.github.hauner.openapi.spring.model.parameters.QueryParameter
3637
import spock.lang.Specification
3738
import spock.lang.Unroll
@@ -179,6 +180,40 @@ class MethodWriterSpec extends Specification {
179180
"""
180181
}
181182
183+
void "writes simple (required) header parameter" () {
184+
def endpoint = new Endpoint (path: '/foo', method: HttpMethod.GET, responses: [
185+
new Response (contentType: 'application/json', responseType: new NoneDataType())
186+
], parameters: [
187+
new HeaderParameter(name: 'x-foo', required: true, dataType: new StringDataType())
188+
])
189+
190+
when:
191+
writer.write (target, endpoint)
192+
193+
then:
194+
target.toString () == """\
195+
@GetMapping(path = "${endpoint.path}")
196+
ResponseEntity<void> getFoo(@RequestHeader(name = "x-foo") String xFoo);
197+
"""
198+
}
199+
200+
void "writes simple (optional) header parameter" () {
201+
def endpoint = new Endpoint (path: '/foo', method: HttpMethod.GET, responses: [
202+
new Response (contentType: 'application/json', responseType: new NoneDataType())
203+
], parameters: [
204+
new HeaderParameter(name: 'x-foo', required: false, dataType: new StringDataType())
205+
])
206+
207+
when:
208+
writer.write (target, endpoint)
209+
210+
then:
211+
target.toString () == """\
212+
@GetMapping(path = "${endpoint.path}")
213+
ResponseEntity<void> getFoo(@RequestHeader(name = "x-foo", required = false) String xFoo);
214+
"""
215+
}
216+
182217
void "writes object query parameter without @RequestParam annotation" () {
183218
def endpoint = new Endpoint (path: '/foo', method: HttpMethod.GET, responses: [
184219
new Response (contentType: 'application/json', responseType: new NoneDataType())

0 commit comments

Comments
 (0)