Skip to content

Commit 6aee9a7

Browse files
authored
Merge pull request #27 from hauner/#23
resolves #23, handle cookie parameter
2 parents 95f4168 + e607152 commit 6aee9a7

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.CookieParameter
2122
import com.github.hauner.openapi.spring.model.parameters.HeaderParameter
2223
import com.github.hauner.openapi.spring.model.parameters.Parameter as ModelParameter
2324
import com.github.hauner.openapi.spring.model.parameters.PathParameter
@@ -127,6 +128,8 @@ class ApiConverter {
127128
return new PathParameter (name: parameter.name, required: parameter.required, dataType: dataType)
128129
case 'header':
129130
return new HeaderParameter (name: parameter.name, required: parameter.required, dataType: dataType)
131+
case 'cookie':
132+
return new CookieParameter (name: parameter.name, required: parameter.required, dataType: dataType)
130133
default:
131134
// todo throw
132135
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 cookie parameter.
23+
*
24+
* @author Martin Hauner
25+
*/
26+
class CookieParameter implements Parameter {
27+
String name
28+
boolean required
29+
DataType dataType
30+
31+
String getAnnotationName () {
32+
"CookieValue"
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
@@ -142,4 +142,44 @@ paths:
142142
param.annotationWithPackage == 'org.springframework.web.bind.annotation.RequestHeader'
143143
}
144144

145+
void "converts simple cookie parameter"() {
146+
def openApi = parse (
147+
"""\
148+
openapi: 3.0.2
149+
info:
150+
title: test simple cookie parameter
151+
version: 1.0.0
152+
153+
paths:
154+
/endpoint:
155+
156+
get:
157+
tags:
158+
- endpoint
159+
parameters:
160+
- name: foo
161+
description: cookie, required, string
162+
in: cookie
163+
required: true
164+
schema:
165+
type: string
166+
responses:
167+
'204':
168+
description: empty
169+
""")
170+
171+
when:
172+
def api = new ApiConverter ().convert (openApi)
173+
174+
then:
175+
def itf = api.interfaces.first ()
176+
def ep = itf.endpoints.first ()
177+
def param = ep.parameters.first ()
178+
param.name == 'foo'
179+
param.required
180+
param.dataType.name == 'String'
181+
param.annotation == '@CookieValue'
182+
param.annotationWithPackage == 'org.springframework.web.bind.annotation.CookieValue'
183+
}
184+
145185
}

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.CookieParameter
3536
import com.github.hauner.openapi.spring.model.parameters.HeaderParameter
3637
import com.github.hauner.openapi.spring.model.parameters.QueryParameter
3738
import spock.lang.Specification
@@ -214,6 +215,40 @@ class MethodWriterSpec extends Specification {
214215
"""
215216
}
216217
218+
void "writes simple (required) cookie parameter" () {
219+
def endpoint = new Endpoint (path: '/foo', method: HttpMethod.GET, responses: [
220+
new Response (contentType: 'application/json', responseType: new NoneDataType())
221+
], parameters: [
222+
new CookieParameter(name: 'foo', required: true, dataType: new StringDataType())
223+
])
224+
225+
when:
226+
writer.write (target, endpoint)
227+
228+
then:
229+
target.toString () == """\
230+
@GetMapping(path = "${endpoint.path}")
231+
ResponseEntity<void> getFoo(@CookieValue(name = "foo") String foo);
232+
"""
233+
}
234+
235+
void "writes simple (optional) cookie parameter" () {
236+
def endpoint = new Endpoint (path: '/foo', method: HttpMethod.GET, responses: [
237+
new Response (contentType: 'application/json', responseType: new NoneDataType())
238+
], parameters: [
239+
new CookieParameter(name: 'foo', required: false, dataType: new StringDataType())
240+
])
241+
242+
when:
243+
writer.write (target, endpoint)
244+
245+
then:
246+
target.toString () == """\
247+
@GetMapping(path = "${endpoint.path}")
248+
ResponseEntity<void> getFoo(@CookieValue(name = "foo", required = false) String foo);
249+
"""
250+
}
251+
217252
void "writes object query parameter without @RequestParam annotation" () {
218253
def endpoint = new Endpoint (path: '/foo', method: HttpMethod.GET, responses: [
219254
new Response (contentType: 'application/json', responseType: new NoneDataType())

0 commit comments

Comments
 (0)