Skip to content

Commit dd0e69c

Browse files
committed
object parameters should not have @QueryParam annotation
1 parent deab753 commit dd0e69c

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

src/main/groovy/com/github/hauner/openapi/spring/model/parameters/QueryParameter.groovy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.github.hauner.openapi.spring.model.parameters
1818

1919
import com.github.hauner.openapi.spring.model.datatypes.DataType
2020
import com.github.hauner.openapi.spring.model.datatypes.MapDataType
21+
import com.github.hauner.openapi.spring.model.datatypes.MappedDataType
2122
import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
2223

2324
/**
@@ -56,6 +57,19 @@ class QueryParameter {
5657
}
5758

5859
/**
60+
* Create annotation? If the query parameter is mapped to a pojo object it should not have a
61+
* {@code @RequestParam} annotation.
62+
*/
63+
boolean withAnnotation () {
64+
! (
65+
dataType instanceof ObjectDataType
66+
|| dataType instanceof MappedDataType
67+
)
68+
}
69+
70+
/**
71+
* todo this is not right.
72+
*
5973
* Create annotation with parameters? If the query parameter is mapped to a pojo object it
6074
* should not have any parameters.
6175
*

src/main/groovy/com/github/hauner/openapi/spring/writer/InterfaceWriter.groovy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ class InterfaceWriter {
5757
imports.add (ep.method.classNameWithPackage)
5858

5959
ep.parameters.each { p ->
60-
imports.add (p.annotationWithPackage)
60+
if (p.withAnnotation()) {
61+
imports.add (p.annotationWithPackage)
62+
}
6163
}
6264

6365
if (!ep.response.empty) {

src/main/groovy/com/github/hauner/openapi/spring/writer/MethodWriter.groovy

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,13 @@ class MethodWriter {
7777

7878
private String createParameter (Endpoint endpoint) {
7979
def ps = endpoint.parameters.collect {
80-
"${createParameterAnnotation (it)} ${it.dataType.name} ${it.name}"
80+
81+
if (it.withAnnotation ()) {
82+
"${createParameterAnnotation (it)} ${it.dataType.name} ${it.name}"
83+
} else {
84+
"${it.dataType.name} ${it.name}"
85+
}
86+
8187
}
8288

8389
ps.join (', ')

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.github.hauner.openapi.spring.model.Endpoint
2020
import com.github.hauner.openapi.spring.model.HttpMethod
2121
import com.github.hauner.openapi.spring.model.Interface
2222
import com.github.hauner.openapi.spring.model.Response
23+
import com.github.hauner.openapi.spring.model.datatypes.NoneDataType
2324
import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
2425
import com.github.hauner.openapi.spring.model.datatypes.StringDataType
2526
import com.github.hauner.openapi.spring.model.parameters.QueryParameter
@@ -134,6 +135,30 @@ import org.springframework.web.bind.annotation.RequestParam;
134135
""")
135136
}
136137

138+
void "does not write @RequestParam annotation import of request parameter that does not want the annotation" () {
139+
def endpoint = new Endpoint (path: '/foo', method: HttpMethod.GET, responses: [
140+
new Response (contentType: 'application/json', responseType: new NoneDataType())
141+
], parameters: [
142+
new QueryParameter(name: 'foo', required: false, dataType: new ObjectDataType (
143+
type: 'Foo', properties: [
144+
foo1: new StringDataType (),
145+
foo2: new StringDataType ()
146+
]
147+
))
148+
])
149+
150+
def apiItf = new Interface (name: 'name', endpoints: [endpoint])
151+
152+
when:
153+
writer.write (target, apiItf)
154+
155+
then:
156+
def result = extractImports (target.toString ())
157+
! result.contains("""\
158+
import org.springframework.web.bind.annotation.RequestParam;
159+
""")
160+
}
161+
137162
void "writes model import"() {
138163
def pkg = 'model.package'
139164
def type = 'Model'

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class MethodWriterSpec extends Specification {
179179
"""
180180
}
181181
182-
void "writes object query parameter" () {
182+
void "writes object query parameter without @RequestParam annotation" () {
183183
def endpoint = new Endpoint (path: '/foo', method: HttpMethod.GET, responses: [
184184
new Response (contentType: 'application/json', responseType: new NoneDataType())
185185
], parameters: [
@@ -197,7 +197,7 @@ class MethodWriterSpec extends Specification {
197197
then:
198198
target.toString () == """\
199199
@GetMapping(path = "${endpoint.path}")
200-
ResponseEntity<void> getFoo(@RequestParam Foo foo);
200+
ResponseEntity<void> getFoo(Foo foo);
201201
"""
202202
}
203203

0 commit comments

Comments
 (0)