Skip to content
Merged

#41 #42

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
4 changes: 2 additions & 2 deletions docs/generatr/identifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ first letter on the next word. The special characters are:
For properties of model classes, the properties will be annotated with `@JsonProperty` to provide
the mapping from the OpenAPI identifier to the Java identifier.

```java
class Example {

@JsonProperty("foo-bar")
private String fooBar;

// ...
}


```

[java-char-start]: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/Character.html#isJavaIdentifierStart(char)
[java-char-part]: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/Character.html#isJavaIdentifierPart(char)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.github.hauner.openapi.spring.converter

import com.github.hauner.openapi.spring.converter.mapping.TypeMappingX

/**
* Options of the generatr.
*
Expand Down Expand Up @@ -53,6 +55,6 @@ class ApiOptions {
* {@link com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping}: used to override
* parameter, response type mappings or to add additional parameters on a single endpoint.
*/
List<?> typeMappings
List<TypeMappingX> typeMappings

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package com.github.hauner.openapi.spring.converter
import com.github.hauner.openapi.spring.converter.mapping.AmbiguousTypeMappingException
import com.github.hauner.openapi.spring.converter.mapping.TargetType
import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
import com.github.hauner.openapi.spring.converter.mapping.TypeMappingX
import com.github.hauner.openapi.spring.converter.schema.ArraySchemaType
import com.github.hauner.openapi.spring.converter.schema.ObjectSchemaType
import com.github.hauner.openapi.spring.converter.schema.SchemaInfo
import com.github.hauner.openapi.spring.converter.schema.SchemaType
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
Expand Down Expand Up @@ -49,11 +51,9 @@ import com.github.hauner.openapi.spring.model.datatypes.StringDataType
class DataTypeConverter {

private ApiOptions options
private DataTypeMapper mapper

DataTypeConverter(ApiOptions options) {
this.options = options
this.mapper = new DataTypeMapper(options.typeMappings)
}

DataType none() {
Expand Down Expand Up @@ -96,7 +96,7 @@ class DataTypeConverter {
DataType item = convert (itemSchemaInfo, dataTypes)

def arrayType
TargetType targetType = mapper.getMappedDataType (new ArraySchemaType (schemaInfo))
TargetType targetType = getMappedDataType (new ArraySchemaType (schemaInfo))
switch (targetType?.typeName) {
case Collection.name:
arrayType = new CollectionDataType (item: item)
Expand All @@ -117,7 +117,7 @@ class DataTypeConverter {
private DataType createObjectDataType (SchemaInfo schemaInfo, DataTypes dataTypes) {
def objectType

TargetType targetType = mapper.getMappedDataType (new ObjectSchemaType (schemaInfo))
TargetType targetType = getMappedDataType (new ObjectSchemaType (schemaInfo))
if (targetType) {
objectType = new MappedDataType (
type: targetType.name,
Expand Down Expand Up @@ -204,11 +204,44 @@ class DataTypeConverter {
simpleType
}

TargetType getMappedDataType (SchemaType schemaType) {
// check endpoint mappings
List<TypeMappingX> endpointMatches = schemaType.matchEndpointMapping (options.typeMappings)
if (!endpointMatches.empty) {
TargetType target = endpointMatches.first().targetType
if (target) {
return target
}
}

// check global io (parameter & response) mappings
List<TypeMappingX> ioMatches = schemaType.matchIoMapping (options.typeMappings)
if (!ioMatches.empty) {
TargetType target = ioMatches.first().targetType
if (target) {
return target
}
}

// check global type mapping
List<TypeMappingX> typeMatches = schemaType.matchTypeMapping (options.typeMappings)
if (typeMatches.isEmpty ()) {
return null
}

if (typeMatches.size () != 1) {
throw new AmbiguousTypeMappingException (typeMatches)
}

TypeMapping match = typeMatches.first () as TypeMapping
return match.targetType
}

private TargetType getSimpleDataType (SchemaInfo schemaInfo) {
if (options.typeMappings) {

// check global mapping
List<TypeMapping> mappings = getTypeMappings ()
List<TypeMapping> mappings = getTypeMappingsY ()
List<TypeMapping> matches = mappings.findAll {
it.sourceTypeName == schemaInfo.type && it.sourceTypeFormat == schemaInfo.format
}
Expand All @@ -231,7 +264,7 @@ class DataTypeConverter {
null
}

private List<TypeMapping> getTypeMappings () {
private List<TypeMapping> getTypeMappingsY () {
options.typeMappings.findResults {
it instanceof TypeMapping ? it : null
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.github.hauner.openapi.spring.converter.mapping

import com.github.hauner.openapi.spring.converter.mapping.TypeMapping

/**
* thrown when the DataTypeConverter finds an ambiguous data type mapping.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ enum MappingLevel {
}

/**
*
* Common interface for type mappings.
*/
interface TypeMappingX {
boolean matches (SchemaInfo info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ interface SchemaType {

}

abstract class SchemaTypeBase implements SchemaType {
abstract class BaseSchemaType implements SchemaType {

protected SchemaInfo info

SchemaTypeBase(SchemaInfo info) {
BaseSchemaType (SchemaInfo info) {
this.info = info
}

Expand All @@ -57,11 +57,7 @@ abstract class SchemaTypeBase implements SchemaType {
}

// type mappings
endpoint.findAll {
it.isLevel (MappingLevel.TYPE) && it.matches (info)
}.collect {
it.childMappings
}.flatten () as List<TypeMappingX>
matchTypeMapping (endpoint)
}

List<TypeMappingX> matchIoMapping (List<TypeMappingX> typeMappings) {
Expand All @@ -75,7 +71,7 @@ abstract class SchemaTypeBase implements SchemaType {

}

class ObjectSchemaType extends SchemaTypeBase {
class ObjectSchemaType extends BaseSchemaType {

ObjectSchemaType (SchemaInfo info) {
super (info)
Expand All @@ -90,16 +86,17 @@ class ObjectSchemaType extends SchemaTypeBase {

}

class ArraySchemaType extends SchemaTypeBase {
class ArraySchemaType extends BaseSchemaType {

ArraySchemaType (SchemaInfo info) {
super (info)
}

@Override
List<TypeMappingX> matchTypeMapping (List<TypeMappingX> typeMappings) {
def array = new SchemaInfo (null, null,'array')
typeMappings.findAll () {
it.isLevel (MappingLevel.TYPE) && it.matches (new SchemaInfo (null, null,'array'))
it.isLevel (MappingLevel.TYPE) && it.matches (array)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping
import com.github.hauner.openapi.spring.converter.mapping.ParameterTypeMapping
import com.github.hauner.openapi.spring.converter.mapping.ResponseTypeMapping
import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
import com.github.hauner.openapi.spring.converter.mapping.TypeMappingX
import org.yaml.snakeyaml.Yaml

import java.util.regex.Matcher
Expand All @@ -33,7 +34,7 @@ import java.util.regex.Pattern
class TypeMappingReader {
private Pattern GENERIC_INLINE = ~/(.+?)<(.+?)>/

List<?> read (String typeMappings) {
List<TypeMappingX> read (String typeMappings) {
if (typeMappings == null) {
return []
}
Expand Down
Loading