Skip to content

Commit 2548596

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

8 files changed

Lines changed: 61 additions & 12 deletions

File tree

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,26 +173,26 @@ class DataTypeConverter {
173173
}
174174

175175
def defaultValue = schemaInfo.defaultValue
176-
def constraints = defaultValue ? new DataTypeConstraints(defaultValue: defaultValue) : null
176+
def constraints = defaultValue != null ? new DataTypeConstraints(defaultValue: defaultValue) : null
177177

178178
def simpleType
179179
switch (typeFormat) {
180180
case 'integer':
181181
case 'integer/int32':
182-
simpleType = new IntegerDataType ()
182+
simpleType = new IntegerDataType (constraints: constraints)
183183
break
184184
case 'integer/int64':
185-
simpleType = new LongDataType ()
185+
simpleType = new LongDataType (constraints: constraints)
186186
break
187187
case 'number':
188188
case 'number/float':
189-
simpleType = new FloatDataType ()
189+
simpleType = new FloatDataType (constraints: constraints)
190190
break
191191
case 'number/double':
192-
simpleType = new DoubleDataType ()
192+
simpleType = new DoubleDataType (constraints: constraints)
193193
break
194194
case 'boolean':
195-
simpleType = new BooleanDataType ()
195+
simpleType = new BooleanDataType (constraints: constraints)
196196
break
197197
case 'string':
198198
simpleType = createStringDataType (schemaInfo, constraints, dataTypes)

src/main/groovy/com/github/hauner/openapi/spring/model/datatypes/BooleanDataType.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 BooleanDataType implements DataType {
2525

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

48+
@Override
49+
DataTypeConstraints getConstraints() {
50+
constraints
51+
}
52+
4653
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class DataTypeConstraints {
2323

2424
def defaultValue
2525

26-
String getDefault () {
26+
def getDefault () {
2727
defaultValue
2828
}
2929

src/main/groovy/com/github/hauner/openapi/spring/model/datatypes/DoubleDataType.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 DoubleDataType implements DataType {
2525

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

48+
@Override
49+
DataTypeConstraints getConstraints() {
50+
constraints
51+
}
52+
4653
}

src/main/groovy/com/github/hauner/openapi/spring/model/datatypes/FloatDataType.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 FloatDataType implements DataType {
2525

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

48+
@Override
49+
DataTypeConstraints getConstraints() {
50+
constraints
51+
}
52+
4653
}

src/main/groovy/com/github/hauner/openapi/spring/model/datatypes/IntegerDataType.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 IntegerDataType implements DataType {
2525

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

48+
@Override
49+
DataTypeConstraints getConstraints() {
50+
constraints
51+
}
52+
4653
}

src/main/groovy/com/github/hauner/openapi/spring/model/datatypes/LongDataType.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 LongDataType implements DataType {
2525

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

48+
@Override
49+
DataTypeConstraints getConstraints() {
50+
constraints
51+
}
52+
4653
}

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,29 @@ 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')
66+
@Unroll
67+
void "converts schema(#type, #format, #dflt) to javaType with default value" () {
68+
Schema schema = new Schema(type: type, format: format, _default: dflt)
6869
6970
when:
70-
def datatype = converter.convert (new SchemaInfo (null, schema, 'String'), new DataTypes())
71+
def datatype = converter.convert (
72+
new SchemaInfo (null, schema, javaType), new DataTypes()
73+
)
7174
7275
then:
73-
datatype.name == 'String'
74-
datatype.constraints.default == 'bar'
76+
datatype.name == javaType
77+
datatype.constraints.default == dflt
78+
79+
where:
80+
type | format | dflt | javaType
81+
'string' | null | 'foo' | 'String'
82+
'integer' | null | 101 | 'Integer'
83+
'integer' | 'int32' | 102 | 'Integer'
84+
'integer' | 'int64' | 103 | 'Long'
85+
'number' | null | 10.1 | 'Float'
86+
'number' | 'float' | 10.2 | 'Float'
87+
'number' | 'double' | 10.3 | 'Double'
88+
'boolean' | null | false | 'Boolean'
7589
}
7690
7791
void "throws when hitting an unknown data type" () {

0 commit comments

Comments
 (0)