GH-662 Fix support for reactive functions in AWS
This commit also includes other minor fixes around CustomRuntime which was getting in the way of this specific issue Added lookup for _HANDLER env variable Added few tests (will need more) Added support for Iterable for reactive functions Resolves #662
This commit is contained in:
@@ -664,11 +664,27 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
|
||||
/*
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private Object fluxifyInputIfNecessary(Object input) {
|
||||
if (!(input instanceof Publisher) && this.isTypePublisher(this.inputType) && !FunctionTypeUtils.isMultipleArgumentType(this.inputType)) {
|
||||
return input == null
|
||||
? FunctionTypeUtils.isMono(this.inputType) ? Mono.empty() : Flux.empty()
|
||||
: FunctionTypeUtils.isMono(this.inputType) ? Mono.just(input) : Flux.just(input);
|
||||
if (input == null) {
|
||||
input = FunctionTypeUtils.isMono(this.inputType) ? Mono.empty() : Flux.empty();
|
||||
}
|
||||
else if (input instanceof Message && ((Message) input).getPayload() instanceof Iterable) {
|
||||
input = FunctionTypeUtils.isMono(this.inputType) ? Mono.just(input) : Flux.just(input).flatMap(v -> {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Creating Flux from Iterable: " + ((Message) v).getPayload());
|
||||
}
|
||||
return Flux.fromIterable((Iterable) ((Message) v).getPayload());
|
||||
});
|
||||
}
|
||||
else if (input instanceof Iterable) {
|
||||
input = FunctionTypeUtils.isMono(this.inputType) ? Mono.just(input) : Flux.fromIterable((Iterable) input);
|
||||
|
||||
}
|
||||
else {
|
||||
input = FunctionTypeUtils.isMono(this.inputType) ? Mono.just(input) : Flux.just(input);
|
||||
}
|
||||
}
|
||||
return input;
|
||||
}
|
||||
@@ -1050,6 +1066,9 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
|
||||
}
|
||||
if (message.getPayload() instanceof Collection<?>) {
|
||||
Type itemType = FunctionTypeUtils.getImmediateGenericType(type, 0);
|
||||
if (itemType == null) {
|
||||
itemType = type;
|
||||
}
|
||||
Type collectionType = CollectionUtils.findCommonElementType((Collection<?>) message.getPayload());
|
||||
if (collectionType == itemType) {
|
||||
return message.getPayload();
|
||||
|
||||
@@ -20,6 +20,7 @@ package org.springframework.cloud.function.context.catalog;
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
@@ -593,6 +594,24 @@ public class BeanFactoryAwareFunctionRegistryTests {
|
||||
assertThat(resultList).isEmpty();
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@Test
|
||||
public void testArrayPayloadOnFluxFunction() throws Exception {
|
||||
FunctionCatalog catalog = this.configureCatalog(SampleFunctionConfiguration.class);
|
||||
FunctionInvocationWrapper lmFunction = catalog.lookup("uppercaseFlux", "application/json");
|
||||
lmFunction.setSkipOutputConversion(true);
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("Ricky");
|
||||
list.add("Julien");
|
||||
list.add("Bubbles");
|
||||
Publisher p = (Publisher) lmFunction.apply(MessageBuilder.withPayload(list).setHeader(MessageHeaders.CONTENT_TYPE, "application/json").build());
|
||||
List<Object> result = new ArrayList<>();
|
||||
for (Object value : Flux.from(p).toIterable()) {
|
||||
result.add(value);
|
||||
}
|
||||
assertThat(result.size()).isEqualTo(3);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@EnableAutoConfiguration
|
||||
|
||||
Reference in New Issue
Block a user