Skip to content

#6 #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Dec 8, 2019
Merged

#6 #46

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ permalink: /
# openapi-generatr-spring
{: .no_toc }

a simple [OpenAPI][openapi] interface only code generator for [Spring Boot][springboot].
a simple [OpenAPI][openapi] interface only (& model) code generator for [Spring Boot][springboot].

It is useful in an API first approach where you API is explicitly defined by an OpenAPI yaml file
before it gets implemented.

The generatr generates java interfaces based on the endpoint description of the API and simple POJO
classes for parameter or response objects defined in th API. It is **your** task to create the controller
classes that implement the interfaces.

The interfaces will help to keep the implementation in sync with the API. If anything relevant changes
in the API the interface changes and the compiler will warn that the interface is not implemented
correctly.

See the [generatr intro][docs-generatr]{:target="_blank"} for a short example.
{: .mb-6 }

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

[docs-gradle]: /openapi-generatr-spring/gradle.html
[docs-generatr]: /openapi-generatr-spring/generatr/

[bintray]: https://bintray.com/hauner/openapi-generatr
[generatr-gradle]: https://github.com/hauner/openapi-generatr-gradle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.github.hauner.openapi.spring.model.DataTypes
import com.github.hauner.openapi.spring.model.datatypes.ArrayDataType
import com.github.hauner.openapi.spring.model.datatypes.BooleanDataType
import com.github.hauner.openapi.spring.model.datatypes.CollectionDataType
import com.github.hauner.openapi.spring.model.datatypes.DataTypeConstraints
import com.github.hauner.openapi.spring.model.datatypes.ListDataType
import com.github.hauner.openapi.spring.model.datatypes.LocalDateDataType
import com.github.hauner.openapi.spring.model.datatypes.MapDataType
Expand Down Expand Up @@ -171,27 +172,30 @@ class DataTypeConverter {
typeFormat += '/' + schemaInfo.format
}

def defaultValue = schemaInfo.defaultValue
def constraints = defaultValue != null ? new DataTypeConstraints(defaultValue: defaultValue) : null

