GH-531 Fail function registration for incompatible types

Given that we now can auto-discover function type from provided target object, this fix will fail function registration if provided type is not compatible (assignableFrom. . .)

Resolves #531
This commit is contained in:
Oleg Zhurakousky
2020-06-10 18:28:28 +02:00
parent dfa02750c1
commit 6bfc614f9f
2 changed files with 73 additions and 2 deletions

View File

@@ -27,10 +27,13 @@ import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import net.jodah.typetools.TypeResolver;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
import org.springframework.cloud.function.context.config.RoutingFunction;
import org.springframework.cloud.function.core.FluxConsumer;
import org.springframework.cloud.function.core.FluxFunction;
@@ -43,6 +46,8 @@ import org.springframework.cloud.function.core.MonoToFluxFunction;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
/**
* @param <T> target type
* @author Dave Syer
@@ -114,11 +119,21 @@ public class FunctionRegistration<T> implements BeanNameAware {
}
public FunctionRegistration<T> type(Type type) {
this.type = FunctionType.of(type);
return this;
return type(FunctionType.of(type));
}
public FunctionRegistration<T> type(FunctionType type) {
Type t = FunctionTypeUtils.discoverFunctionTypeFromFunctionalObject(this.target);
FunctionType discoveredFunctionType = FunctionType.of(t);
Class<?> inputType = TypeResolver.resolveRawClass(discoveredFunctionType.getInputType(), null);
Class<?> outputType = TypeResolver.resolveRawClass(discoveredFunctionType.getOutputType(), null);
if (!(inputType.isAssignableFrom(TypeResolver.resolveRawClass(type.getInputType(), null))
&& outputType.isAssignableFrom(TypeResolver.resolveRawClass(type.getOutputType(), null)))) {
throw new IllegalStateException("Discovered function type does not match provided function type. Discovered: "
+ discoveredFunctionType + "; Provided: " + type);
}
this.type = type;
return this;
}