Skip to content

Commit fa0b14a

Browse files
authored
Merge pull request #69 from hauner/#66
resolves #66
2 parents 56b9696 + 1e7b31f commit fa0b14a

File tree

13 files changed

+268
-175
lines changed

13 files changed

+268
-175
lines changed

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

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import com.github.hauner.openapi.spring.model.datatypes.CollectionDataType
3232
import com.github.hauner.openapi.spring.model.datatypes.DataTypeConstraints
3333
import com.github.hauner.openapi.spring.model.datatypes.ListDataType
3434
import com.github.hauner.openapi.spring.model.datatypes.LocalDateDataType
35-
import com.github.hauner.openapi.spring.model.datatypes.MapDataType
3635
import com.github.hauner.openapi.spring.model.datatypes.MappedDataType
36+
import com.github.hauner.openapi.spring.model.datatypes.MappedMapDataType
3737
import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
3838
import com.github.hauner.openapi.spring.model.datatypes.DataType
3939
import com.github.hauner.openapi.spring.model.datatypes.DoubleDataType
@@ -74,17 +74,11 @@ class DataTypeConverter {
7474
*/
7575
DataType convert (SchemaInfo dataTypeInfo, DataTypes dataTypes) {
7676

77-
if (dataTypeInfo.isArray ()) {
78-
createArrayDataType (dataTypeInfo, dataTypes)
79-
80-
} else if (dataTypeInfo.isRefObject ()) {
81-
def datatype = dataTypes.findRef (dataTypeInfo.ref)
82-
if (datatype) {
83-
return datatype
84-
}
77+
if (dataTypeInfo.isRefObject ()) {
78+
createRefDataType(dataTypeInfo, dataTypes)
8579

86-
def refTypeInfo = dataTypeInfo.buildForRef ()
87-
convert (refTypeInfo, dataTypes)
80+
} else if (dataTypeInfo.isArray ()) {
81+
createArrayDataType (dataTypeInfo, dataTypes)
8882

8983
} else if (dataTypeInfo.isObject ()) {
9084
createObjectDataType (dataTypeInfo, dataTypes)
@@ -117,41 +111,48 @@ class DataTypeConverter {
117111
arrayType
118112
}
119113

114+
private DataType createRefDataType (SchemaInfo schemaInfo, DataTypes dataTypes) {
115+
convert (schemaInfo.buildForRef (), dataTypes)
116+
}
117+
120118
private DataType createObjectDataType (SchemaInfo schemaInfo, DataTypes dataTypes) {
121119
def objectType
122120

123121
TargetType targetType = getMappedDataType (new ObjectSchemaType (schemaInfo))
124122
if (targetType) {
125-
objectType = new MappedDataType (
126-
type: targetType.name,
127-
pkg: targetType.pkg,
128-
genericTypes: targetType.genericNames
129-
)
130-
131-
dataTypes.add (schemaInfo.name, objectType)
132-
return objectType
123+
switch (targetType?.typeName) {
124+
case Map.name:
125+
case 'org.springframework.util.MultiValueMap':
126+
objectType = new MappedMapDataType (
127+
type: targetType.name,
128+
pkg: targetType.pkg,
129+
genericTypes: targetType.genericNames
130+
)
131+
return objectType
132+
default:
133+
objectType = new MappedDataType (
134+
type: targetType.name,
135+
pkg: targetType.pkg,
136+
genericTypes: targetType.genericNames
137+
)
138+
139+
// probably not required anymore
140+
dataTypes.add (schemaInfo.name, objectType)
141+
return objectType
142+
}
133143
}
134144

135-
switch (schemaInfo.getXJavaType ()) {
136-
case Map.name:
137-
objectType = new MapDataType ()
138-
dataTypes.add (schemaInfo.name, objectType)
139-
break
140-
141-
default:
142-
objectType = new ObjectDataType (
143-
type: schemaInfo.name,
144-
pkg: [options.packageName, 'model'].join ('.')
145-
)
146-
147-
schemaInfo.eachProperty { String propName, SchemaInfo propDataTypeInfo ->
148-
def propType = convert (propDataTypeInfo, dataTypes)
149-
objectType.addObjectProperty (propName, propType)
150-
}
145+
objectType = new ObjectDataType (
146+
type: schemaInfo.name,
147+
pkg: [options.packageName, 'model'].join ('.')
148+
)
151149

152-
dataTypes.add (objectType)
150+
schemaInfo.eachProperty { String propName, SchemaInfo propDataTypeInfo ->
151+
def propType = convert (propDataTypeInfo, dataTypes)
152+
objectType.addObjectProperty (propName, propType)
153153
}
154154

155+
dataTypes.add (objectType)
155156
objectType
156157
}
157158

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class SpringGeneratr implements OpenApiGeneratr {
5050
)
5151

5252
SwaggerParseResult result = new OpenAPIV3Parser ()
53-
.readLocation (generatrOptions.apiPath as String, null, opts)
53+
.readLocation (generatrOptions.apiPath as String, null, opts)
5454

5555
if (generatrOptions.showWarnings) {
5656
printWarnings(result.messages)

src/main/groovy/com/github/hauner/openapi/spring/model/DataTypes.groovy

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original authors
2+
* Copyright 2019-2020 the original authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package com.github.hauner.openapi.spring.model
1818

1919
import com.github.hauner.openapi.spring.model.datatypes.DataType
20+
import com.github.hauner.openapi.spring.model.datatypes.MappedDataType
2021
import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
2122
import com.github.hauner.openapi.spring.model.datatypes.StringEnumDataType
2223

@@ -28,6 +29,7 @@ import com.github.hauner.openapi.spring.model.datatypes.StringEnumDataType
2829
class DataTypes {
2930

3031
private Map<String, DataType> types = [:]
32+
private Map<String, MappedDataType> mappedTypes = [:]
3133

3234
/**
3335
* provides all named data types (including simple data types) used by the api endpoint.
@@ -74,7 +76,7 @@ class DataTypes {
7476
* @param dataType the source data type
7577
*/
7678
void add (DataType dataType) {
77-
types.put (dataType.name, dataType)
79+
add (dataType.name, dataType)
7880
}
7981

8082
/**
@@ -84,7 +86,11 @@ class DataTypes {
8486
* @param dataType the source data type
8587
*/
8688
void add (String name, DataType dataType) {
87-
types.put (name, dataType)
89+
if (dataType instanceof MappedDataType) {
90+
mappedTypes.put (name, dataType)
91+
} else {
92+
types.put (name, dataType)
93+
}
8894
}
8995

9096
/**
@@ -94,7 +100,12 @@ class DataTypes {
94100
* @return the data type or null if not found
95101
*/
96102
DataType find (String name) {
97-
types.get (name)
103+
def type = types.get (name)
104+
if (type) {
105+
return type
106+
} else {
107+
return mappedTypes.get (name)
108+
}
98109
}
99110

100111
/**

src/main/groovy/com/github/hauner/openapi/spring/model/datatypes/DataType.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ trait /*interface*/ DataType {
5555
*
5656
* @return the constraints or null if there are no constraints.
5757
*/
58-
DataTypeConstraints getConstraints() {
58+
DataTypeConstraints getConstraints () {
5959
null
6060
}
6161

src/main/groovy/com/github/hauner/openapi/spring/model/datatypes/MappedDataType.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ package com.github.hauner.openapi.spring.model.datatypes
2323
*/
2424
class MappedDataType implements DataType {
2525

26-
private String type
26+
protected String type
2727
String pkg = 'unknown'
2828
List<String> genericTypes = []
2929

@@ -57,7 +57,7 @@ class MappedDataType implements DataType {
5757
}
5858
}
5959

60-
private String getClassName (String ref) {
60+
protected String getClassName (String ref) {
6161
ref.substring (ref.lastIndexOf ('.') + 1)
6262
}
6363

src/main/groovy/com/github/hauner/openapi/spring/model/datatypes/MapDataType.groovy renamed to src/main/groovy/com/github/hauner/openapi/spring/model/datatypes/MappedMapDataType.groovy

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original authors
2+
* Copyright 2020 the original authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,31 +17,9 @@
1717
package com.github.hauner.openapi.spring.model.datatypes
1818

1919
/**
20-
* OpenAPI type 'object' with 'x-type-java': java.util.Map extension.
20+
* OpenAPI schema mapped to a map java type.
2121
*
2222
* @author Martin Hauner
2323
*/
24-
@Deprecated
25-
class MapDataType implements DataType {
26-
27-
@Override
28-
String getName () {
29-
'Map'
30-
}
31-
32-
@Override
33-
String getPackageName () {
34-
'java.util'
35-
}
36-
37-
@Override
38-
Set<String> getImports () {
39-
[[packageName, 'Map'].join('.')]
40-
}
41-
42-
@Override
43-
Set<String> getReferencedImports () {
44-
['java.util.Map', 'java.lang.String']
45-
}
46-
24+
class MappedMapDataType extends MappedDataType {
4725
}
Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original authors
2+
* Copyright 2019-2020 the original authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,8 @@
1616

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

19-
import com.github.hauner.openapi.spring.model.datatypes.MapDataType
2019
import com.github.hauner.openapi.spring.model.datatypes.MappedDataType
20+
import com.github.hauner.openapi.spring.model.datatypes.MappedMapDataType
2121
import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
2222

2323
/**
@@ -31,32 +31,44 @@ class QueryParameter extends Parameter {
3131
"RequestParam"
3232
}
3333

34-
boolean isRequired () {
35-
if (dataType instanceof MapDataType) {
36-
return true
37-
}
38-
39-
this.@required
40-
}
41-
4234
/**
4335
* If the query parameter is mapped to a pojo object it should not have a {@code @RequestParam}
4436
* annotation.
4537
*/
4638
boolean withAnnotation () {
47-
! (
48-
dataType instanceof ObjectDataType
49-
|| dataType instanceof MappedDataType
50-
)
39+
// Map should be annotated
40+
if (dataType instanceof MappedMapDataType) {
41+
return true
42+
}
43+
44+
// Pojo's should NOT be annotated
45+
if (dataType instanceof ObjectDataType) {
46+
return false
47+
}
48+
49+
// usually a pojo....
50+
if (dataType instanceof MappedDataType) {
51+
return false
52+
}
53+
54+
true
5155
}
5256

5357
/**
54-
* todo validate.
55-
*
5658
* If the query parameter is mapped to a pojo object it should not have any parameters.
5759
*/
5860
boolean withParameters () {
59-
! (dataType instanceof ObjectDataType)
61+
// Map should not have parameters
62+
if (dataType instanceof MappedMapDataType) {
63+
return false
64+
}
65+
66+
// Pojo should not have parameters
67+
if (dataType instanceof ObjectDataType) {
68+
return false
69+
}
70+
71+
true
6072
}
6173

6274
}

0 commit comments

Comments
 (0)