Skip to content

Commit 55b87ec

Browse files
committed
write parameter default value
1 parent 2dbc59d commit 55b87ec

10 files changed

Lines changed: 154 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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.datatypes
18+
19+
/**
20+
* OpenAPI constraint details of a data type.
21+
*/
22+
interface DataTypeConstraints<T> {
23+
24+
T getDefault()
25+
26+
}

src/main/groovy/com/github/hauner/openapi/spring/model/datatypes/StringDataType.groovy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ package com.github.hauner.openapi.spring.model.datatypes
2323
*/
2424
class StringDataType implements DataType {
2525

26+
StringDataTypeConstraints constraints
27+
2628
@Override
2729
String getName () {
2830
'String'
@@ -43,4 +45,9 @@ class StringDataType implements DataType {
4345
[]
4446
}
4547

48+
@Override
49+
DataTypeConstraints<String> getConstraints() {
50+
constraints
51+
}
52+
4653
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.datatypes;
18+
19+
/**
20+
* OpenAPI constraints of {@link StringDataType}.
21+
*
22+
* @author Martin Hauner
23+
*/
24+
class StringDataTypeConstraints implements DataTypeConstraints<String> {
25+
26+
def dfault
27+
28+
@Override
29+
String getDefault () {
30+
dfault
31+
}
32+
33+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class CookieParameter implements Parameter {
5353
required
5454
}
5555

56+
ParameterConstraints getConstraints() {
57+
null
58+
}
59+
5660
/**
5761
* Create annotation?
5862
*/

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class HeaderParameter implements Parameter {
5353
required
5454
}
5555

56+
ParameterConstraints getConstraints() {
57+
null
58+
}
59+
5660
/**
5761
* Create annotation?
5862
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.DataTypeConstraints
20+
21+
/**
22+
* Constraints of the parameter data type.
23+
*
24+
* @author Martin Hauner
25+
*/
26+
class ParameterConstraints {
27+
28+
DataTypeConstraints constraints
29+
30+
boolean hasDefault() {
31+
constraints != null
32+
}
33+
34+
def getDefault() {
35+
constraints.default
36+
}
37+
38+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class PathParameter implements Parameter {
5353
required
5454
}
5555

56+
ParameterConstraints getConstraints() {
57+
null
58+
}
59+
5660
/**
5761
* Create annotation?
5862
*/

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class QueryParameter implements Parameter {
6060
required
6161
}
6262

63+
ParameterConstraints getConstraints() {
64+
new ParameterConstraints(constraints: dataType.constraints)
65+
}
66+
6367
/**
6468
* Create annotation? If the query parameter is mapped to a pojo object it should not have a
6569
* {@code @RequestParam} annotation.

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ class MethodWriter {
7070
param += 'required = false'
7171
}
7272

73+
if (!parameter.required && parameter.constraints?.hasDefault()) {
74+
param += ", "
75+
param += "default = ${getDefault(parameter)}"
76+
}
77+
7378
param += ')'
7479
param
7580
}
@@ -116,4 +121,13 @@ class MethodWriter {
116121
'"' + content + '"'
117122
}
118123

124+
private def getDefault(Parameter parameter) {
125+
def value = parameter.constraints.default
126+
if (value instanceof String) {
127+
quote(value)
128+
} else {
129+
value
130+
}
131+
}
132+
119133
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import com.github.hauner.openapi.spring.model.datatypes.NoneDataType
3333
import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
3434
import com.github.hauner.openapi.spring.model.datatypes.SetDataType
3535
import com.github.hauner.openapi.spring.model.datatypes.StringDataType
36+
import com.github.hauner.openapi.spring.model.datatypes.StringDataTypeConstraints
3637
import com.github.hauner.openapi.spring.model.parameters.CookieParameter
3738
import com.github.hauner.openapi.spring.model.parameters.HeaderParameter
3839
import com.github.hauner.openapi.spring.model.parameters.QueryParameter
@@ -366,4 +367,23 @@ class MethodWriterSpec extends Specification {
366367
"""
367368
}
368369
370+
void "writes simple (optional) parameter with default value" () {
371+
def endpoint = new Endpoint (path: '/foo', method: HttpMethod.GET, responses: [
372+
new Response (contentType: 'application/json', responseType: new NoneDataType())
373+
], parameters: [
374+
new QueryParameter(name: 'foo', required: false,
375+
dataType: new StringDataType(
376+
constraints: new StringDataTypeConstraints (dfault: 'bar')))
377+
])
378+
379+
when:
380+
writer.write (target, endpoint)
381+
382+
then:
383+
target.toString () == """\
384+
@GetMapping(path = "${endpoint.path}")
385+
ResponseEntity<void> getFoo(@RequestParam(name = "foo", required = false, default = "bar") String foo);
386+
"""
387+
}
388+
369389
}

0 commit comments

Comments
 (0)