Skip to content

Commit f733dbb

Browse files
committed
want to access the parsed and un-converted yaml
1 parent 8741f0f commit f733dbb

3 files changed

Lines changed: 146 additions & 36 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 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.fasterxml.jackson.databind.DeserializationFeature
20+
import com.fasterxml.jackson.databind.ObjectMapper
21+
import com.fasterxml.jackson.databind.PropertyNamingStrategy
22+
import com.fasterxml.jackson.databind.module.SimpleModule
23+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
24+
import com.github.hauner.openapi.spring.generatr.mapping.Mapping
25+
import com.github.hauner.openapi.spring.generatr.mapping.Parameter
26+
import com.github.hauner.openapi.spring.generatr.mapping.ParameterDeserializer
27+
28+
import java.util.regex.Pattern
29+
30+
/**
31+
* Reader for mapping yaml.
32+
*
33+
* @author Martin Hauner
34+
*/
35+
class MappingReader {
36+
private Pattern GENERIC_INLINE = ~/(.+?)<(.+?)>/
37+
38+
Mapping read (String typeMappings) {
39+
if (typeMappings == null || typeMappings.empty) {
40+
return null
41+
}
42+
43+
String mapping = typeMappings
44+
if (isFileName (typeMappings)) {
45+
mapping = new File (typeMappings).text
46+
}
47+
48+
def mapper = createYamlParser ()
49+
50+
mapper.readValue (mapping, Mapping)
51+
}
52+
53+
private ObjectMapper createYamlParser () {
54+
SimpleModule module = new SimpleModule ()
55+
module.addDeserializer (Parameter, new ParameterDeserializer ())
56+
57+
new ObjectMapper (new YAMLFactory ())
58+
.configure (DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
59+
.setPropertyNamingStrategy (PropertyNamingStrategy.KEBAB_CASE)
60+
.registerModule (module)
61+
}
62+
63+
private boolean isFileName (String name) {
64+
name.endsWith ('.yaml') || name.endsWith ('.yml')
65+
}
66+
67+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 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 org.junit.Rule
20+
import org.junit.rules.TemporaryFolder
21+
import spock.lang.Specification
22+
23+
class MappingReaderSpec extends Specification {
24+
25+
@Rule
26+
TemporaryFolder folder
27+
28+
void "ignores empty type mapping" () {
29+
when:
30+
def reader = new MappingReader()
31+
def mapping = reader.read (yaml)
32+
33+
then:
34+
mapping == null
35+
36+
where:
37+
yaml << [null, ""]
38+
}
39+
40+
void "reads mapping from file" () {
41+
def yaml = """\
42+
openapi-generatr-spring: v1.0
43+
44+
map:
45+
types:
46+
- from: array
47+
to: java.util.Collection
48+
"""
49+
50+
def yamlFile = folder.newFile ("openapi-generatr-spring.yaml")
51+
yamlFile.text = yaml
52+
53+
when:
54+
def reader = new MappingReader()
55+
def mapping = reader.read (yamlFile.absolutePath)
56+
57+
then:
58+
mapping
59+
}
60+
61+
void "reads mapping from string" () {
62+
def yaml = """\
63+
openapi-generatr-spring: v1.0
64+
65+
map:
66+
types:
67+
- from: array
68+
to: java.util.Collection
69+
"""
70+
71+
when:
72+
def reader = new MappingReader()
73+
def mapping = reader.read (yaml)
74+
75+
then:
76+
mapping
77+
}
78+
79+
}

‎src/test/groovy/com/github/hauner/openapi/spring/generatr/TypeMappingReaderSpec.groovy‎

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,6 @@ import spock.lang.Specification
2727

2828
class TypeMappingReaderSpec extends Specification {
2929

30-
@Rule
31-
TemporaryFolder folder
32-
33-
void "ignores empty type mapping" () {
34-
when:
35-
def reader = new TypeMappingReader()
36-
def mappings = reader.read (yaml)
37-
38-
then:
39-
mappings == []
40-
41-
where:
42-
yaml << [null, ""]
43-
}
44-
45-
void "reads mapping from file" () {
46-
def yaml = """\
47-
openapi-generatr-spring: v1.0
48-
49-
map:
50-
types:
51-
- from: array
52-
to: java.util.Collection
53-
"""
54-
55-
def yamlFile = folder.newFile ("openapi-generatr-spring.yaml")
56-
yamlFile.text = yaml
57-
58-
when:
59-
def reader = new TypeMappingReader()
60-
def mappings = reader.read (yamlFile.absolutePath)
61-
62-
then:
63-
mappings.size () == 1
64-
}
65-
6630
void "reads global type mapping" () {
6731
String yaml = """\
6832
openapi-generatr-spring: v1.0

0 commit comments

Comments
 (0)