Skip to content

Commit 43ea482

Browse files
committed
add schema default value to data type
1 parent d5feeb8 commit 43ea482

8 files changed

Lines changed: 45 additions & 43 deletions

File tree

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import com.github.hauner.openapi.spring.model.DataTypes
2929
import com.github.hauner.openapi.spring.model.datatypes.ArrayDataType
3030
import com.github.hauner.openapi.spring.model.datatypes.BooleanDataType
3131
import com.github.hauner.openapi.spring.model.datatypes.CollectionDataType
32+
import com.github.hauner.openapi.spring.model.datatypes.DataTypeConstraints
3233
import com.github.hauner.openapi.spring.model.datatypes.ListDataType
3334
import com.github.hauner.openapi.spring.model.datatypes.LocalDateDataType
3435
import com.github.hauner.openapi.spring.model.datatypes.MapDataType
@@ -171,6 +172,9 @@ class DataTypeConverter {
171172
typeFormat += '/' + schemaInfo.format
172173
}
173174

175+
def defaultValue = schemaInfo.defaultValue
176+
def constraints = defaultValue ? new DataTypeConstraints(defaultValue: defaultValue) : null
177+
174178
def simpleType
175179
switch (typeFormat) {
176180
case 'integer':
@@ -191,7 +195,7 @@ class DataTypeConverter {
191195
simpleType = new BooleanDataType ()
192196
break
193197
case 'string':
194-
simpleType = createStringDataType (schemaInfo, dataTypes)
198+
simpleType = createStringDataType (schemaInfo, constraints, dataTypes)
195199
break
196200
case 'string/date':
197201
simpleType = new LocalDateDataType ()
@@ -206,17 +210,18 @@ class DataTypeConverter {
206210
simpleType
207211
}
208212

209-
private DataType createStringDataType (SchemaInfo info, DataTypes dataTypes) {
213+
private DataType createStringDataType (SchemaInfo info, DataTypeConstraints constraints, DataTypes dataTypes) {
210214
if (!info.isEnum()) {
211-
return new StringDataType ()
215+
return new StringDataType (constraints: constraints)
212216
}
213217

214218
// in case of an inline definition the name may be lowercase, make sure the enum
215219
// class gets an uppercase name!
216220
def enumType = new StringEnumDataType (
217221
type: info.name.capitalize (),
218222
pkg: [options.packageName, 'model'].join ('.'),
219-
values: info.enumValues)
223+
values: info.enumValues,
224+
constraints: constraints)
220225

221226
dataTypes.add (enumType)
222227
enumType

src/main/groovy/com/github/hauner/openapi/spring/converter/schema/SchemaInfo.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ class SchemaInfo {
125125
schema.$ref
126126
}
127127

128+
/**
129+
* get default value.
130+
*
131+
* @return default value or null
132+
*/
133+
def getDefaultValue() {
134+
schema.default
135+
}
136+
128137
/**
129138
* get the custom Java type (fully qualified) defined via the {@code x-java-type} OpenAPI
130139
* extension. If no {@code x-java-type} is set the result is {@code null}.

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ package com.github.hauner.openapi.spring.model.datatypes
1919
/**
2020
* OpenAPI constraint details of a data type.
2121
*/
22-
interface DataTypeConstraints<T> {
22+
class DataTypeConstraints {
2323

24-
T getDefault()
24+
def defaultValue
25+
26+
String getDefault () {
27+
defaultValue
28+
}
2529

2630
}

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

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

26-
StringDataTypeConstraints constraints
26+
DataTypeConstraints constraints
2727

2828
@Override
2929
String getName () {
@@ -46,7 +46,7 @@ class StringDataType implements DataType {
4646
}
4747

4848
@Override
49-
DataTypeConstraints<String> getConstraints() {
49+
DataTypeConstraints getConstraints() {
5050
constraints
5151
}
5252

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

Lines changed: 0 additions & 33 deletions
This file was deleted.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class StringEnumDataType implements DataType {
2626
String type
2727
String pkg = 'unknown'
2828
List<String> values = []
29+
DataTypeConstraints constraints
2930

3031
@Override
3132
String getName () {
@@ -47,6 +48,11 @@ class StringEnumDataType implements DataType {
4748
[]
4849
}
4950

51+
@Override
52+
DataTypeConstraints getConstraints() {
53+
constraints
54+
}
55+
5056
}
5157

5258

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ class DataTypeConverterSpec extends Specification {
6363
'boolean' | null | 'Boolean'
6464
}
6565
66+
void "converts primitive schema with default value" () {
67+
Schema schema = new Schema(type: 'string', _default: 'bar')
68+
69+
when:
70+
def datatype = converter.convert (new SchemaInfo (null, schema, 'String'), new DataTypes())
71+
72+
then:
73+
datatype.name == 'String'
74+
datatype.constraints.default == 'bar'
75+
}
76+
6677
void "throws when hitting an unknown data type" () {
6778
Schema schema = new Schema(type: type, format: format)
6879

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
@@ -22,6 +22,7 @@ import com.github.hauner.openapi.spring.model.RequestBody
2222
import com.github.hauner.openapi.spring.model.Response
2323
import com.github.hauner.openapi.spring.model.datatypes.BooleanDataType
2424
import com.github.hauner.openapi.spring.model.datatypes.CollectionDataType
25+
import com.github.hauner.openapi.spring.model.datatypes.DataTypeConstraints
2526
import com.github.hauner.openapi.spring.model.datatypes.DoubleDataType
2627
import com.github.hauner.openapi.spring.model.datatypes.FloatDataType
2728
import com.github.hauner.openapi.spring.model.datatypes.InlineObjectDataType
@@ -33,7 +34,6 @@ import com.github.hauner.openapi.spring.model.datatypes.NoneDataType
3334
import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
3435
import com.github.hauner.openapi.spring.model.datatypes.SetDataType
3536
import com.github.hauner.openapi.spring.model.datatypes.StringDataType
36-
import com.github.hauner.openapi.spring.model.datatypes.StringDataTypeConstraints
3737
import com.github.hauner.openapi.spring.model.parameters.CookieParameter
3838
import com.github.hauner.openapi.spring.model.parameters.HeaderParameter
3939
import com.github.hauner.openapi.spring.model.parameters.QueryParameter
@@ -373,7 +373,7 @@ class MethodWriterSpec extends Specification {
373373
], parameters: [
374374
new QueryParameter(name: 'foo', required: false,
375375
dataType: new StringDataType(
376-
constraints: new StringDataTypeConstraints (dfault: 'bar')))
376+
constraints: new DataTypeConstraints (defaultValue: 'bar')))
377377
])
378378
379379
when:

0 commit comments

Comments
 (0)