Skip to content

Commit 99430fd

Browse files
committed
extracted mapping converter
1 parent 157c379 commit 99430fd

2 files changed

Lines changed: 169 additions & 19 deletions

File tree

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright 2019-2020 the original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.github.hauner.openapi.spring.generatr
18+
19+
import com.github.hauner.openapi.spring.converter.mapping.AddParameterTypeMapping
20+
import com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping
21+
import com.github.hauner.openapi.spring.converter.mapping.Mapping
22+
import com.github.hauner.openapi.spring.converter.mapping.ParameterTypeMapping
23+
import com.github.hauner.openapi.spring.converter.mapping.ResponseTypeMapping
24+
import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
25+
import com.github.hauner.openapi.spring.generatr.mapping.AdditionalParameter
26+
import com.github.hauner.openapi.spring.generatr.mapping.Mapping as YamlMapping
27+
import com.github.hauner.openapi.spring.generatr.mapping.Parameter
28+
import com.github.hauner.openapi.spring.generatr.mapping.Path
29+
import com.github.hauner.openapi.spring.generatr.mapping.RequestParameter
30+
import com.github.hauner.openapi.spring.generatr.mapping.Response
31+
import com.github.hauner.openapi.spring.generatr.mapping.Type
32+
33+
import java.util.regex.Matcher
34+
import java.util.regex.Pattern
35+
36+
/**
37+
* Converter for the type mapping from the mapping yaml. It converts the type mapping information
38+
* into the format used by {@link com.github.hauner.openapi.spring.converter.DataTypeConverter}.
39+
*
40+
* @author Martin Hauner
41+
*/
42+
class MappingConverter {
43+
private Pattern GENERIC_INLINE = ~/(.+?)<(.+?)>/
44+
45+
List<Mapping> convert (YamlMapping source) {
46+
def result = new ArrayList<Mapping>()
47+
48+
source.map.types.each {
49+
result.add (convertType (it))
50+
}
51+
52+
source.map.parameters.each {
53+
result.add (convertParameter (it))
54+
}
55+
56+
source.map.responses.each {
57+
result.add (convertResponse (it))
58+
}
59+
60+
source.map.paths.each {
61+
result.add(convertPath (it.key, it.value))
62+
}
63+
64+
result
65+
}
66+
67+
private TypeMapping convertType (Type type) {
68+
Matcher matcher = type.to =~ GENERIC_INLINE
69+
70+
def (from, format) = type.from ? (type.from as String).tokenize (':') : [null, null]
71+
String to = type.to
72+
List<String> generics = []
73+
74+
// has inline generics
75+
if (matcher.find ()) {
76+
to = matcher.group (1)
77+
generics = matcher
78+
.group (2)
79+
.split (',')
80+
.collect { it.trim () }
81+
82+
// has explicit generic list
83+
} else if (type.generics) {
84+
generics = type.generics
85+
}
86+
87+
new TypeMapping (
88+
sourceTypeName: from, sourceTypeFormat: format,
89+
targetTypeName: to, genericTypeNames: generics)
90+
}
91+
92+
private Mapping convertParameter (Parameter source) {
93+
if (source instanceof RequestParameter) {
94+
def name = source.name
95+
def mapping = convertType (new Type(
96+
from: null,
97+
to: source.to,
98+
generics: source.generics
99+
))
100+
new ParameterTypeMapping (parameterName: name, mapping: mapping)
101+
102+
} else if (source instanceof AdditionalParameter) {
103+
def name = source.add
104+
def mapping = convertType (new Type(
105+
from: null,
106+
to: source.to,
107+
generics: source.generics
108+
))
109+
new AddParameterTypeMapping (parameterName: name, mapping: mapping)
110+
111+
} else {
112+
throw new Exception("unknown parameter mapping $source")
113+
}
114+
}
115+
116+
private ResponseTypeMapping convertResponse (Response source) {
117+
def content = source.content
118+
def mapping = convertType (new Type(
119+
from: null,
120+
to: source.to,
121+
generics: source.generics
122+
))
123+
new ResponseTypeMapping(contentType: content, mapping: mapping)
124+
}
125+
126+
private EndpointTypeMapping convertPath (String path, Path source) {
127+
def result = new ArrayList<Mapping>()
128+
129+
source.types.each {
130+
result.add (convertType (it))
131+
}
132+
133+
source.parameters.each {
134+
result.add (convertParameter (it))
135+
}
136+
137+
source.responses.each {
138+
result.add (convertResponse (it))
139+
}
140+
141+
new EndpointTypeMapping(path: path, typeMappings: result)
142+
}
143+
144+
}

