|
| 1 | +/* |
| 2 | + * Copyright 2020-2021 the original author or 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 | + * https://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 org.springframework.graphql.data.method.annotation.support; |
| 18 | + |
| 19 | +import java.lang.reflect.Constructor; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Optional; |
| 24 | +import java.util.Stack; |
| 25 | + |
| 26 | +import org.springframework.beans.BeanUtils; |
| 27 | +import org.springframework.beans.MutablePropertyValues; |
| 28 | +import org.springframework.core.MethodParameter; |
| 29 | +import org.springframework.validation.DataBinder; |
| 30 | + |
| 31 | +/** |
| 32 | + * Instantiate a target type and bind data from |
| 33 | + * {@link graphql.schema.DataFetchingEnvironment} arguments. |
| 34 | + * |
| 35 | + * @author Brian Clozel |
| 36 | + */ |
| 37 | +class GraphQlArgumentInstantiator { |
| 38 | + |
| 39 | + /** |
| 40 | + * Instantiate the given target type and bind data from |
| 41 | + * {@link graphql.schema.DataFetchingEnvironment} arguments. |
| 42 | + * <p>This is considering the default constructor or a primary constructor |
| 43 | + * if available. |
| 44 | + * |
| 45 | + * @param targetType the type of the argument to instantiate |
| 46 | + * @param arguments the data fetching environment arguments |
| 47 | + * @param <T> the type of the input argument |
| 48 | + * @return the instantiated and populated input argument. |
| 49 | + * @throws IllegalStateException if there is no suitable constructor. |
| 50 | + */ |
| 51 | + @SuppressWarnings("unchecked") |
| 52 | + public <T> T instantiate(Class<T> targetType, Map<String, Object> arguments) { |
| 53 | + Object target; |
| 54 | + Constructor<?> ctor = BeanUtils.getResolvableConstructor(targetType); |
| 55 | + MutablePropertyValues propertyValues = extractPropertyValues(arguments); |
| 56 | + |
| 57 | + if (ctor.getParameterCount() == 0) { |
| 58 | + target = BeanUtils.instantiateClass(ctor); |
| 59 | + DataBinder dataBinder = new DataBinder(target); |
| 60 | + dataBinder.bind(propertyValues); |
| 61 | + } |
| 62 | + else { |
| 63 | + // Data class constructor |
| 64 | + DataBinder binder = new DataBinder(null); |
| 65 | + String[] paramNames = BeanUtils.getParameterNames(ctor); |
| 66 | + Class<?>[] paramTypes = ctor.getParameterTypes(); |
| 67 | + Object[] args = new Object[paramTypes.length]; |
| 68 | + for (int i = 0; i < paramNames.length; i++) { |
| 69 | + String paramName = paramNames[i]; |
| 70 | + Object value = propertyValues.get(paramName); |
| 71 | + value = (value instanceof List ? ((List<?>) value).toArray() : value); |
| 72 | + MethodParameter methodParam = new MethodParameter(ctor, i); |
| 73 | + if (value == null && methodParam.isOptional()) { |
| 74 | + args[i] = (methodParam.getParameterType() == Optional.class ? Optional.empty() : null); |
| 75 | + } |
| 76 | + else { |
| 77 | + args[i] = binder.convertIfNecessary(value, paramTypes[i], methodParam); |
| 78 | + } |
| 79 | + } |
| 80 | + target = BeanUtils.instantiateClass(ctor, args); |
| 81 | + } |
| 82 | + return (T) target; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Perform a Depth First Search in the given JSON map to collect attribute values |
| 87 | + * as {@link MutablePropertyValues} using the full property path as key. |
| 88 | + */ |
| 89 | + private MutablePropertyValues extractPropertyValues(Map<String, Object> arguments) { |
| 90 | + MutablePropertyValues mpvs = new MutablePropertyValues(); |
| 91 | + Stack<String> path = new Stack<>(); |
| 92 | + visitArgumentMap(arguments, mpvs, path); |
| 93 | + return mpvs; |
| 94 | + } |
| 95 | + |
| 96 | + @SuppressWarnings("unchecked") |
| 97 | + private void visitArgumentMap(Map<String, Object> arguments, MutablePropertyValues mpvs, Stack<String> path) { |
| 98 | + for (String key : arguments.keySet()) { |
| 99 | + Object value = arguments.get(key); |
| 100 | + if (value instanceof List) { |
| 101 | + List<Object> items = (List<Object>) value; |
| 102 | + Map<String, Object> subValues = new HashMap<>(items.size()); |
| 103 | + for (int i = 0; i < items.size(); i++) { |
| 104 | + subValues.put(key + "[" + i + "]", items.get(i)); |
| 105 | + } |
| 106 | + visitArgumentMap(subValues, mpvs, path); |
| 107 | + } |
| 108 | + else if (value instanceof Map) { |
| 109 | + path.push(key); |
| 110 | + path.push("."); |
| 111 | + visitArgumentMap((Map<String, Object>) value, mpvs, path); |
| 112 | + path.pop(); |
| 113 | + path.pop(); |
| 114 | + } |
| 115 | + else { |
| 116 | + path.push(key); |
| 117 | + String propertyName = pathToPropertyName(path); |
| 118 | + mpvs.add(propertyName, value); |
| 119 | + path.pop(); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private String pathToPropertyName(Stack<String> path) { |
| 125 | + StringBuilder sb = new StringBuilder(); |
| 126 | + for (String s : path) { |
| 127 | + sb.append(s); |
| 128 | + } |
| 129 | + return sb.toString(); |
| 130 | + } |
| 131 | +} |
0 commit comments