SimpleFunctionRegistryTests: reactive function test case
FunctionTypeUtils#isTypeCollection: unwrap publisher JsonMessageConverter: handler for a ParameterizedType conversionHint refactor
This commit is contained in:
committed by
Oleg Zhurakousky
parent
fdbbcc4e45
commit
a97bdcafd4
@@ -63,14 +63,22 @@ public final class FunctionTypeUtils {
|
||||
* @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 = getPayloadType(type);
|
||||
Type rawType = type instanceof ParameterizedType ? ((ParameterizedType) type).getRawType() : type;
|
||||
|
||||
return rawType instanceof Class<?> && Collection.class.isAssignableFrom((Class<?>) rawType);
|
||||
}
|
||||
|
||||
public static Type getPayloadType(Type type) {
|
||||
if (isPublisher(type)) {
|
||||
type = getImmediateGenericType(type, 0);
|
||||
}
|
||||
if (isMessage(type)) {
|
||||
type = getImmediateGenericType(type, 0);
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Will attempt to discover functional methods on the class. It's applicable for POJOs as well as
|
||||
* functional classes in `java.util.function` package. For the later the names of the methods are
|
||||
|
||||
@@ -763,7 +763,7 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
|
||||
if (value instanceof Message<?>) { // see AWS adapter with Optional payload
|
||||
if (messageNeedsConversion(rawType, (Message<?>) value)) {
|
||||
convertedValue = FunctionTypeUtils.isTypeCollection(type)
|
||||
? messageConverter.fromMessage((Message<?>) value, (Class<?>) rawType, type)
|
||||
? messageConverter.fromMessage((Message<?>) value, (Class<?>) rawType, FunctionTypeUtils.getPayloadType(type))
|
||||
: messageConverter.fromMessage((Message<?>) value, (Class<?>) rawType);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Converted from Message: " + convertedValue);
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.cloud.function.context.config;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import org.springframework.cloud.function.json.JsonMapper;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.messaging.Message;
|
||||
@@ -71,7 +74,13 @@ public class JsonMessageConverter extends AbstractMessageConverter {
|
||||
if (targetClass.isInstance(message.getPayload())) {
|
||||
return message.getPayload();
|
||||
}
|
||||
Object result = jsonMapper.fromJson(message.getPayload(), targetClass);
|
||||
Object result = null;
|
||||
if (conversionHint == null) {
|
||||
result = jsonMapper.fromJson(message.getPayload(), targetClass);
|
||||
}
|
||||
else if (conversionHint instanceof ParameterizedType) {
|
||||
result = jsonMapper.fromJson(message.getPayload(), (Type) conversionHint);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user