Skip to content
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
out
build
.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.github.hauner.openapi.spring.converter.mapping.Mapping
* Options of the generatr.
*
* @author Martin Hauner
* @author Bastian Wilhelm
*/
class ApiOptions {

Expand Down Expand Up @@ -58,4 +59,9 @@ class ApiOptions {
*/
List<Mapping> typeMappings

/**
* provide enabling Bean Validation (JSR303) annotations. Default is false (disabled)
*/
boolean beanValidation = false;

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import com.github.hauner.openapi.spring.model.datatypes.StringEnumDataType
* Converter to map OpenAPI schemas to Java data types.
*
* @author Martin Hauner
* @author Bastian Wilhelm
*/
class DataTypeConverter {

Expand Down Expand Up @@ -94,18 +95,26 @@ class DataTypeConverter {

def arrayType
TargetType targetType = getMappedDataType (new ArraySchemaType (schemaInfo))

def constraints = new DataTypeConstraints(
defaultValue: schemaInfo.defaultValue,
nullable: schemaInfo.nullable,
minItems: schemaInfo.minItems,
maxItems: schemaInfo.maxItems,
)

switch (targetType?.typeName) {
case Collection.name:
arrayType = new CollectionDataType (item: item)
arrayType = new CollectionDataType (item: item, constraints: constraints)
break
case List.name:
arrayType = new ListDataType (item: item)
arrayType = new ListDataType (item: item, constraints: constraints)
break
case Set.name:
arrayType = new SetDataType (item: item)
arrayType = new SetDataType (item: item, constraints: constraints)
break
default:
arrayType = new ArrayDataType (item: item)
arrayType = new ArrayDataType (item: item, constraints: constraints)
}

arrayType
Expand Down Expand Up @@ -142,9 +151,14 @@ class DataTypeConverter {
}
}

def constraints = new DataTypeConstraints(
nullable: schemaInfo.nullable,
)

objectType = new ObjectDataType (
type: schemaInfo.name,
pkg: [options.packageName, 'model'].join ('.')
pkg: [options.packageName, 'model'].join ('.'),
constraints: constraints
)

schemaInfo.eachProperty { String propName, SchemaInfo propDataTypeInfo ->
Expand Down Expand Up @@ -173,8 +187,16 @@ class DataTypeConverter {
typeFormat += '/' + schemaInfo.format
}

def defaultValue = schemaInfo.defaultValue
def constraints = defaultValue != null ? new DataTypeConstraints(defaultValue: defaultValue) : null
def constraints = new DataTypeConstraints(
defaultValue: schemaInfo.defaultValue,
nullable: schemaInfo.nullable,
minLength: schemaInfo.minLength,
maxLength: schemaInfo.maxLength,
maximum: schemaInfo.maximum,
exclusiveMaximum: schemaInfo.exclusiveMaximum,
minimum: schemaInfo.minimum,
exclusiveMinimum: schemaInfo.exclusiveMinimum,
)

def simpleType
switch (typeFormat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import io.swagger.v3.oas.models.media.Schema
* schema with context information, i.e. name and if this is an inline type with a generated name.
*
* @author Martin Hauner
* @author Bastian Wilhelm
*/
class SchemaInfo implements MappingSchema {

Expand Down Expand Up @@ -138,6 +139,87 @@ class SchemaInfo implements MappingSchema {
schema.default
}

/**
* get nullable value.
*
* @return nullable value or null
*/
def getNullable() {
schema.nullable
}

/**
* get minLength value.
*
* @return minLength value or null
*/
def getMinLength() {
schema.minLength
}

/**
* get maxLength value.
*
* @return maxLength value or null
*/
def getMaxLength() {
schema.maxLength
}

/**
* get minItems value.
*
* @return minItems value or null
*/
def getMinItems() {
schema.minItems
}

/**
* get maxItems value.
*
* @return maxItems value or null
*/
def getMaxItems() {
schema.maxItems
}

/**
* get maximum value.
*
* @return maximum value or null
*/
def getMaximum() {
schema.maximum
}

/**
* get exclusiveMaximum value.
*
* @return exclusiveMaximum value or null
*/
def getExclusiveMaximum() {
schema.exclusiveMaximum
}

/**
* get minimum value.
*
* @return minimum value or null
*/
def getMinimum() {
schema.minimum
}

/**
* get exclusiveMinimum value.
*
* @return exclusiveMinimum value or null
*/
def getExclusiveMinimum() {
schema.exclusiveMinimum
}

/**
* get the custom Java type (fully qualified) defined via the {@code x-java-type} OpenAPI
* extension. If no {@code x-java-type} is set the result is {@code null}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.github.hauner.openapi.api.OpenApiGeneratr
import com.github.hauner.openapi.spring.converter.ApiConverter
import com.github.hauner.openapi.spring.converter.ApiOptions
import com.github.hauner.openapi.spring.writer.ApiWriter
import com.github.hauner.openapi.spring.writer.BeanValidationFactory
import com.github.hauner.openapi.spring.writer.DataTypeWriter
import com.github.hauner.openapi.spring.writer.HeaderWriter
import com.github.hauner.openapi.spring.writer.InterfaceWriter
Expand All @@ -33,6 +34,7 @@ import io.swagger.v3.parser.core.models.SwaggerParseResult
* Entry point of openapi-generatr-spring.
*
* @author Martin Hauner
* @author Bastian Wilhelm
*/
class SpringGeneratr implements OpenApiGeneratr {

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

def headerWriter = new HeaderWriter()
def beanValidationFactory = new BeanValidationFactory()

def writer = new ApiWriter (options,
new InterfaceWriter(
headerWriter: headerWriter,
methodWriter: new MethodWriter()),
new DataTypeWriter(headerWriter: headerWriter),
methodWriter: new MethodWriter(
beanValidationFactory: beanValidationFactory,
apiOptions: options
),
beanValidationFactory: beanValidationFactory,
apiOptions: options
),
new DataTypeWriter(
headerWriter: headerWriter,
beanValidationFactory: beanValidationFactory,
apiOptions: options,
),
new StringEnumWriter(headerWriter: headerWriter)
)

Expand All @@ -79,6 +93,7 @@ class SpringGeneratr implements OpenApiGeneratr {
options.targetDir = generatrOptions.targetDir
options.packageName = generatrOptions.packageName
options.typeMappings = reader.read (generatrOptions.typeMappings as String)
options.beanValidation = generatrOptions.beanValidation != null && generatrOptions.beanValidation == true
options
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ package com.github.hauner.openapi.spring.model.datatypes
* OpenAPI type 'array' maps to java [].
*
* @author Martin Hauner
* @author Bastian Wilhelm
*/
class ArrayDataType implements DataType {

private DataType item
private DataTypeConstraints constraints

@Override
String getName () {
Expand All @@ -45,4 +47,8 @@ class ArrayDataType implements DataType {
item.referencedImports
}

@Override
DataTypeConstraints getConstraints () {
constraints
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ package com.github.hauner.openapi.spring.model.datatypes
* OpenAPI type 'array' maps to Collection<>.
*
* @author Martin Hauner
* @author Bastian Wilhelm
*/
class CollectionDataType implements DataType {

private DataType item
private DataTypeConstraints constraints

@Override
String getName () {
Expand All @@ -45,4 +47,8 @@ class CollectionDataType implements DataType {
[]
}

@Override
DataTypeConstraints getConstraints () {
constraints
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,22 @@ package com.github.hauner.openapi.spring.model.datatypes

/**
* OpenAPI constraint details of a data type.
*
* @author Martin Hauner
* @author Bastian Wilhelm
*/
class DataTypeConstraints {

def defaultValue
def nullable
def minLength
def maxLength
def minimum
def exclusiveMinimum
def maximum
def exclusiveMaximum
def minItems
def maxItems

def getDefault () {
defaultValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ package com.github.hauner.openapi.spring.model.datatypes
* OpenAPI type 'array' maps to List<>.
*
* @author Martin Hauner
* @author Bastian Wilhelm
*/
class ListDataType implements DataType {

private DataType item
private DataTypeConstraints constraints


@Override
String getName () {
Expand All @@ -45,4 +48,8 @@ class ListDataType implements DataType {
[]
}

@Override
DataTypeConstraints getConstraints () {
constraints
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ package com.github.hauner.openapi.spring.model.datatypes
* OpenAPI named #/component/schemas type or an inline type.
*
* @author Martin Hauner
* @author Bastian Wilhelm
*/
class ObjectDataType implements DataType {

String type
String pkg = 'unknown'
private DataTypeConstraints constraints


// must preserve the insertion order
Map<String, DataType> properties = new LinkedHashMap<> ()
Expand Down Expand Up @@ -65,4 +68,8 @@ class ObjectDataType implements DataType {
properties
}

@Override
DataTypeConstraints getConstraints () {
constraints
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ package com.github.hauner.openapi.spring.model.datatypes
* OpenAPI type 'array' maps to Set<>.
*
* @author Martin Hauner
* @author Bastian Wilhelm
*/
class SetDataType implements DataType {

private DataType item
private DataTypeConstraints constraints


@Override
String getName () {
Expand All @@ -45,4 +48,8 @@ class SetDataType implements DataType {
[]
}

@Override
DataTypeConstraints getConstraints () {
constraints
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import com.github.hauner.openapi.spring.model.datatypes.DataTypeConstraints
* Constraints of the parameter data type.
*
* @author Martin Hauner
* @author Bastian Wilhelm
*/
class ParameterConstraints {

DataTypeConstraints constraints

boolean hasDefault() {
constraints != null
constraints?.defaultValue != null
}

def getDefault() {
Expand Down
Loading