GH-608 Fix support for Message payload conversion

Resolves #608
This commit is contained in:
Oleg Zhurakousky
2021-04-09 12:24:53 +02:00
parent e9441b50d3
commit 8b9051dfeb
2 changed files with 51 additions and 6 deletions

View File

@@ -821,12 +821,15 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
boolean convertWithHint = false;
Type hint = FunctionTypeUtils.getGenericType(type);
if (FunctionTypeUtils.isTypeCollection(type)) {
convertWithHint = true;
}
else if (!FunctionTypeUtils.isPublisher(type) && !rawType.equals(type)) {
convertWithHint = true;
}
convertWithHint = this.useConversionHint(type, rawType);
// if (FunctionTypeUtils.isTypeCollection(type)) {
// convertWithHint = true;
// }
// else if (!FunctionTypeUtils.isPublisher(type) && !rawType.equals(type) && !FunctionTypeUtils.isMessage(type)) {
// convertWithHint = true;
// }
convertedValue = convertWithHint
? this.fromMessage((Message<?>) value, (Class<?>) rawType, hint)
@@ -864,6 +867,16 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
return convertedValue;
}
private boolean useConversionHint(Type type, Type rawType) {
if (FunctionTypeUtils.isTypeCollection(type)) {
return true;
}
else if (!FunctionTypeUtils.isPublisher(type) && !rawType.equals(type) && !FunctionTypeUtils.isMessage(type)) {
return true;
}
return false;
}
private Object convertNonMessageInputIfNecessary(Type inputType, Object input) {
Object convertedInput = input;
Class<?> rawInputType = FunctionTypeUtils.isReactive(inputType) || FunctionTypeUtils.isMessage(inputType)

View File

@@ -546,6 +546,23 @@ public class BeanFactoryAwareFunctionRegistryTests {
assertThat(((Person) config.consumerInputRef.get()).getName()).isEqualTo("Ricky");
}
@Test
public void testGH_608_B() {
ApplicationContext context = new SpringApplicationBuilder(MessageFunctionConfiguration.class)
.run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.main.lazy-initialization=true");
FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
String productJson = "{\"name\":\"bike\"}";
FunctionInvocationWrapper function = catalog.lookup("echo", "application/json");
Message<byte[]> result = (Message<byte[]>) function.apply(productJson);
assertThat(productJson).isEqualTo(new String(result.getPayload()));
function = catalog.lookup("echoFlux", "application/json");
result = ((Flux<Message<byte[]>>) function.apply("{\"name\":\"bike\"}")).blockFirst();
assertThat(productJson).isEqualTo(new String(result.getPayload()));
}
@Test
public void testGH_609() {
FunctionCatalog catalog = this.configureCatalog(SampleFunctionConfiguration.class);
@@ -803,6 +820,21 @@ public class BeanFactoryAwareFunctionRegistryTests {
}
}
@EnableAutoConfiguration
@Configuration
protected static class MessageFunctionConfiguration {
@Bean
public Function<Message<Product>, Message<Product>> echo() {
return x -> x;
}
@Bean
public Function<Flux<Message<Product>>, Flux<Message<Product>>> echoFlux() {
return x -> x;
}
}
@EnableAutoConfiguration
@Configuration
protected static class SampleFunctionConfiguration {