Skip to content

Commit 826b3e9

Browse files
authored
Merge pull request #42 from hauner/#41
resolves #41
2 parents 1a4e46f + 1709d4e commit 826b3e9

10 files changed

Lines changed: 193 additions & 128 deletions

File tree

docs/generatr/identifier.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ first letter on the next word. The special characters are:
2929
For properties of model classes, the properties will be annotated with `@JsonProperty` to provide
3030
the mapping from the OpenAPI identifier to the Java identifier.
3131

32+
```java
3233
class Example {
3334

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

3738
// ...
3839
}
39-
40-
40+
```
4141

4242
[java-char-start]: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/Character.html#isJavaIdentifierStart(char)
4343
[java-char-part]: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/Character.html#isJavaIdentifierPart(char)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

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

19+
import com.github.hauner.openapi.spring.converter.mapping.TypeMappingX
20+
1921
/**
2022
* Options of the generatr.
2123
*
@@ -53,6 +55,6 @@ class ApiOptions {
5355
* {@link com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping}: used to override
5456
* parameter, response type mappings or to add additional parameters on a single endpoint.
5557
*/
56-
List<?> typeMappings
58+
List<TypeMappingX> typeMappings
5759

5860
}

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ package com.github.hauner.openapi.spring.converter
1919
import com.github.hauner.openapi.spring.converter.mapping.AmbiguousTypeMappingException
2020
import com.github.hauner.openapi.spring.converter.mapping.TargetType
2121
import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
22+
import com.github.hauner.openapi.spring.converter.mapping.TypeMappingX
2223
import com.github.hauner.openapi.spring.converter.schema.ArraySchemaType
2324
import com.github.hauner.openapi.spring.converter.schema.ObjectSchemaType
2425
import com.github.hauner.openapi.spring.converter.schema.SchemaInfo
26+
import com.github.hauner.openapi.spring.converter.schema.SchemaType
2527
import com.github.hauner.openapi.spring.model.DataTypes
2628
import com.github.hauner.openapi.spring.model.datatypes.ArrayDataType
2729
import com.github.hauner.openapi.spring.model.datatypes.BooleanDataType
@@ -49,11 +51,9 @@ import com.github.hauner.openapi.spring.model.datatypes.StringDataType
4951
class DataTypeConverter {
5052

5153
private ApiOptions options
52-
private DataTypeMapper mapper
5354

5455
DataTypeConverter(ApiOptions options) {
5556
this.options = options
56-
this.mapper = new DataTypeMapper(options.typeMappings)
5757
}
5858

5959
DataType none() {
@@ -96,7 +96,7 @@ class DataTypeConverter {
9696
DataType item = convert (itemSchemaInfo, dataTypes)
9797

9898
def arrayType
99-
TargetType targetType = mapper.getMappedDataType (new ArraySchemaType (schemaInfo))
99+
TargetType targetType = getMappedDataType (new ArraySchemaType (schemaInfo))
100100
switch (targetType?.typeName) {
101101
case Collection.name:
102102
arrayType = new CollectionDataType (item: item)
@@ -117,7 +117,7 @@ class DataTypeConverter {
117117
private DataType createObjectDataType (SchemaInfo schemaInfo, DataTypes dataTypes) {
118118
def objectType
119119

120-
TargetType targetType = mapper.getMappedDataType (new ObjectSchemaType (schemaInfo))
120+
TargetType targetType = getMappedDataType (new ObjectSchemaType (schemaInfo))
121121
if (targetType) {
122122
objectType = new MappedDataType (
123123
type: targetType.name,
@@ -204,11 +204,44 @@ class DataTypeConverter {
204204
simpleType
205205
}
206206

207+
TargetType getMappedDataType (SchemaType schemaType) {
208+
// check endpoint mappings
209+
List<TypeMappingX> endpointMatches = schemaType.matchEndpointMapping (options.typeMappings)
210+
if (!endpointMatches.empty) {
211+
TargetType target = endpointMatches.first().targetType
212+
if (target) {
213+
return target
214+
}
215+
}
216+
217+
// check global io (parameter & response) mappings
218+
List<TypeMappingX> ioMatches = schemaType.matchIoMapping (options.typeMappings)
219+
if (!ioMatches.empty) {
220+
TargetType target = ioMatches.first().targetType
221+
if (target) {
222+
return target
223+
}
224+
}
225+
226+
// check global type mapping
227+
List<TypeMappingX> typeMatches = schemaType.matchTypeMapping (options.typeMappings)
228+
if (typeMatches.isEmpty ()) {
229+
return null
230+
}
231+
232+
if (typeMatches.size () != 1) {
233+
throw new AmbiguousTypeMappingException (typeMatches)
234+
}
235+
236+
TypeMapping match = typeMatches.first () as TypeMapping
237+
return match.targetType
238+
}
239+
207240
private TargetType getSimpleDataType (SchemaInfo schemaInfo) {
208241
if (options.typeMappings) {
209242

210243
// check global mapping
211-
List<TypeMapping> mappings = getTypeMappings ()
244+
List<TypeMapping> mappings = getTypeMappingsY ()
212245
List<TypeMapping> matches = mappings.findAll {
213246
it.sourceTypeName == schemaInfo.type && it.sourceTypeFormat == schemaInfo.format
214247
}
@@ -231,7 +264,7 @@ class DataTypeConverter {
231264
null
232265
}
233266

234-
private List<TypeMapping> getTypeMappings () {
267+
private List<TypeMapping> getTypeMappingsY () {
235268
options.typeMappings.findResults {
236269
it instanceof TypeMapping ? it : null
237270
}

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

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/main/groovy/com/github/hauner/openapi/spring/converter/mapping/AmbiguousTypeMappingException.groovy

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

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

19-
import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
20-
2119
/**
2220
* thrown when the DataTypeConverter finds an ambiguous data type mapping.
2321
*

src/main/groovy/com/github/hauner/openapi/spring/converter/mapping/TypeMappingX.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ enum MappingLevel {
2626
}
2727

2828
/**
29-
*
29+
* Common interface for type mappings.
3030
*/
3131
interface TypeMappingX {
3232
boolean matches (SchemaInfo info)

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ interface SchemaType {
2828

2929
}
3030

31-
abstract class SchemaTypeBase implements SchemaType {
31+
abstract class BaseSchemaType implements SchemaType {
3232

3333
protected SchemaInfo info
3434

35-
SchemaTypeBase(SchemaInfo info) {
35+
BaseSchemaType (SchemaInfo info) {
3636
this.info = info
3737
}
3838

@@ -57,11 +57,7 @@ abstract class SchemaTypeBase implements SchemaType {
5757
}
5858

5959
// type mappings
60-
endpoint.findAll {
61-
it.isLevel (MappingLevel.TYPE) && it.matches (info)
62-
}.collect {
63-
it.childMappings
64-
}.flatten () as List<TypeMappingX>
60+
matchTypeMapping (endpoint)
6561
}
6662

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

7672
}
7773

78-
class ObjectSchemaType extends SchemaTypeBase {
74+
class ObjectSchemaType extends BaseSchemaType {
7975

8076
ObjectSchemaType (SchemaInfo info) {
8177
super (info)
@@ -90,16 +86,17 @@ class ObjectSchemaType extends SchemaTypeBase {
9086

9187
}
9288

93-
class ArraySchemaType extends SchemaTypeBase {
89+
class ArraySchemaType extends BaseSchemaType {
9490

9591
ArraySchemaType (SchemaInfo info) {
9692
super (info)
9793
}
9894

9995
@Override
10096
List<TypeMappingX> matchTypeMapping (List<TypeMappingX> typeMappings) {
97+
def array = new SchemaInfo (null, null,'array')
10198
typeMappings.findAll () {
102-
it.isLevel (MappingLevel.TYPE) && it.matches (new SchemaInfo (null, null,'array'))
99+
it.isLevel (MappingLevel.TYPE) && it.matches (array)
103100
}
104101
}
105102

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping
2020
import com.github.hauner.openapi.spring.converter.mapping.ParameterTypeMapping
2121
import com.github.hauner.openapi.spring.converter.mapping.ResponseTypeMapping
2222
import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
23+
import com.github.hauner.openapi.spring.converter.mapping.TypeMappingX
2324
import org.yaml.snakeyaml.Yaml
2425

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

36-
List<?> read (String typeMappings) {
37+
List<TypeMappingX> read (String typeMappings) {
3738
if (typeMappings == null) {
3839
return []
3940
}

0 commit comments

Comments
 (0)