|
| 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.converter |
| 18 | + |
| 19 | +import com.github.hauner.openapi.spring.converter.mapping.AmbiguousTypeMappingException |
| 20 | +import com.github.hauner.openapi.spring.converter.mapping.Mapping |
| 21 | +import com.github.hauner.openapi.spring.converter.mapping.TargetType |
| 22 | +import com.github.hauner.openapi.spring.converter.mapping.TargetTypeMapping |
| 23 | +import com.github.hauner.openapi.spring.model.datatypes.DataType |
| 24 | +import com.github.hauner.openapi.spring.model.datatypes.NoneDataType |
| 25 | +import com.github.hauner.openapi.spring.model.datatypes.SingleDataType |
| 26 | + |
| 27 | +/** |
| 28 | + * wraps the data type with the 'singe' data mapping. |
| 29 | + * |
| 30 | + * Used to wrap Responses or RequestBody's with {@code Mono<>} or similar types. |
| 31 | + * |
| 32 | + * @author Martin Hauner |
| 33 | + */ |
| 34 | +class SingleDataTypeWrapper { |
| 35 | + |
| 36 | + private ApiOptions options |
| 37 | + private MappingFinder finder |
| 38 | + |
| 39 | + SingleDataTypeWrapper (ApiOptions options) { |
| 40 | + this.options = options |
| 41 | + this.finder = new MappingFinder(typeMappings: options.typeMappings) |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * wraps a (converted) on-array data type with the configured single data type like |
| 46 | + * {@code Mono<>} ec. |
| 47 | + * |
| 48 | + * If the configuration for the result type is 'plain' or not defined the source data type |
| 49 | + * is not wrapped. |
| 50 | + * |
| 51 | + * @param dataType the data type to wrap |
| 52 | + * @param schemaInfo the open api type with context information |
| 53 | + * @return the resulting java data type |
| 54 | + */ |
| 55 | + DataType wrap (DataType dataType, SchemaInfo schemaInfo) { |
| 56 | + def targetType = getSingleResultDataType (schemaInfo) |
| 57 | + if (!targetType || schemaInfo.isArray ()) { |
| 58 | + return dataType |
| 59 | + } |
| 60 | + |
| 61 | + if (targetType.typeName == 'plain') { |
| 62 | + return dataType |
| 63 | + } |
| 64 | + |
| 65 | + def wrappedType = new SingleDataType ( |
| 66 | + type: targetType.name, |
| 67 | + pkg: targetType.pkg, |
| 68 | + dataType: checkNone (dataType) |
| 69 | + ) |
| 70 | + |
| 71 | + return wrappedType |
| 72 | + } |
| 73 | + |
| 74 | + private TargetType getSingleResultDataType (SchemaInfo info) { |
| 75 | + // check endpoint single mapping |
| 76 | + List<Mapping> endpointMatches = finder.findEndpointSingleMapping (info) |
| 77 | + |
| 78 | + if (!endpointMatches.empty) { |
| 79 | + |
| 80 | + if (endpointMatches.size () != 1) { |
| 81 | + throw new AmbiguousTypeMappingException (endpointMatches) |
| 82 | + } |
| 83 | + |
| 84 | + TargetType target = (endpointMatches.first() as TargetTypeMapping).targetType |
| 85 | + if (target) { |
| 86 | + return target |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // find global single mapping |
| 91 | + List<Mapping> typeMatches = finder.findSingleMapping (info) |
| 92 | + if (typeMatches.isEmpty ()) { |
| 93 | + return null |
| 94 | + } |
| 95 | + |
| 96 | + if (typeMatches.size () != 1) { |
| 97 | + throw new AmbiguousTypeMappingException (typeMatches) |
| 98 | + } |
| 99 | + |
| 100 | + def match = typeMatches.first () as TargetTypeMapping |
| 101 | + return match.targetType |
| 102 | + } |
| 103 | + |
| 104 | + private DataType checkNone (DataType dataType) { |
| 105 | + if (dataType instanceof NoneDataType) { |
| 106 | + return dataType.wrappedInResult () |
| 107 | + } |
| 108 | + |
| 109 | + dataType |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments