Skip to content

Commit dd38424

Browse files
authored
Merge pull request #71 from schlagi123/#34-Support-bean-validation
resolves #34 support bean validation
2 parents 112703a + db537f1 commit dd38424

28 files changed

+1018
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
out
22
build
3+
.gradle

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.github.hauner.openapi.spring.converter.mapping.Mapping
2222
* Options of the generatr.
2323
*
2424
* @author Martin Hauner
25+
* @author Bastian Wilhelm
2526
*/
2627
class ApiOptions {
2728

@@ -58,4 +59,9 @@ class ApiOptions {
5859
*/
5960
List<Mapping> typeMappings
6061

62+
/**
63+
* provide enabling Bean Validation (JSR303) annotations. Default is false (disabled)
64+
*/
65+
boolean beanValidation = false;
66+
6167
}

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import com.github.hauner.openapi.spring.model.datatypes.StringEnumDataType
5050
* Converter to map OpenAPI schemas to Java data types.
5151
*
5252
* @author Martin Hauner
53+
* @author Bastian Wilhelm
5354
*/
5455
class DataTypeConverter {
5556

@@ -94,18 +95,26 @@ class DataTypeConverter {
9495

9596
def arrayType
9697
TargetType targetType = getMappedDataType (new ArraySchemaType (schemaInfo))
98+
99+
def constraints = new DataTypeConstraints(
100+
defaultValue: schemaInfo.defaultValue,
101+
nullable: schemaInfo.nullable,
102+
minItems: schemaInfo.minItems,
103+
maxItems: schemaInfo.maxItems,
104+
)
105+
97106
switch (targetType?.typeName) {
98107
case Collection.name:
99-
arrayType = new CollectionDataType (item: item)
108+
arrayType = new CollectionDataType (item: item, constraints: constraints)
100109
break
101110
case List.name:
102-
arrayType = new ListDataType (item: item)
111+
arrayType = new ListDataType (item: item, constraints: constraints)
103112
break
104113
case Set.name:
105-
arrayType = new SetDataType (item: item)
114+
arrayType = new SetDataType (item: item, constraints: constraints)
106115
break
107116
default:
108-
arrayType = new ArrayDataType (item: item)
117+
arrayType = new ArrayDataType (item: item, constraints: constraints)
109118
}
110119

111120
arrayType
@@ -142,9 +151,14 @@ class DataTypeConverter {
142151
}
143152
}
144153

154+
def constraints = new DataTypeConstraints(
155+
nullable: schemaInfo.nullable,
156+
)
157+
145158
objectType = new ObjectDataType (
146159
type: schemaInfo.name,
147-
pkg: [options.packageName, 'model'].join ('.')
160+
pkg: [options.packageName, 'model'].join ('.'),
161+
constraints: constraints
148162
)
149163

150164
schemaInfo.eachProperty { String propName, SchemaInfo propDataTypeInfo ->
@@ -173,8 +187,16 @@ class DataTypeConverter {
173187
typeFormat += '/' + schemaInfo.format
174188
}
175189

176-
def defaultValue = schemaInfo.defaultValue
177-
def constraints = defaultValue != null ? new DataTypeConstraints(defaultValue: defaultValue) : null
190+
def constraints = new DataTypeConstraints(
191+
defaultValue: schemaInfo.defaultValue,
192+
nullable: schemaInfo.nullable,
193+
minLength: schemaInfo.minLength,
194+
maxLength: schemaInfo.maxLength,
195+
maximum: schemaInfo.maximum,
196+
exclusiveMaximum: schemaInfo.exclusiveMaximum,
197+
minimum: schemaInfo.minimum,
198+
exclusiveMinimum: schemaInfo.exclusiveMinimum,
199+
)
178200

179201
def simpleType
180202
switch (typeFormat) {

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import io.swagger.v3.oas.models.media.Schema
2626
* schema with context information, i.e. name and if this is an inline type with a generated name.
2727
*
2828
* @author Martin Hauner
29+
* @author Bastian Wilhelm
2930
*/
3031
class SchemaInfo implements MappingSchema {
3132

@@ -138,6 +139,87 @@ class SchemaInfo implements MappingSchema {
138139
schema.default
139140
}
140141

142+
/**
143+
* get nullable value.
144+
*
145+
* @return nullable value or null
146+
*/
147+
def getNullable() {
148+
schema.nullable
149+
}
150+
151+
/**
152+
* get minLength value.
153+
*
154+
* @return minLength value or null
155+
*/
156+
def getMinLength() {
157+
schema.minLength
158+
}
159+
160+
/**
161+
* get maxLength value.
162+
*
163+
* @return maxLength value or null
164+
*/
165+
def getMaxLength() {
166+
schema.maxLength
167+
}
168+
169+
/**
170+
* get minItems value.
171+
*
172+
* @return minItems value or null
173+
*/
174+
def getMinItems() {
175+
schema.minItems
176+
}
177+
178+
/**
179+
* get maxItems value.
180+
*
181+
* @return maxItems value or null
182+
*/
183+
def getMaxItems() {
184+
schema.maxItems
185+
}
186+
187+
/**
188+
* get maximum value.
189+
*
190+
* @return maximum value or null
191+
*/
192+
def getMaximum() {
193+
schema.maximum
194+
}
195+
196+
/**
197+
* get exclusiveMaximum value.
198+
*
199+
* @return exclusiveMaximum value or null
200+
*/
201+
def getExclusiveMaximum() {
202+
schema.exclusiveMaximum
203+
}
204+
205+
/**
206+
* get minimum value.
207+
*
208+
* @return minimum value or null
209+
*/
210+
def getMinimum() {
211+
schema.minimum
212+
}
213+
214+
/**
215+
* get exclusiveMinimum value.
216+
*
217+
* @return exclusiveMinimum value or null
218+
*/
219+
def getExclusiveMinimum() {
220+
schema.exclusiveMinimum
221+
}
222+
141223
/**
142224
* get the custom Java type (fully qualified) defined via the {@code x-java-type} OpenAPI
143225
* extension. If no {@code x-java-type} is set the result is {@code null}.

src/main/groovy/com/github/hauner/openapi/spring/generatr/SpringGeneratr.groovy

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.github.hauner.openapi.api.OpenApiGeneratr
2020
import com.github.hauner.openapi.spring.converter.ApiConverter
2121
import com.github.hauner.openapi.spring.converter.ApiOptions
2222
import com.github.hauner.openapi.spring.writer.ApiWriter
23+
import com.github.hauner.openapi.spring.writer.BeanValidationFactory
2324
import com.github.hauner.openapi.spring.writer.DataTypeWriter
2425
import com.github.hauner.openapi.spring.writer.HeaderWriter
2526
import com.github.hauner.openapi.spring.writer.InterfaceWriter
@@ -33,6 +34,7 @@ import io.swagger.v3.parser.core.models.SwaggerParseResult
3334
* Entry point of openapi-generatr-spring.
3435
*
3536
* @author Martin Hauner
37+
* @author Bastian Wilhelm
3638
*/
3739
class SpringGeneratr implements OpenApiGeneratr {
3840

@@ -61,11 +63,23 @@ class SpringGeneratr implements OpenApiGeneratr {
6163
def api = cv.convert (result.openAPI)
6264

6365
def headerWriter = new HeaderWriter()
66+
def beanValidationFactory = new BeanValidationFactory()
67+
6468
def writer = new ApiWriter (options,
6569
new InterfaceWriter(
6670
headerWriter: headerWriter,
67-
methodWriter: new MethodWriter()),
68-
new DataTypeWriter(headerWriter: headerWriter),
71+
methodWriter: new MethodWriter(
72+
beanValidationFactory: beanValidationFactory,
73+
apiOptions: options
74+
),
75+
beanValidationFactory: beanValidationFactory,
76+
apiOptions: options
77+
),
78+
new DataTypeWriter(
79+
headerWriter: headerWriter,
80+
beanValidationFactory: beanValidationFactory,
81+
apiOptions: options,
82+
),
6983
new StringEnumWriter(headerWriter: headerWriter)
7084
)
7185

@@ -79,6 +93,7 @@ class SpringGeneratr implements OpenApiGeneratr {
7993
options.targetDir = generatrOptions.targetDir
8094
options.packageName = generatrOptions.packageName
8195
options.typeMappings = reader.read (generatrOptions.typeMappings as String)
96+
options.beanValidation = generatrOptions.beanValidation != null && generatrOptions.beanValidation == true
8297
options
8398
}
8499

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ package com.github.hauner.openapi.spring.model.datatypes
2020
* OpenAPI type 'array' maps to java [].
2121
*
2222
* @author Martin Hauner
23+
* @author Bastian Wilhelm
2324
*/
2425
class ArrayDataType implements DataType {
2526

2627
private DataType item
28+
private DataTypeConstraints constraints
2729

2830
@Override
2931
String getName () {
@@ -45,4 +47,8 @@ class ArrayDataType implements DataType {
4547
item.referencedImports
4648
}
4749

50+
@Override
51+
DataTypeConstraints getConstraints () {
52+
constraints
53+
}
4854
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ package com.github.hauner.openapi.spring.model.datatypes
2020
* OpenAPI type 'array' maps to Collection<>.
2121
*
2222
* @author Martin Hauner
23+
* @author Bastian Wilhelm
2324
*/
2425
class CollectionDataType implements DataType {
2526

2627
private DataType item
28+
private DataTypeConstraints constraints
2729

2830
@Override
2931
String getName () {
@@ -45,4 +47,8 @@ class CollectionDataType implements DataType {
4547
[]
4648
}
4749

50+
@Override
51+
DataTypeConstraints getConstraints () {
52+
constraints
53+
}
4854
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,22 @@ package com.github.hauner.openapi.spring.model.datatypes
1818

1919
/**
2020
* OpenAPI constraint details of a data type.
21+
*
22+
* @author Martin Hauner
23+
* @author Bastian Wilhelm
2124
*/
2225
class DataTypeConstraints {
2326

2427
def defaultValue
28+
def nullable
29+
def minLength
30+
def maxLength
31+
def minimum
32+
def exclusiveMinimum
33+
def maximum
34+
def exclusiveMaximum
35+
def minItems
36+
def maxItems
2537

2638
def getDefault () {
2739
defaultValue

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ package com.github.hauner.openapi.spring.model.datatypes
2020
* OpenAPI type 'array' maps to List<>.
2121
*
2222
* @author Martin Hauner
23+
* @author Bastian Wilhelm
2324
*/
2425
class ListDataType implements DataType {
2526

2627
private DataType item
28+
private DataTypeConstraints constraints
29+
2730

2831
@Override
2932
String getName () {
@@ -45,4 +48,8 @@ class ListDataType implements DataType {
4548
[]
4649
}
4750

51+
@Override
52+
DataTypeConstraints getConstraints () {
53+
constraints
54+
}
4855
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ package com.github.hauner.openapi.spring.model.datatypes
2020
* OpenAPI named #/component/schemas type or an inline type.
2121
*
2222
* @author Martin Hauner
23+
* @author Bastian Wilhelm
2324
*/
2425
class ObjectDataType implements DataType {
2526

2627
String type
2728
String pkg = 'unknown'
29+
private DataTypeConstraints constraints
30+
2831

2932
// must preserve the insertion order
3033
Map<String, DataType> properties = new LinkedHashMap<> ()
@@ -65,4 +68,8 @@ class ObjectDataType implements DataType {
6568
properties
6669
}
6770

71+
@Override
72+
DataTypeConstraints getConstraints () {
73+
constraints
74+
}
6875
}

0 commit comments

Comments
 (0)