src/test/groovy/com/github/hauner/openapi/spring/generatr/TypeMappingReaderSpec.groovy renamed to src/test/groovy/com/github/hauner/openapi/spring/generatr/MappingConverterSpec.groovy

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@ import com.github.hauner.openapi.spring.converter.mapping.ParameterTypeMapping
2222
import com.github.hauner.openapi.spring.converter.mapping.ResponseTypeMapping
2323
import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
2424
import spock.lang.Specification
25+
import spock.lang.Subject
2526

26-
class TypeMappingReaderSpec extends Specification {
27+
class MappingConverterSpec extends Specification {
28+
29+
def reader = new MappingReader()
30+
31+
@Subject
32+
def converter = new MappingConverter()
2733

2834
void "reads global type mapping" () {
2935
String yaml = """\
@@ -36,8 +42,8 @@ map:
3642
"""
3743

3844
when:
39-
def reader = new TypeMappingReader()
40-
def mappings = reader.read (yaml)
45+
def mapping = reader.read (yaml)
46+
def mappings = converter.convert (mapping)
4147

4248
then:
4349
mappings.size () == 1
@@ -67,8 +73,8 @@ map:
6773
"""
6874

6975
when:
70-
def reader = new TypeMappingReader()
71-
def mappings = reader.read (yaml)
76+
def mapping = reader.read (yaml)
77+
def mappings = converter.convert (mapping)
7278

7379
then:
7480
mappings.size () == 2
@@ -97,8 +103,8 @@ map:
97103
"""
98104

99105
when:
100-
def reader = new TypeMappingReader()
101-
def mappings = reader.read (yaml)
106+
def mapping = reader.read (yaml)
107+
def mappings = converter.convert (mapping)
102108

103109
then:
104110
mappings.size () == 1
@@ -120,8 +126,8 @@ map:
120126
"""
121127

122128
when:
123-
def reader = new TypeMappingReader()
124-
def mappings = reader.read (yaml)
129+
def mapping = reader.read (yaml)
130+
def mappings = converter.convert (mapping)
125131

126132
then:
127133
mappings.size() == 1
@@ -147,8 +153,8 @@ map:
147153
"""
148154

149155
when:
150-
def reader = new TypeMappingReader()
151-
def mappings = reader.read (yaml)
156+
def mapping = reader.read (yaml)
157+
def mappings = converter.convert (mapping)
152158

153159
then:
154160
mappings.size() == 1
@@ -176,8 +182,8 @@ map:
176182
"""
177183

178184
when:
179-
def reader = new TypeMappingReader()
180-
def mappings = reader.read (yaml)
185+
def mapping = reader.read (yaml)
186+
def mappings = converter.convert (mapping)
181187

182188
then:
183189
mappings.size() == 1
@@ -203,8 +209,8 @@ map:
203209
"""
204210

205211
when:
206-
def reader = new TypeMappingReader()
207-
def mappings = reader.read (yaml)
212+
def mapping = reader.read (yaml)
213+
def mappings = converter.convert (mapping)
208214

209215
then:
210216
mappings.size() == 1
@@ -233,8 +239,8 @@ map:
233239
"""
234240

235241
when:
236-
def reader = new TypeMappingReader()
237-
def mappings = reader.read (yaml)
242+
def mapping = reader.read (yaml)
243+
def mappings = converter.convert (mapping)
238244

239245
then:
240246
mappings.size () == 1
@@ -266,8 +272,8 @@ map:
266272
"""
267273

268274
when:
269-
def reader = new TypeMappingReader()
270-
def mappings = reader.read (yaml)
275+
def mapping = reader.read (yaml)
276+
def mappings = converter.convert (mapping)
271277

272278
then:
273279
mappings.size () == 1

0 commit comments

Comments
 (0)