From d53fc6b672b229d42b19768efca762fb2f4fa4d7 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Wed, 7 Aug 2019 15:54:30 +0200 Subject: [PATCH] Added support for passing conversion hint to MessageConverter --- .../BeanFactoryAwareFunctionRegistry.java | 4 +++- .../context/catalog/FunctionTypeUtils.java | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/BeanFactoryAwareFunctionRegistry.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/BeanFactoryAwareFunctionRegistry.java index d0ba385fd..3e84b515a 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/BeanFactoryAwareFunctionRegistry.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/BeanFactoryAwareFunctionRegistry.java @@ -516,7 +516,9 @@ public class BeanFactoryAwareFunctionRegistry } if (value instanceof Message) { // see AWS adapter with Optional payload if (messageNeedsConversion(rawType, (Message) value)) { - convertedValue = messageConverter.fromMessage((Message) value, (Class) rawType); + convertedValue = FunctionTypeUtils.isTypeCollection(type) + ? messageConverter.fromMessage((Message) value, (Class) rawType, type) + : messageConverter.fromMessage((Message) value, (Class) rawType); if (logger.isDebugEnabled()) { logger.debug("Converted from Message: " + convertedValue); } diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/FunctionTypeUtils.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/FunctionTypeUtils.java index 2fa58c03e..4c51aadba 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/FunctionTypeUtils.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/FunctionTypeUtils.java @@ -19,6 +19,7 @@ package org.springframework.cloud.function.context.catalog; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.util.Collection; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; @@ -27,6 +28,7 @@ import org.reactivestreams.Publisher; import org.springframework.cloud.function.context.FunctionRegistration; import org.springframework.core.ResolvableType; +import org.springframework.messaging.Message; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; @@ -42,6 +44,23 @@ public final class FunctionTypeUtils { } + /** + * Will return 'true' if the provided type is a {@link Collection} type. + * This also includes collections wrapped in {@link Message}. For example, + * If provided type is {@code Message>} this operation will return 'true'. + * + * @param type type to interrogate + * @return 'true' if this type represents a {@link Collection}. Otherwise 'false'. + */ + public static boolean isTypeCollection(Type type) { + if (isMessage(type)) { + type = getImmediateGenericType(type, 0); + } + Type rawType = type instanceof ParameterizedType ? ((ParameterizedType) type).getRawType() : type; + + return rawType instanceof Class && Collection.class.isAssignableFrom((Class) rawType); + } + public static Type getFunctionTypeFromFunctionMethod(Method functionMethod) { Assert.isTrue( functionMethod.getName().equals("apply") ||