def simpleType
switch (typeFormat) {
case 'integer':
case 'integer/int32':
simpleType = new IntegerDataType ()
simpleType = new IntegerDataType (constraints: constraints)
break
case 'integer/int64':
simpleType = new LongDataType ()
simpleType = new LongDataType (constraints: constraints)
break
case 'number':
case 'number/float':
simpleType = new FloatDataType ()
simpleType = new FloatDataType (constraints: constraints)
break
case 'number/double':
simpleType = new DoubleDataType ()
simpleType = new DoubleDataType (constraints: constraints)
break
case 'boolean':
simpleType = new BooleanDataType ()
simpleType = new BooleanDataType (constraints: constraints)
break
case 'string':
simpleType = createStringDataType (schemaInfo, dataTypes)
simpleType = createStringDataType (schemaInfo, constraints, dataTypes)
break
case 'string/date':
simpleType = new LocalDateDataType ()
Expand All @@ -206,17 +210,18 @@ class DataTypeConverter {
simpleType
}

private DataType createStringDataType (SchemaInfo info, DataTypes dataTypes) {
private DataType createStringDataType (SchemaInfo info, DataTypeConstraints constraints, DataTypes dataTypes) {
if (!info.isEnum()) {
return new StringDataType ()
return new StringDataType (constraints: constraints)
}

// in case of an inline definition the name may be lowercase, make sure the enum
// class gets an uppercase name!
def enumType = new StringEnumDataType (
type: info.name.capitalize (),
pkg: [options.packageName, 'model'].join ('.'),
values: info.enumValues)
values: info.enumValues,
constraints: constraints)

dataTypes.add (enumType)
enumType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ class SchemaInfo {
schema.$ref
}

/**
* get default value.
*
* @return default value or null
*/
def getDefaultValue() {
schema.default
}

/**
* 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 @@ -23,6 +23,8 @@ package com.github.hauner.openapi.spring.model.datatypes
*/
class BooleanDataType implements DataType {

DataTypeConstraints constraints

@Override
String getName () {
'Boolean'
Expand All @@ -43,4 +45,9 @@ class BooleanDataType implements DataType {
[]
}

@Override
DataTypeConstraints getConstraints() {
constraints
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,42 @@ package com.github.hauner.openapi.spring.model.datatypes
*
* @author Martin Hauner
*/
interface DataType {
trait /*interface*/ DataType {

/**
* The Java type name without package.
*
* @return the type name.
*/
String getName ()
abstract String getName ()

/**
* The package of this type without class.
*/
String getPackageName ()
abstract String getPackageName ()

/**
* Provides the import(s) of this type, usually a single import. If it is a generic type it will
* add another import for each generic parameter.
*
* @return import of this type.
*/
Set<String> getImports ()
abstract Set<String> getImports ()

/**
* Provides the list of imports for the types referenced by this this type.
*
* @return the referenced import list.
*/
Set<String> getReferencedImports ()
abstract Set<String> getReferencedImports ()

/**
* Provides the constraint information of the data type.
*
* @return the constraints or null if there are no constraints.
*/
DataTypeConstraints getConstraints() {
null
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2019 the original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.hauner.openapi.spring.model.datatypes

/**
* OpenAPI constraint details of a data type.
*/
class DataTypeConstraints {

def defaultValue

def getDefault () {
defaultValue
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ package com.github.hauner.openapi.spring.model.datatypes
*/
class DoubleDataType implements DataType {

DataTypeConstraints constraints

@Override
String getName () {
'Double'
Expand All @@ -43,4 +45,9 @@ class DoubleDataType implements DataType {
[]
}

@Override
DataTypeConstraints getConstraints() {
constraints
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ package com.github.hauner.openapi.spring.model.datatypes
*/
class FloatDataType implements DataType {

DataTypeConstraints constraints

@Override
String getName () {
'Float'
Expand All @@ -43,4 +45,9 @@ class FloatDataType implements DataType {
[]
}

@Override
DataTypeConstraints getConstraints() {
constraints
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ package com.github.hauner.openapi.spring.model.datatypes
*/
class IntegerDataType implements DataType {

DataTypeConstraints constraints

@Override
String getName () {
return 'Integer'
Expand All @@ -43,4 +45,9 @@ class IntegerDataType implements DataType {
[]
}

@Override
DataTypeConstraints getConstraints() {
constraints
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ package com.github.hauner.openapi.spring.model.datatypes
*/
class LongDataType implements DataType {

DataTypeConstraints constraints

@Override
String getName () {
'Long'
Expand All @@ -43,4 +45,9 @@ class LongDataType implements DataType {
[]
}

@Override
DataTypeConstraints getConstraints() {
constraints
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ package com.github.hauner.openapi.spring.model.datatypes
*/
class StringDataType implements DataType {

DataTypeConstraints constraints

@Override
String getName () {
'String'
Expand All @@ -43,4 +45,9 @@ class StringDataType implements DataType {
[]
}

@Override
DataTypeConstraints getConstraints() {
constraints
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class StringEnumDataType implements DataType {
String type
String pkg = 'unknown'
List<String> values = []
DataTypeConstraints constraints

@Override
String getName () {
Expand All @@ -47,6 +48,11 @@ class StringEnumDataType implements DataType {
[]
}

@Override
DataTypeConstraints getConstraints() {
constraints
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,21 @@

package com.github.hauner.openapi.spring.model.parameters

import com.github.hauner.openapi.spring.model.datatypes.DataType

/**
* OpenAPI cookie parameter.
*
* @author Martin Hauner
*/
class CookieParameter implements Parameter {
String name
boolean required
DataType dataType
class CookieParameter extends Parameter {

String getAnnotationName () {
"CookieValue"
}

String getAnnotationWithPackage () {
"org.springframework.web.bind.annotation.${annotationName}"
}

String getAnnotation () {
"@${annotationName}"
}

Set<String> getDataTypeImports () {
dataType.imports
}

/**
* Is the parameter required?
*
* @return true if the parameter is required, otherwise false.
*/
boolean isRequired () {
required
}

/**
* Create annotation?
*/
boolean withAnnotation () {
true
}

/**
* Create annotation with parameters?
*
* @return true if the annotation should have parameters, false otherwise
*/
boolean withParameters () {
true
}
Expand Down
Loading