|
17 | 17 | package com.github.hauner.openapi.spring.converter |
18 | 18 |
|
19 | 19 | 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 |
20 | 22 | import com.github.hauner.openapi.spring.converter.mapping.TypeMapping |
21 | 23 | import com.github.hauner.openapi.spring.model.Api |
22 | 24 | import spock.lang.Specification |
| 25 | +import spock.lang.Unroll |
23 | 26 |
|
24 | 27 | import static com.github.hauner.openapi.spring.support.OpenApiParser.parse |
25 | 28 |
|
26 | | -class DataTypeConverterSimpleTypeMappingSpec extends Specification { |
| 29 | +class DataTypeConverterPrimitiveTypeMappingSpec extends Specification { |
27 | 30 |
|
28 | 31 | void "converts basic types with format to java type via global type mapping" () { |
29 | 32 | def openApi = parse ("""\ |
@@ -108,4 +111,74 @@ paths: |
108 | 111 | e.typeMappings == options.typeMappings |
109 | 112 | } |
110 | 113 |
|
| 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 | + |
111 | 184 | } |
0 commit comments