|
| 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 | +} |
0 commit comments