Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit feac2c3

Browse files
committed
write additional annotation of an additional parameter
1 parent edc0de5 commit feac2c3

File tree

5 files changed

+175
-1
lines changed

5 files changed

+175
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2020 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.core.model.datatypes
18+
19+
/**
20+
* additional annotation type.
21+
*
22+
* @author Martin Hauner
23+
*/
24+
class AnnotationDataType implements DataType {
25+
26+
protected String type
27+
String pkg = 'unknown'
28+
String parameters
29+
30+
@Override
31+
String getName () {
32+
"@$type"
33+
}
34+
35+
@Override
36+
String getPackageName () {
37+
pkg
38+
}
39+
40+
String getParameters () {
41+
parameters ?: ""
42+
}
43+
44+
@Override
45+
Set<String> getImports () {
46+
[[packageName, type].join ('.')]
47+
}
48+
49+
@Override
50+
Set<String> getReferencedImports () {
51+
[]
52+
}
53+
54+
}

src/main/groovy/com/github/hauner/openapi/core/model/parameters/AdditionalParameter.groovy

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

1717
package com.github.hauner.openapi.core.model.parameters
1818

19+
import com.github.hauner.openapi.core.model.datatypes.AnnotationDataType
20+
1921
/**
2022
* default implementation of am additional parameter.
2123
*
@@ -25,8 +27,14 @@ package com.github.hauner.openapi.core.model.parameters
2527
*/
2628
class AdditionalParameter extends ParameterBase {
2729

30+
AnnotationDataType annotationDataType
31+
2832
boolean withAnnotation () {
2933
false
3034
}
3135

36+
boolean hasAdditionalAnnotation () {
37+
annotationDataType != null
38+
}
39+
3240
}

src/main/groovy/com/github/hauner/openapi/core/writer/java/MethodWriter.groovy

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

1717
package com.github.hauner.openapi.core.writer.java
1818

19+
import com.github.hauner.openapi.core.model.parameters.AdditionalParameter
1920
import com.github.hauner.openapi.core.model.parameters.Parameter
2021
import com.github.hauner.openapi.core.writer.java.MappingAnnotationWriter as CoreMappingAnnotationWriter
2122
import com.github.hauner.openapi.core.writer.java.ParameterAnnotationWriter as CoreParameterAnnotationWriter
@@ -108,7 +109,13 @@ class MethodWriter {
108109
if (parameter.deprecated) {
109110
annotation.write ("@Deprecated ")
110111
}
112+
111113
parameterAnnotationWriter.write (annotation, parameter)
114+
115+
if (parameter instanceof AdditionalParameter && parameter.hasAdditionalAnnotation ()) {
116+
annotation.write (" ${parameter.annotationDataType.name}${parameter.annotationDataType.parameters}")
117+
}
118+
112119
annotation.toString ()
113120
}
114121

src/test/groovy/com/github/hauner/openapi/core/writer/java/MethodWriterSpec.groovy

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.github.hauner.openapi.core.writer.java
1818

