Add skip conversion header logic

This commit is contained in:
Oleg Zhurakousky
2019-09-23 11:15:25 -04:00
parent 6538c3da4c
commit e490eda5ff
2 changed files with 14 additions and 8 deletions

View File

@@ -32,6 +32,11 @@ public class FunctionProperties {
*/
public final static String PREFIX = "spring.cloud.function";
/**
* Name of he header to be used to instruct function catalog to skip type conversion.
*/
public final static String SKIP_CONVERSION_HEADER = "skip-type-conversion";
/**
* Definition of the function to be used. This could be function name (e.g., 'myFunction')
* or function composition definition (e.g., 'myFunction|yourFunction')

View File

@@ -50,6 +50,7 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.FunctionProperties;
import org.springframework.cloud.function.context.FunctionRegistration;
import org.springframework.cloud.function.context.FunctionRegistry;
import org.springframework.cloud.function.context.FunctionType;
@@ -156,8 +157,8 @@ public class BeanFactoryAwareFunctionRegistry
@Override
public FunctionRegistration<?> getRegistration(Object function) {
FunctionRegistration<?> registration = this.registrationsByFunction.get(function);
// need to do this due to the deployer not wrapping the actual target into FunctionInvocationWrapper
// hence the lookup would need to be made by the actual target
// // need to do this due to the deployer not wrapping the actual target into FunctionInvocationWrapper
// // hence the lookup would need to be made by the actual target
if (registration == null && function instanceof FunctionInvocationWrapper) {
function = ((FunctionInvocationWrapper) function).target;
}
@@ -165,13 +166,10 @@ public class BeanFactoryAwareFunctionRegistry
}
private Object locateFunction(String name) {
Object function = null;
if (this.applicationContext.containsBean(name)) {
Object function = this.registrationsByName.get(name);
if (function == null && this.applicationContext.containsBean(name)) {
function = this.applicationContext.getBean(name);
}
if (function == null) {
function = this.registrationsByName.get(name);
}
if (function != null && this.notFunction(function.getClass())
&& this.applicationContext.containsBean(name + FunctionRegistration.REGISTRATION_NAME_SUFFIX)) { // e.g., Kotlin lambdas
@@ -186,7 +184,6 @@ public class BeanFactoryAwareFunctionRegistry
&& !Consumer.class.isAssignableFrom(functionClass);
}
private Type discoverFunctionType(Object function, String... names) {
boolean beanDefinitionExists = false;
for (int i = 0; i < names.length && !beanDefinitionExists; i++) {
@@ -674,6 +671,10 @@ public class BeanFactoryAwareFunctionRegistry
}
private boolean messageNeedsConversion(Type rawType, Message<?> message) {
String skipConversion = message.getHeaders().get(FunctionProperties.SKIP_CONVERSION_HEADER, String.class);
if (StringUtils.hasText(skipConversion) && Boolean.valueOf(skipConversion)) {
return false;
}
return rawType instanceof Class<?>
&& !(message.getPayload() instanceof Optional)
&& !(message.getPayload().getClass().isAssignableFrom(((Class<?>) rawType)));