1616
1717package com.github.hauner.openapi.spring.generatr
1818
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
1924import com.github.hauner.openapi.spring.converter.mapping.AddParameterTypeMapping
2025import com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping
2126import com.github.hauner.openapi.spring.converter.mapping.ParameterTypeMapping
2227import com.github.hauner.openapi.spring.converter.mapping.ResponseTypeMapping
2328import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
2429import com.github.hauner.openapi.spring.converter.mapping.Mapping
25- import org.yaml.snakeyaml.Yaml
30+ import com.github.hauner.openapi.spring.generatr.mapping.AdditionalParameter
31+ import com.github.hauner.openapi.spring.generatr.mapping.Mapping as YamlMapping
32+ import com.github.hauner.openapi.spring.generatr.mapping.Parameter
33+ import com.github.hauner.openapi.spring.generatr.mapping.Parameter as YamlParameter
34+ import com.github.hauner.openapi.spring.generatr.mapping.ParameterDeserializer
35+ import com.github.hauner.openapi.spring.generatr.mapping.Path as YamlPath
36+ import com.github.hauner.openapi.spring.generatr.mapping.RequestParameter
37+ import com.github.hauner.openapi.spring.generatr.mapping.Response as YamlResponse
38+ import com.github.hauner.openapi.spring.generatr.mapping.Type as YamlType
2639
2740import java.util.regex.Matcher
2841import java.util.regex.Pattern
@@ -36,7 +49,7 @@ class TypeMappingReader {
3649 private Pattern GENERIC_INLINE = ~/ (.+?)<(.+?)>/
3750
3851 List<Mapping > read (String typeMappings ) {
39- if (typeMappings == null ) {
52+ if (typeMappings == null || typeMappings . empty ) {
4053 return []
4154 }
4255
@@ -45,90 +58,91 @@ class TypeMappingReader {
4558 mapping = new File (typeMappings). text
4659 }
4760
48- Yaml yaml = new Yaml ()
49- Map props = yaml . load (mapping)
61+ def mapper = createYamlParser ()
62+ def props = mapper . readValue (mapping, YamlMapping )
5063
51- if (props == null ) {
52- return null
53- }
54-
55- parse (props)
64+ convert (props)
5665 }
5766
58- private List<Mapping > parse ( Map< String , ?> props ) {
59- // def version = props.get ('openapi-generatr-spring' )
67+ private List<Mapping > convert ( YamlMapping source ) {
68+ def result = new ArrayList< Mapping > ( )
6069
61- def root = props. get (' map' ) as Map<String , ?>
70+ source. map. types. each {
71+ result. add (convert (it))
72+ }
6273
63- def mappings = readTypeMappings (root)
74+ source. map. parameters. each {
75+ result. add (convert (it))
76+ }
6477
65- def paths = root. get (' paths' ) as Map<String , ?>
66- paths. each {
67- def epm = new EndpointTypeMapping (path : it. key)
68- def types = readTypeMappings (it. value as Map<String , ?> )
69- epm. typeMappings = types
70- mappings. add (epm)
78+ source. map. responses. each {
79+ result. add (convert (it))
7180 }
7281
73- mappings
82+ source. map. paths. each {
83+ result. add(convert (it. key, it. value))
84+ }
85+
86+ result
7487 }
7588
76- private List< Mapping > readTypeMappings ( Map< String , ?> root ) {
77- def mappings = []
89+ private EndpointTypeMapping convert ( String path , YamlPath source ) {
90+ def result = new ArrayList< Mapping > ()
7891
79- def types = root. get (' types' ) as List<Map<String , ?> >
80- types. each { Map<String , ?> it ->
81- mappings. add (readTypMapping (it))
92+ source. types. each {
93+ result. add (convert (it))
8294 }
8395
84- def responses = root. get (' responses' ) as List<Map<String , ?> >
85- responses. each {
86- mappings. add (readResponseTypeMapping (it))
96+ source. parameters. each {
97+ result. add (convert (it))
8798 }
8899
89- def parameters = root. get (' parameters' ) as List<Map<String , ?> >
90- parameters. each {
91- mappings. add (readParameterTypeMapping (it))
100+ source. responses. each {
101+ result. add (convert (it))
92102 }
93103
94- return mappings
104+ new EndpointTypeMapping ( path : path, typeMappings : result)
95105 }
96106
97- private Mapping readParameterTypeMapping ( Map< String , ?> source ) {
98- if (isParameterMappings ( source) ) {
107+ private Mapping convert ( YamlParameter source ) {
108+ if (source instanceof RequestParameter ) {
99109 def name = source. name
100- def mapping = readTypMapping (source)
110+ def mapping = convert (new YamlType (
111+ from : null ,
112+ to : source. to,
113+ generics : source. generics
114+ ))
101115 new ParameterTypeMapping (parameterName : name, mapping : mapping)
102116
103- } else if (isParameterAddition ( source) ) {
117+ } else if (source instanceof AdditionalParameter ) {
104118 def name = source. add
105- def mapping = readTypMapping (source, ' as' )
119+ def mapping = convert (new YamlType (
120+ from : null ,
121+ to : source. to,
122+ generics : source. generics
123+ ))
106124 new AddParameterTypeMapping (parameterName : name, mapping : mapping)
107125
108126 } else {
109127 throw new Exception (" unknown parameter mapping $source " )
110128 }
111129 }
112130
113- private boolean isParameterAddition (Map<String , ?> source ) {
114- source. containsKey (' add' ) && source. containsKey (' as' )
115- }
116-
117- private boolean isParameterMappings (Map<String , ?> source ) {
118- source. containsKey (' name' ) && source. containsKey (' to' )
119- }
120-
121- private ResponseTypeMapping readResponseTypeMapping (Map<String , ?> source ) {
131+ private ResponseTypeMapping convert (YamlResponse source ) {
122132 def content = source. content
123- def mapping = readTypMapping (source)
133+ def mapping = convert (new YamlType (
134+ from : null ,
135+ to : source. to,
136+ generics : source. generics
137+ ))
124138 new ResponseTypeMapping (contentType : content, mapping : mapping)
125139 }
126140
127- private TypeMapping readTypMapping ( Map< String , ?> source , String target = ' to ' ) {
128- Matcher matcher = source . to =~ GENERIC_INLINE
141+ private TypeMapping convert ( YamlType type ) {
142+ Matcher matcher = type . to =~ GENERIC_INLINE
129143
130- def (from, format) = source . from ? (source . from as String ). tokenize (' :' ) : [null , null ]
131- String to = source[target]
144+ def (from, format) = type . from ? (type . from as String ). tokenize (' :' ) : [null , null ]
145+ String to = type . to
132146 List<String > generics = []
133147
134148 // has inline generics
@@ -140,15 +154,25 @@ class TypeMappingReader {
140154 .collect { it. trim () }
141155
142156 // has explicit generic list
143- } else if (source . containsKey ( ' generics' ) ) {
144- generics = source . generics as List
157+ } else if (type . generics) {
158+ generics = type . generics
145159 }
146160
147161 new TypeMapping (
148162 sourceTypeName : from, sourceTypeFormat : format,
149163 targetTypeName : to, genericTypeNames : generics)
150164 }
151165
166+ private ObjectMapper createYamlParser () {
167+ SimpleModule module = new SimpleModule ()
168+ module. addDeserializer (Parameter , new ParameterDeserializer ())
169+
170+ new ObjectMapper (new YAMLFactory ())
171+ .configure (DeserializationFeature . FAIL_ON_UNKNOWN_PROPERTIES , false )
172+ .setPropertyNamingStrategy (PropertyNamingStrategy . KEBAB_CASE )
173+ .registerModule (module)
174+ }
175+
152176 private boolean isFileName (String name ) {
153177 name. endsWith (' .yaml' ) || name. endsWith (' .yml' )
154178 }
0 commit comments