1919
import com.github.hauner.openapi.core.converter.ApiOptions
20+
import com.github.hauner.openapi.core.model.datatypes.AnnotationDataType
2021
import com.github.hauner.openapi.core.model.datatypes.NoneDataType
2122
import com.github.hauner.openapi.core.model.datatypes.ResultDataType
2223
import com.github.hauner.openapi.core.model.parameters.Parameter
@@ -192,7 +193,7 @@ class MethodWriterSpec extends Specification {
192193
"""
193194
}
194195
195-
void "does not write parameter annotation if empty" () {
196+
void "writes no parameter annotation if the annotation writer skips it" () {
196197
def stubWriter = Stub (ParameterAnnotationWriter) {}
197198
198199
writer.parameterAnnotationWriter = stubWriter
@@ -213,6 +214,30 @@ class MethodWriterSpec extends Specification {
213214
"""
214215
}
215216
217+
void "writes additional parameter annotation" () {
218+
def endpoint = endpoint('/foo') {
219+
parameters {
220+
add {
221+
name ('foo')
222+
type (new StringDataType())
223+
annotation (new AnnotationDataType (pkg: 'oap', type: 'Foo', parameters: '()'))
224+
}
225+
}
226+
responses ('204') {
227+
empty ()
228+
}
229+
}
230+
231+
when:
232+
writer.write (target, endpoint, endpoint.endpointResponses.first ())
233+
234+
then:
235+
target.toString () == """\
236+
@CoreMapping
237+
void getFoo(@Parameter @Foo() String foo);
238+
"""
239+
}
240+
216241
void "writes method name from path with valid java identifiers" () {
217242
def endpoint = createEndpoint (path: '/f_o-ooo/b_a-rrr', method: HttpMethod.GET, responses: [
218243
'204': [new EmptyResponse ()]

src/test/groovy/io/openapiprocessor/core/model/Builder.groovy

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ import com.github.hauner.openapi.core.model.HttpMethod
2121
import com.github.hauner.openapi.core.model.Interface
2222
import com.github.hauner.openapi.core.model.RequestBody
2323
import com.github.hauner.openapi.core.model.Response
24+
import com.github.hauner.openapi.core.model.datatypes.AnnotationDataType
2425
import com.github.hauner.openapi.core.model.datatypes.DataType
2526
import com.github.hauner.openapi.core.model.datatypes.NoneDataType
2627
import com.github.hauner.openapi.core.model.datatypes.ObjectDataType
28+
import com.github.hauner.openapi.core.model.parameters.AdditionalParameter
2729
import com.github.hauner.openapi.core.model.parameters.MultipartParameter
2830
import com.github.hauner.openapi.core.model.parameters.Parameter
31+
import com.github.hauner.openapi.core.model.parameters.QueryParameter
2932

3033
class Builder {
3134

@@ -90,6 +93,15 @@ class EndpointBuilder {
9093
parameters.addAll (builder.buildParameters ())
9194
}
9295

96+
void parameters (@DelegatesTo(strategy = Closure.DELEGATE_ONLY, value = ParametersBuilder) Closure init) {
97+
def builder = new ParametersBuilder()
98+
def code = init.rehydrate (builder, this, this)
99+
code.resolveStrategy = Closure.DELEGATE_ONLY
100+
code()
101+
def params = builder.build ()
102+
parameters.addAll (params)
103+
}
104+
93105
void responses (String httpStatus, @DelegatesTo(strategy = Closure.DELEGATE_ONLY, value = ResponsesBuilder) Closure init) {
94106
def builder = new ResponsesBuilder()
95107
def code = init.rehydrate (builder, this, this)
@@ -180,6 +192,74 @@ class BodyBuilder {
180192
}
181193

182194

195+
class ParametersBuilder {
196+
private List<Parameter> parameters = []
197+
198+
void query (@DelegatesTo(strategy = Closure.DELEGATE_ONLY, value = QueryParameterBuilder) Closure init) {
199+
def builder = new QueryParameterBuilder()
200+
def code = init.rehydrate (builder, this, this)
201+
code.resolveStrategy = Closure.DELEGATE_ONLY
202+
code()
203+
def parameter = builder.build ()
204+
parameters.add (parameter)
205+
}
206+
207+
void add (@DelegatesTo(strategy = Closure.DELEGATE_ONLY, value = QueryParameterBuilder) Closure init) {
208+
def builder = new AddParameterBuilder()
209+
def code = init.rehydrate (builder, this, this)
210+
code.resolveStrategy = Closure.DELEGATE_ONLY
211+
code()
212+
def parameter = builder.build ()
213+
parameters.add (parameter)
214+
}
215+
216+
List<Parameter> build () {
217+
parameters
218+
}
219+
220+
}
221+
222+
class QueryParameterBuilder {
223+
private String name
224+
private DataType type
225+
226+
void name (String name) {
227+
this.name = name
228+
}
229+
230+
void type (DataType dataType) {
231+
this.type = dataType
232+
}
233+
234+
QueryParameter build () {
235+
new QueryParameter(name: name, dataType: dataType)
236+
}
237+
238+
}
239+
240+
class AddParameterBuilder {
241+
private String name
242+
private DataType type
243+
private AnnotationDataType annotationType
244+
245+
void name (String name) {
246+
this.name = name
247+
}
248+
249+
void type (DataType dataType) {
250+
this.type = dataType
251+
}
252+
253+
void annotation (AnnotationDataType dataType) {
254+
annotationType = dataType
255+
}
256+
257+
AdditionalParameter build () {
258+
new AdditionalParameter (name: name, dataType: type, annotationDataType: annotationType)
259+
}
260+
261+
}
262+
183263

184264
class ResponsesBuilder {
185265
private List<Response> responses = []

0 commit comments

Comments
 (0)