Skip to content

Commit 7ab9d27

Browse files
authored
Merge pull request #46 from hauner/#6
resolves #6
2 parents 68ca4b3 + 2548596 commit 7ab9d27

24 files changed

Lines changed: 315 additions & 154 deletions

File tree

docs/index.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,20 @@ permalink: /
1212
# openapi-generatr-spring
1313
{: .no_toc }
1414

15-
a simple [OpenAPI][openapi] interface only code generator for [Spring Boot][springboot].
15+
a simple [OpenAPI][openapi] interface only (& model) code generator for [Spring Boot][springboot].
16+
17+
It is useful in an API first approach where you API is explicitly defined by an OpenAPI yaml file
18+
before it gets implemented.
19+
20+
The generatr generates java interfaces based on the endpoint description of the API and simple POJO
21+
classes for parameter or response objects defined in th API. It is **your** task to create the controller
22+
classes that implement the interfaces.
23+
24+
The interfaces will help to keep the implementation in sync with the API. If anything relevant changes
25+
in the API the interface changes and the compiler will warn that the interface is not implemented
26+
correctly.
27+
28+
See the [generatr intro][docs-generatr]{:target="_blank"} for a short example.
1629
{: .mb-6 }
1730

1831
Note that the generatr is still in an early state of development and not all features are completely implemented.
@@ -110,6 +123,7 @@ openapi-generatr-spring is distributed by [Apache License 2.0][license].
110123
[workflow-ci]: https://github.com/hauner/openapi-generatr-spring/actions?query=workflow%3Aci
111124

112125
[docs-gradle]: /openapi-generatr-spring/gradle.html
126+
[docs-generatr]: /openapi-generatr-spring/generatr/
113127

114128
[bintray]: https://bintray.com/hauner/openapi-generatr
115129
[generatr-gradle]: https://github.com/hauner/openapi-generatr-gradle

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

Lines changed: 14 additions & 9 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,27 +172,30 @@ class DataTypeConverter {
171172
typeFormat += '/' + schemaInfo.format
172173
}
173174

175+
def defaultValue = schemaInfo.defaultValue
176+
def constraints = defaultValue != null ? new DataTypeConstraints(defaultValue: defaultValue) : null
177+
174178
def simpleType
175179
switch (typeFormat) {
176180
case 'integer':
177181
case 'integer/int32':
178-
simpleType = new IntegerDataType ()
182+
simpleType = new IntegerDataType (constraints: constraints)
179183
break
180184
case 'integer/int64':
181-
simpleType = new LongDataType ()
185+
simpleType = new LongDataType (constraints: constraints)
182186
break
183187
case 'number':
184188
case 'number/float':
185-
simpleType = new FloatDataType ()
189+
simpleType = new FloatDataType (constraints: constraints)
186190
break
187191
case 'number/double':
188-
simpleType = new DoubleDataType ()
192+
simpleType = new DoubleDataType (constraints: constraints)
189193
break
190194
case 'boolean':
191-
simpleType = new BooleanDataType ()
195+
simpleType = new BooleanDataType (constraints: constraints)
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/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/DataType.groovy

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,42 @@ package com.github.hauner.openapi.spring.model.datatypes
2121
*
2222
* @author Martin Hauner
2323
*/
24-
interface DataType {
24+
trait /*interface*/ DataType {
2525

2626
/**
2727
* The Java type name without package.
2828
*
2929
* @return the type name.
3030
*/
31-
String getName ()
31+
abstract String getName ()
3232

3333
/**
3434
* The package of this type without class.
3535
*/
36-
String getPackageName ()
36+
abstract String getPackageName ()
3737

3838
/**
3939
* Provides the import(s) of this type, usually a single import. If it is a generic type it will
4040
* add another import for each generic parameter.
4141
*
4242
* @return import of this type.
4343
*/
44-
Set<String> getImports ()
44+
abstract Set<String> getImports ()
4545

4646
/**
4747
* Provides the list of imports for the types referenced by this this type.
4848
*
4949
* @return the referenced import list.
5050
*/
51-
Set<String> getReferencedImports ()
51+
abstract Set<String> getReferencedImports ()
52+
53+
/**
54+
* Provides the constraint information of the data type.
55+
*
56+
* @return the constraints or null if there are no constraints.
57+
*/
58+
DataTypeConstraints getConstraints() {
59+
null
60+
}
61+
5262
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
class DataTypeConstraints {
23+
24+
def defaultValue
25+
26+
def getDefault () {
27+
defaultValue
28+
}
29+
30+
}

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
}

0 commit comments

Comments
 (0)