GH-640 Fix NPE with non-Message json conversion

Resolves #640

Added test
This commit is contained in:
Oleg Zhurakousky
2021-02-05 16:03:45 +01:00
parent 315ed4612d
commit c4dfffe0ba
2 changed files with 27 additions and 3 deletions

View File

@@ -1001,10 +1001,10 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
&& SimpleFunctionRegistry.this.conversionService.canConvert(input.getClass(), rawInputType)) {
convertedInput = SimpleFunctionRegistry.this.conversionService.convert(input, rawInputType);
}
if (convertedInput == null && input.getClass().isAssignableFrom(rawInputType)) {
convertedInput = input;
if (convertedInput == null && logger.isDebugEnabled()) {
logger.debug("Failed to convert input '" + input + "' to type " + inputType + ". Will use it as is.");
}
return convertedInput;
return convertedInput == null ? input : convertedInput;
}
/*

View File

@@ -86,6 +86,21 @@ public class SimpleFunctionRegistryTests {
this.conversionService = new DefaultConversionService();
}
@Test
public void testSCF640() {
Echo function = new Echo();
FunctionRegistration<Echo> registration = new FunctionRegistration<>(
function, "echo").type(FunctionType.of(Echo.class));
SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter,
new JacksonMapper(new ObjectMapper()));
catalog.register(registration);
FunctionInvocationWrapper lookedUpFunction = catalog.lookup("echo");
Object result = lookedUpFunction.apply("{\"HELLO\":\"WORLD\"}");
assertThat(result).isNotInstanceOf(Message.class);
assertThat(result).isEqualTo("{\"HELLO\":\"WORLD\"}");
}
@SuppressWarnings("unchecked")
@Test
public void testSCF588() {
@@ -468,6 +483,15 @@ public class SimpleFunctionRegistryTests {
}
private static class Echo implements Function<Object, Object> {
@Override
public Object apply(Object t) {
return t;
}
}
private static class UpperCaseMessage
implements Function<Message<String>, Message<String>> {