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