GH-602 Ensure collections with converted items are not converted again

Resolves #602
This commit is contained in:
Oleg Zhurakousky
2020-11-05 17:00:09 +01:00
parent dd0f70bc8e
commit 9b325ce7e6
6 changed files with 158 additions and 22 deletions

View File

@@ -61,6 +61,7 @@ import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.converter.CompositeMessageConverter;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
@@ -356,7 +357,7 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
* @return the type of the item if wrapped otherwise the provided type.
*/
public Type getItemType(Type type) {
if (FunctionTypeUtils.isPublisher(type) || FunctionTypeUtils.isMessage(type)) {
if (FunctionTypeUtils.isPublisher(type) || FunctionTypeUtils.isMessage(type) || FunctionTypeUtils.isTypeCollection(type)) {
type = FunctionTypeUtils.getGenericType(type);
}
return type;
@@ -784,19 +785,6 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
? input
: new OriginalMessageHolder(((Message) input).getPayload(), (Message<?>) input);
}
// else if (FunctionTypeUtils.isMultipleArgumentType(type)) {
// Type[] inputTypes = ((ParameterizedType) type).getActualTypeArguments();
// Object[] multipleValueArguments = this.parseMultipleValueArguments(input, inputTypes.length);
// Object[] convertedInputs = new Object[inputTypes.length];
// for (int i = 0; i < multipleValueArguments.length; i++) {
// Object cInput = this.convertInputIfNecessary(multipleValueArguments[i], inputTypes[i]);
// convertedInputs[i] = cInput;
// }
// convertedInput = Tuples.fromArray(convertedInputs);
// }
// else if (input instanceof Publisher) {
// convertedInput = this.convertInputPublisherIfNecessary((Publisher) input, type);
// }
else if (input instanceof Message) {
convertedInput = this.convertInputMessageIfNecessary((Message) input, type);
if (convertedInput == null) { // give ConversionService a chance
@@ -949,6 +937,14 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
if (message.getPayload() instanceof Optional) {
return message;
}
if (message.getPayload() instanceof Collection<?>) {
Type itemType = FunctionTypeUtils.getImmediateGenericType(type, 0);
Type collectionType = CollectionUtils.findCommonElementType((Collection<?>) message.getPayload());
if (collectionType == itemType) {
return message.getPayload();
}
}
//if (message.getPayload().getClass().isAss) {
Object convertedInput = message;
type = this.extractActualValueTypeIfNecessary(type);
@@ -973,7 +969,6 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
convertedInput = MessageBuilder.withPayload(convertedInput).copyHeaders(message.getHeaders()).build();
}
}
// convertedInput = convertedInput == null ? message.getPayload() : convertedInput;
return convertedInput;
}

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.function.context.config;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import org.springframework.cloud.function.json.JsonMapper;
import org.springframework.lang.Nullable;
@@ -73,12 +74,9 @@ public class JsonMessageConverter extends AbstractMessageConverter {
@Override
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) {
if (targetClass.isInstance(message.getPayload())) {
if (targetClass.isInstance(message.getPayload()) && !(message.getPayload() instanceof Collection<?>)) {
return message.getPayload();
}
Type convertToType = conversionHint == null ? targetClass : (Type) conversionHint;
try {
return this.jsonMapper.fromJson(message.getPayload(), convertToType);

View File

@@ -46,7 +46,7 @@ public class GsonMapper extends JsonMapper {
}
@Override
public <T> T fromJson(Object json, Type type) {
protected <T> T doFromJson(Object json, Type type) {
T convertedValue = null;
if (json instanceof byte[]) {
convertedValue = this.gson.fromJson(new String(((byte[]) json), StandardCharsets.UTF_8), type);

View File

@@ -42,7 +42,7 @@ public class JacksonMapper extends JsonMapper {
}
@Override
public <T> T fromJson(Object json, Type type) {
protected <T> T doFromJson(Object json, Type type) {
T convertedValue = null;
JavaType constructType = TypeFactory.defaultInstance().constructType(type);

View File

@@ -19,11 +19,14 @@ package org.springframework.cloud.function.json;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
import org.springframework.core.ResolvableType;
/**
@@ -60,7 +63,25 @@ public abstract class JsonMapper {
@Deprecated
abstract <T> T toObject(String json, Type type);
public abstract <T> T fromJson(Object json, Type type);
@SuppressWarnings("unchecked")
public <T> T fromJson(Object json, Type type) {
if (json instanceof Collection<?>) {
Collection<?> inputs = (Collection<?>) json;
Type itemType = FunctionTypeUtils.getImmediateGenericType(type, 0);
Collection<?> results = FunctionTypeUtils.getRawType(type).isAssignableFrom(List.class)
? new ArrayList<>()
: new HashSet<>();
for (Object input : inputs) {
results.add(this.doFromJson(input, itemType));
}
return (T) results;
}
else {
return this.doFromJson(json, type);
}
}
protected abstract <T> T doFromJson(Object json, Type type);
public byte[] toJson(Object value) {
byte[] result = null;