GH-635 Fix support for returning empty Collections

Resolves #635
This commit is contained in:
Oleg Zhurakousky
2021-01-22 16:43:55 +01:00
parent 3d4bd14926
commit fbc2ca815a
2 changed files with 43 additions and 3 deletions

View File

@@ -900,7 +900,7 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
if (ObjectUtils.isArray(payload)) {
payload = CollectionUtils.arrayToList(payload);
}
if (payload instanceof Collection
if (payload instanceof Collection && !CollectionUtils.isEmpty((Collection<?>) payload)
&& Message.class.isAssignableFrom(CollectionUtils.findCommonElementType((Collection<?>) payload))) {
return true;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 the original author or authors.
* Copyright 2019-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.io.Serializable;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -47,6 +48,7 @@ import org.springframework.cloud.function.context.FunctionRegistration;
import org.springframework.cloud.function.context.FunctionRegistry;
import org.springframework.cloud.function.context.FunctionType;
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
import org.springframework.cloud.function.json.JsonMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -73,12 +75,14 @@ import static org.junit.jupiter.api.Assertions.fail;
*/
public class BeanFactoryAwareFunctionRegistryTests {
private ApplicationContext context;
private FunctionCatalog configureCatalog() {
return this.configureCatalog(SampleFunctionConfiguration.class);
}
private FunctionCatalog configureCatalog(Class<?>... configClass) {
ApplicationContext context = new SpringApplicationBuilder(configClass)
this.context = new SpringApplicationBuilder(configClass)
.run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.main.lazy-initialization=true");
FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
@@ -550,6 +554,28 @@ public class BeanFactoryAwareFunctionRegistryTests {
assertThat(result.block()).isEqualTo("hello");
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testGH_635() throws Exception {
FunctionCatalog catalog = this.configureCatalog(SCF_GH_635ConfigurationAsFunction.class);
Function lmFunction = catalog.lookup("emptyMessageList", "application/json");
List<Message<?>> emptyListOfMessages = (List<Message<?>>) lmFunction.apply(MessageBuilder.withPayload("hello").build());
assertThat(emptyListOfMessages).isEmpty();
emptyListOfMessages = (List<Message<?>>) lmFunction.apply("hello");
assertThat(emptyListOfMessages).isEmpty();
JsonMapper mapper = this.context.getBean(JsonMapper.class);
Function lsFunction = catalog.lookup("emptyStringList", "application/json");
Message<byte[]> emptyListOfString = (Message<byte[]>) lsFunction.apply(MessageBuilder.withPayload("hello").build());
List resultList = mapper.fromJson(emptyListOfString.getPayload(), List.class);
assertThat(resultList).isEmpty();
emptyListOfString = (Message<byte[]>) lsFunction.apply("hello");
resultList = mapper.fromJson(emptyListOfString.getPayload(), List.class);
assertThat(resultList).isEmpty();
}
@EnableAutoConfiguration
public static class PojoToMessageFunctionCompositionConfiguration {
@@ -967,6 +993,20 @@ public class BeanFactoryAwareFunctionRegistryTests {
}
}
@EnableAutoConfiguration
public static class SCF_GH_635ConfigurationAsFunction {
@Bean
public Function<String, List<Message<?>>> emptyMessageList() {
return input -> Collections.emptyList();
}
@Bean
public Function<String, List<String>> emptyStringList() {
return input -> Collections.emptyList();
}
}
public static class Person {
private String name;
private int id;