|
| 1 | +/* |
| 2 | + * Copyright (c) 2019-2020 Oracle and/or its affiliates. |
| 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 io.helidon.openapi; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.eclipse.microprofile.openapi.models.PathItem; |
| 23 | +import org.eclipse.microprofile.openapi.models.Paths; |
| 24 | +import org.eclipse.microprofile.openapi.models.callbacks.Callback; |
| 25 | +import org.eclipse.microprofile.openapi.models.media.Content; |
| 26 | +import org.eclipse.microprofile.openapi.models.media.MediaType; |
| 27 | +import org.eclipse.microprofile.openapi.models.responses.APIResponse; |
| 28 | +import org.eclipse.microprofile.openapi.models.responses.APIResponses; |
| 29 | +import org.eclipse.microprofile.openapi.models.security.Scopes; |
| 30 | +import org.eclipse.microprofile.openapi.models.security.SecurityRequirement; |
| 31 | +import org.eclipse.microprofile.openapi.models.servers.ServerVariable; |
| 32 | +import org.eclipse.microprofile.openapi.models.servers.ServerVariables; |
| 33 | +import org.yaml.snakeyaml.TypeDescription; |
| 34 | +import org.yaml.snakeyaml.constructor.Constructor; |
| 35 | +import org.yaml.snakeyaml.nodes.MappingNode; |
| 36 | +import org.yaml.snakeyaml.nodes.Node; |
| 37 | +import org.yaml.snakeyaml.nodes.NodeId; |
| 38 | +import org.yaml.snakeyaml.nodes.SequenceNode; |
| 39 | + |
| 40 | +/** |
| 41 | + * Specialized SnakeYAML constructor for modifying {@code Node} objects for OpenAPI types that extend {@code Map} to adjust the |
| 42 | + * type of the child nodes of such nodes. |
| 43 | + * <p> |
| 44 | + * Several MicroProfile OpenAPI interfaces extend {@code Map}. For example, {@code Paths} extends {@code Map |
| 45 | + * <String, PathItem>} and {@code SecurityRequirement} extends {@code Map<String, List<String>>}. When SnakeYAML builds the node |
| 46 | + * corresponding to one of these types, it correctly creates each child node as a {@code MappingNode} but it assigns those |
| 47 | + * child nodes a type of {@code Object} instead of the mapped type -- {@code PathItem} in the example above. |
| 48 | + * </p> |
| 49 | + * <p> |
| 50 | + * This class customizes the preparation of the node tree in these situations by setting the types for the child nodes explicitly |
| 51 | + * to the corresponding child type. In OpenAPI 1.1.2 there are two situations, depending on whether the mapped-to type is a |
| 52 | + * {@code List} or not. |
| 53 | + * </p> |
| 54 | + * <p> |
| 55 | + * The MicroProfile OpenAPI 2.0 versions of the interfaces no longer use this construct of an interface extending {@code Map}, so |
| 56 | + * ideally we can remove this workaround when we adopt 2.0. |
| 57 | + * </p> |
| 58 | + */ |
| 59 | +final class CustomConstructor extends Constructor { |
| 60 | + |
| 61 | + // maps OpenAPI interfaces which extend Map<?, type> to the mapped-to type where that mapped-to type is NOT List |
| 62 | + private static final Map<Class<?>, Class<?>> CHILD_MAP_TYPES = new HashMap<>(); |
| 63 | + |
| 64 | + // maps OpenAPI interfaces which extend Map<?, List<type>> to the type that appears in the list |
| 65 | + private static final Map<Class<?>, Class<?>> CHILD_MAP_OF_LIST_TYPES = new HashMap<>(); |
| 66 | + |
| 67 | + static { |
| 68 | + CHILD_MAP_TYPES.put(Paths.class, PathItem.class); |
| 69 | + CHILD_MAP_TYPES.put(Callback.class, PathItem.class); |
| 70 | + CHILD_MAP_TYPES.put(Content.class, MediaType.class); |
| 71 | + CHILD_MAP_TYPES.put(APIResponses.class, APIResponse.class); |
| 72 | + CHILD_MAP_TYPES.put(ServerVariables.class, ServerVariable.class); |
| 73 | + CHILD_MAP_TYPES.put(Scopes.class, String.class); |
| 74 | + CHILD_MAP_OF_LIST_TYPES.put(SecurityRequirement.class, String.class); |
| 75 | + } |
| 76 | + |
| 77 | + CustomConstructor(TypeDescription td) { |
| 78 | + super(td); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected void constructMapping2ndStep(MappingNode node, Map<Object, Object> mapping) { |
| 83 | + Class<?> parentType = node.getType(); |
| 84 | + if (CHILD_MAP_TYPES.containsKey(parentType)) { |
| 85 | + Class<?> childType = CHILD_MAP_TYPES.get(parentType); |
| 86 | + node.getValue().forEach(tuple -> { |
| 87 | + Node valueNode = tuple.getValueNode(); |
| 88 | + if (valueNode.getType() == Object.class) { |
| 89 | + valueNode.setType(childType); |
| 90 | + } |
| 91 | + }); |
| 92 | + } else if (CHILD_MAP_OF_LIST_TYPES.containsKey(parentType)) { |
| 93 | + Class<?> childType = CHILD_MAP_OF_LIST_TYPES.get(parentType); |
| 94 | + node.getValue().forEach(tuple -> { |
| 95 | + Node valueNode = tuple.getValueNode(); |
| 96 | + if (valueNode.getNodeId() == NodeId.sequence) { |
| 97 | + SequenceNode seqNode = (SequenceNode) valueNode; |
| 98 | + seqNode.setListType(childType); |
| 99 | + } |
| 100 | + }); |
| 101 | + } |
| 102 | + super.constructMapping2ndStep(node, mapping); |
| 103 | + } |
| 104 | +} |
0 commit comments