Skip to content

Commit 9aa7b3e

Browse files
committed
merged primitive type mapping object type mapping
1 parent 27cafe9 commit 9aa7b3e

4 files changed

Lines changed: 92 additions & 36 deletions

File tree

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

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
2222
import com.github.hauner.openapi.spring.converter.mapping.TypeMappingX
2323
import com.github.hauner.openapi.spring.converter.schema.ArraySchemaType
2424
import com.github.hauner.openapi.spring.converter.schema.ObjectSchemaType
25+
import com.github.hauner.openapi.spring.converter.schema.PrimitiveSchemaType
2526
import com.github.hauner.openapi.spring.converter.schema.SchemaInfo
2627
import com.github.hauner.openapi.spring.converter.schema.SchemaType
2728
import com.github.hauner.openapi.spring.model.DataTypes
@@ -154,7 +155,7 @@ class DataTypeConverter {
154155

155156
private DataType createSimpleDataType (SchemaInfo schemaInfo) {
156157

157-
TargetType targetType = getSimpleDataType (schemaInfo)
158+
TargetType targetType = getMappedDataType (new PrimitiveSchemaType(schemaInfo))
158159
if (targetType) {
159160
def simpleType = new MappedDataType (
160161
type: targetType.name,
@@ -247,37 +248,4 @@ class DataTypeConverter {
247248
return match.targetType
248249
}
249250

250-
private TargetType getSimpleDataType (SchemaInfo schemaInfo) {
251-
if (options.typeMappings) {
252-
253-
// check global mapping
254-
List<TypeMapping> mappings = getTypeMappingsY ()
255-
List<TypeMapping> matches = mappings.findAll {
256-
it.sourceTypeName == schemaInfo.type && it.sourceTypeFormat == schemaInfo.format
257-
}
258-
259-
// no mapping, use default
260-
if (matches.isEmpty ()) {
261-
return null
262-
}
263-
264-
if (matches.size () != 1) {
265-
throw new AmbiguousTypeMappingException (matches)
266-
}
267-
268-
def match = matches.first ()
269-
return new TargetType (
270-
typeName: match.targetTypeName,
271-
genericNames: match.genericTypeNames ?: [])
272-
}
273-
274-
null
275-
}
276-
277-
private List<TypeMapping> getTypeMappingsY () {
278-
options.typeMappings.findResults {
279-
it instanceof TypeMapping ? it : null
280-
}
281-
}
282-
283251
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,19 @@ class ArraySchemaType extends BaseSchemaType {
102102

103103
}
104104

105+
class PrimitiveSchemaType extends BaseSchemaType {
105106

107+
PrimitiveSchemaType (SchemaInfo info) {
108+
super (info)
109+
}
110+
111+
@Override
112+
List<TypeMappingX> matchTypeMapping (List<TypeMappingX> typeMappings) {
113+
typeMappings.findAll () {
114+
(it.isLevel (MappingLevel.TYPE)
115+
&& it.sourceTypeName == info.type
116+
&& it.sourceTypeFormat == info.format)
117+
}
118+
}
119+
120+
}

src/test/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverterArrayTypeMappingSpec.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ paths:
302302
}
303303

304304
@Unroll
305-
void "converts array response schema to Collection<> via type" () {
305+
void "converts array response schema to Collection<> via #type" () {
306306
def openApi = parse ("""\
307307
openapi: 3.0.2
308308
info:

src/test/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverterSimpleTypeMappingSpec.groovy renamed to src/test/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverterPrimitiveTypeMappingSpec.groovy

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
package com.github.hauner.openapi.spring.converter
1818

1919
import com.github.hauner.openapi.spring.converter.mapping.AmbiguousTypeMappingException
20+
import com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping
21+
import com.github.hauner.openapi.spring.converter.mapping.ParameterTypeMapping
2022
import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
2123
import com.github.hauner.openapi.spring.model.Api
2224
import spock.lang.Specification
25+
import spock.lang.Unroll
2326

2427
import static com.github.hauner.openapi.spring.support.OpenApiParser.parse
2528

26-
class DataTypeConverterSimpleTypeMappingSpec extends Specification {
29+
class DataTypeConverterPrimitiveTypeMappingSpec extends Specification {
2730

2831
void "converts basic types with format to java type via global type mapping" () {
2932
def openApi = parse ("""\
@@ -108,4 +111,74 @@ paths:
108111
e.typeMappings == options.typeMappings
109112
}
110113

114+
@Unroll
115+
void "converts primitive parameter schema to java type via #type" () {
116+
def openApi = parse ("""\
117+
openapi: 3.0.2
118+
info:
119+
title: API
120+
version: 1.0.0
121+
122+
paths:
123+
/foo:
124+
get:
125+
parameters:
126+
- in: query
127+
name: bar
128+
required: false
129+
schema:
130+
type: string
131+
format: date-time
132+
responses:
133+
'204':
134+
description: none
135+
""")
136+
137+
when:
138+
def options = new ApiOptions(packageName: 'pkg', typeMappings: mappings)
139+
Api api = new ApiConverter (options).convert (openApi)
140+
141+
then:
142+
def itf = api.interfaces.first ()
143+
def ep = itf.endpoints.first ()
144+
def parameter = ep.parameters.first ()
145+
parameter.dataType.packageName == 'java.time'
146+
parameter.dataType.name == 'ZonedDateTime'
147+
148+
where:
149+
type << [
150+
'endpoint parameter mapping',
151+
'global parameter mapping',
152+
'global type mapping'
153+
]
154+
155+
mappings << [
156+
[
157+
new EndpointTypeMapping (path: '/foo',
158+
typeMappings: [
159+
new ParameterTypeMapping (
160+
parameterName: 'bar',
161+
mapping: new TypeMapping (
162+
sourceTypeName: 'string',
163+
sourceTypeFormat: 'date-time',
164+
targetTypeName: 'java.time.ZonedDateTime')
165+
)
166+
])
167+
], [
168+
new ParameterTypeMapping (
169+
parameterName: 'bar',
170+
mapping: new TypeMapping (
171+
sourceTypeName: 'string',
172+
sourceTypeFormat: 'date-time',
173+
targetTypeName: 'java.time.ZonedDateTime')
174+
)
175+
], [
176+
new TypeMapping (
177+
sourceTypeName: 'string',
178+
sourceTypeFormat: 'date-time',
179+
targetTypeName: 'java.time.ZonedDateTime')
180+
]
181+
]
182+
}
183+
111184
}

0 commit comments

Comments
 (0)