Deprecate and remove all usages of FunctionInspector

This commit is contained in:
Oleg Zhurakousky
2020-10-19 15:30:11 +02:00
parent 3f1315c523
commit 818cda144c
12 changed files with 268 additions and 319 deletions

View File

@@ -32,6 +32,7 @@ import org.springframework.messaging.support.GenericMessage;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
/** /**
* @param <E> payload type * @param <E> payload type
* @param <O> response type * @param <O> response type

View File

@@ -27,6 +27,8 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer; import org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer;
import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
/** /**
* @param <I> input type * @param <I> input type
@@ -129,7 +131,7 @@ public class AzureSpringBootRequestHandler<I, O> extends AbstractSpringFunctionA
} }
if (getInspector() != null) { if (getInspector() != null) {
return Collection.class return Collection.class
.isAssignableFrom(getInspector().getInputType(function)); .isAssignableFrom(((FunctionInvocationWrapper) function).getRawInputType());
} }
return ((Collection<?>) input).size() <= 1; return ((Collection<?>) input).size() <= 1;
} }
@@ -139,8 +141,8 @@ public class AzureSpringBootRequestHandler<I, O> extends AbstractSpringFunctionA
return true; return true;
} }
if (getInspector() != null) { if (getInspector() != null) {
return Collection.class Class<?> outputType = FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(((FunctionInvocationWrapper) function).getOutputType()));
.isAssignableFrom(getInspector().getOutputType(function)); return Collection.class.isAssignableFrom(outputType);
} }
return ((Collection<?>) output).size() <= 1; return ((Collection<?>) output).size() <= 1;
} }

View File

@@ -36,6 +36,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType; import org.springframework.boot.WebApplicationType;
import org.springframework.cloud.function.context.catalog.FunctionInspector; import org.springframework.cloud.function.context.catalog.FunctionInspector;
import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
import org.springframework.cloud.function.context.config.FunctionContextUtils; import org.springframework.cloud.function.context.config.FunctionContextUtils;
import org.springframework.cloud.function.context.config.RoutingFunction; import org.springframework.cloud.function.context.config.RoutingFunction;
import org.springframework.cloud.function.json.JsonMapper; import org.springframework.cloud.function.json.JsonMapper;
@@ -138,15 +140,18 @@ public abstract class AbstractSpringFunctionAdapterInitializer<C> implements Clo
} }
} }
@Deprecated
protected FunctionInspector getInspector() { protected FunctionInspector getInspector() {
return inspector; return inspector;
} }
protected Class<?> getInputType() { protected Class<?> getInputType() {
if (this.inspector != null) {
return this.inspector.getInputType(function()); Object func = function();
if (func != null && func instanceof FunctionInvocationWrapper) {
return FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(((FunctionInvocationWrapper) func).getInputType()));
} }
else if (functionRegistration != null) { if (functionRegistration != null) {
return functionRegistration.getType().getInputType(); return functionRegistration.getType().getInputType();
} }
return Object.class; return Object.class;
@@ -237,7 +242,7 @@ public abstract class AbstractSpringFunctionAdapterInitializer<C> implements Clo
} }
if (getInspector() != null) { if (getInspector() != null) {
return Collection.class return Collection.class
.isAssignableFrom(getInspector().getInputType(function)); .isAssignableFrom(((FunctionInvocationWrapper) function).getRawInputType());
} }
return ((Collection<?>) input).size() <= 1; return ((Collection<?>) input).size() <= 1;
} }
@@ -247,8 +252,8 @@ public abstract class AbstractSpringFunctionAdapterInitializer<C> implements Clo
return true; return true;
} }
if (getInspector() != null) { if (getInspector() != null) {
return Collection.class Class<?> outputType = FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(((FunctionInvocationWrapper) function).getOutputType()));
.isAssignableFrom(getInspector().getOutputType(function)); return Collection.class.isAssignableFrom(outputType);
} }
return ((Collection<?>) output).size() <= 1; return ((Collection<?>) output).size() <= 1;
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -31,7 +31,10 @@ import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry
/** /**
* @author Dave Syer * @author Dave Syer
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
*
* @deprecated since 3.1 no longer used by the framework
*/ */
@Deprecated
public interface FunctionInspector { public interface FunctionInspector {
FunctionRegistration<?> getRegistration(Object function); FunctionRegistration<?> getRegistration(Object function);
@@ -49,6 +52,11 @@ public interface FunctionInspector {
return ((FunctionInvocationWrapper) function).isInputTypeMessage(); return ((FunctionInvocationWrapper) function).isInputTypeMessage();
} }
/**
*
* @deprecated since 3.1 no longer used by the framework
*/
@Deprecated
default Class<?> getInputType(Object function) { default Class<?> getInputType(Object function) {
if (function == null) { if (function == null) {
return Object.class; return Object.class;
@@ -69,6 +77,11 @@ public interface FunctionInspector {
return inputType; return inputType;
} }
/**
*
* @deprecated since 3.1 no longer used by the framework
*/
@Deprecated
default Class<?> getOutputType(Object function) { default Class<?> getOutputType(Object function) {
if (function == null) { if (function == null) {
return Object.class; return Object.class;
@@ -89,6 +102,11 @@ public interface FunctionInspector {
return outputType; return outputType;
} }
/**
*
* @deprecated since 3.1 no longer used by the framework
*/
@Deprecated
default Class<?> getInputWrapper(Object function) { default Class<?> getInputWrapper(Object function) {
Class c = function == null ? Object.class : TypeResolver.resolveRawClass(((FunctionInvocationWrapper) function).getInputType(), null); Class c = function == null ? Object.class : TypeResolver.resolveRawClass(((FunctionInvocationWrapper) function).getInputType(), null);
if (Flux.class.isAssignableFrom(c)) { if (Flux.class.isAssignableFrom(c)) {
@@ -102,6 +120,11 @@ public interface FunctionInspector {
} }
} }
/**
*
* @deprecated since 3.1 no longer used by the framework
*/
@Deprecated
default Class<?> getOutputWrapper(Object function) { default Class<?> getOutputWrapper(Object function) {
Class c = function == null ? Object.class : TypeResolver.resolveRawClass(((FunctionInvocationWrapper) function).getOutputType(), null); Class c = function == null ? Object.class : TypeResolver.resolveRawClass(((FunctionInvocationWrapper) function).getOutputType(), null);
if (Flux.class.isAssignableFrom(c)) { if (Flux.class.isAssignableFrom(c)) {
@@ -115,6 +138,11 @@ public interface FunctionInspector {
} }
} }
/**
*
* @deprecated since 3.1 no longer used by the framework
*/
@Deprecated
default String getName(Object function) { default String getName(Object function) {
if (function == null) { if (function == null) {
return null; return null;

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2019 the original author or authors. * Copyright 2019-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -31,6 +31,8 @@ import java.util.function.Supplier;
import java.util.stream.Stream; import java.util.stream.Stream;
import net.jodah.typetools.TypeResolver; import net.jodah.typetools.TypeResolver;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@@ -50,6 +52,8 @@ import org.springframework.util.ReflectionUtils;
*/ */
public final class FunctionTypeUtils { public final class FunctionTypeUtils {
private static Log logger = LogFactory.getLog(FunctionTypeUtils.class);
private FunctionTypeUtils() { private FunctionTypeUtils() {
} }
@@ -86,6 +90,10 @@ public final class FunctionTypeUtils {
return type; return type;
} }
public static Class<?> getRawType(Type type) {
return type != null ? TypeResolver.resolveRawClass(type, null) : null;
}
/** /**
* Will attempt to discover functional methods on the class. It's applicable for POJOs as well as * Will attempt to discover functional methods on the class. It's applicable for POJOs as well as
* functional classes in `java.util.function` package. For the later the names of the methods are * functional classes in `java.util.function` package. For the later the names of the methods are
@@ -188,41 +196,52 @@ public final class FunctionTypeUtils {
return outputCount; return outputCount;
} }
/**
* Returns input type of function type that represents Function or Consumer.
* @param functionType the Type of Function or Consumer
* @return the input type as {@link Type}
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static Type getInputType(Type functionType) { public static Type getInputType(Type functionType) {
if (isSupplier(functionType)) {
logger.debug("Supplier does not have input type, returning null as input type.");
return null;
}
assertSupportedTypes(functionType); assertSupportedTypes(functionType);
Type inputType;
if (functionType instanceof Class) { if (functionType instanceof Class) {
Class<?> functionClass = (Class<?>) functionType; functionType = Function.class.isAssignableFrom((Class<?>) functionType)
if (Function.class.isAssignableFrom(functionClass)) { ? TypeResolver.reify(Function.class, (Class<Function<?, ?>>) functionType)
functionType = TypeResolver.reify(Function.class, (Class<Function<?, ?>>) functionClass); : TypeResolver.reify(Consumer.class, (Class<Consumer<?>>) functionType);
}
else if (Consumer.class.isAssignableFrom(functionClass)) {
functionType = TypeResolver.reify(Consumer.class, (Class<Consumer<?>>) functionClass);
}
else {
return null;
}
} }
Type inputType = Object.class; inputType = functionType instanceof ParameterizedType
if ((isFunction(functionType) || isConsumer(functionType)) && functionType instanceof ParameterizedType) { ? ((ParameterizedType) functionType).getActualTypeArguments()[0]
inputType = ((ParameterizedType) functionType).getActualTypeArguments()[0]; : Object.class;
}
return inputType; return inputType;
} }
public static Type getOutputType(Type functionType, int index) { @SuppressWarnings("unchecked")
public static Type getOutputType(Type functionType) {
assertSupportedTypes(functionType); assertSupportedTypes(functionType);
if (isFunction(functionType)) { if (isConsumer(functionType)) {
return functionType instanceof ParameterizedType ? ((ParameterizedType) functionType).getActualTypeArguments()[1] : Object.class; logger.debug("Consumer does not have output type, returning null as output type.");
}
else if (isSupplier(functionType)) {
return functionType instanceof ParameterizedType ? ((ParameterizedType) functionType).getActualTypeArguments()[0] : Object.class;
}
else {
return null; return null;
} }
Type inputType;
if (functionType instanceof Class) {
functionType = Function.class.isAssignableFrom((Class<?>) functionType)
? TypeResolver.reify(Function.class, (Class<Function<?, ?>>) functionType)
: TypeResolver.reify(Function.class, (Class<Supplier<?>>) functionType);
}
inputType = functionType instanceof ParameterizedType
? (isSupplier(functionType) ? ((ParameterizedType) functionType).getActualTypeArguments()[0] : ((ParameterizedType) functionType).getActualTypeArguments()[1])
: Object.class;
return inputType;
} }
public static Type getImmediateGenericType(Type type, int index) { public static Type getImmediateGenericType(Type type, int index) {
@@ -238,8 +257,6 @@ public final class FunctionTypeUtils {
public static boolean isFlux(Type type) { public static boolean isFlux(Type type) {
return TypeResolver.resolveRawClass(type, null) == Flux.class; return TypeResolver.resolveRawClass(type, null) == Flux.class;
// type = extractReactiveType(type);
// return type.getTypeName().startsWith("reactor.core.publisher.Flux");
} }
public static boolean isMessage(Type type) { public static boolean isMessage(Type type) {
@@ -252,23 +269,13 @@ public final class FunctionTypeUtils {
return TypeResolver.resolveRawClass(type, null) == Message.class; return TypeResolver.resolveRawClass(type, null) == Message.class;
} }
/**
* Determines if input argument to a Function is an array.
* @param functionType the function type
* @return true if input type is an array, otherwise false
*/
public static boolean isInputArray(Type functionType) {
Type inputType = FunctionTypeUtils.getInputType(functionType);
return inputType instanceof GenericArrayType || inputType instanceof Class && ((Class<?>) inputType).isArray();
}
/** /**
* Determines if input argument to a Function is an array. * Determines if input argument to a Function is an array.
* @param functionType the function type * @param functionType the function type
* @return true if input type is an array, otherwise false * @return true if input type is an array, otherwise false
*/ */
public static boolean isOutputArray(Type functionType) { public static boolean isOutputArray(Type functionType) {
Type outputType = FunctionTypeUtils.getOutputType(functionType, 0); Type outputType = FunctionTypeUtils.getOutputType(functionType);
return outputType instanceof GenericArrayType || outputType instanceof Class && ((Class<?>) outputType).isArray(); return outputType instanceof GenericArrayType || outputType instanceof Class && ((Class<?>) outputType).isArray();
} }
@@ -296,7 +303,7 @@ public final class FunctionTypeUtils {
public static boolean isMono(Type type) { public static boolean isMono(Type type) {
type = extractReactiveType(type); type = extractReactiveType(type);
return type.getTypeName().startsWith("reactor.core.publisher.Mono"); return type == null ? false : type.getTypeName().startsWith("reactor.core.publisher.Mono");
} }
private static boolean isFunctional(Type type) { private static boolean isFunctional(Type type) {

View File

@@ -250,7 +250,7 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
private FunctionInvocationWrapper invocationWrapperInstance(String functionDefinition, Object target, Type functionType) { private FunctionInvocationWrapper invocationWrapperInstance(String functionDefinition, Object target, Type functionType) {
return invocationWrapperInstance(functionDefinition, target, return invocationWrapperInstance(functionDefinition, target,
FunctionTypeUtils.isSupplier(functionType) ? null : FunctionTypeUtils.getInputType(functionType), FunctionTypeUtils.isSupplier(functionType) ? null : FunctionTypeUtils.getInputType(functionType),
FunctionTypeUtils.getOutputType(functionType, 0)); FunctionTypeUtils.getOutputType(functionType));
} }
/** /**
@@ -325,11 +325,11 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
} }
public Class<?> getRawOutputType() { public Class<?> getRawOutputType() {
return TypeResolver.resolveRawClass(this.outputType, null); return this.outputType == null ? null : TypeResolver.resolveRawClass(this.outputType, null);
} }
public Class<?> getRawInputType() { public Class<?> getRawInputType() {
return TypeResolver.resolveRawClass(this.inputType, null); return this.inputType == null ? null : TypeResolver.resolveRawClass(this.inputType, null);
} }
/** /**

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.cloud.function.context.config; package org.springframework.cloud.function.context.config;
import java.lang.reflect.Type;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.ArrayList; import java.util.ArrayList;
@@ -47,7 +48,7 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.function.context.FunctionCatalog; import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.FunctionRegistration; import org.springframework.cloud.function.context.FunctionRegistration;
import org.springframework.cloud.function.context.FunctionRegistry; import org.springframework.cloud.function.context.FunctionRegistry;
import org.springframework.cloud.function.context.catalog.FunctionInspector; import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper; import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
import org.springframework.cloud.function.inject.FooConfiguration; import org.springframework.cloud.function.inject.FooConfiguration;
import org.springframework.cloud.function.scan.ScannedFunction; import org.springframework.cloud.function.scan.ScannedFunction;
@@ -77,24 +78,15 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class ContextFunctionCatalogAutoConfigurationTests { public class ContextFunctionCatalogAutoConfigurationTests {
private static String value;
private ConfigurableApplicationContext context; private ConfigurableApplicationContext context;
private FunctionCatalog catalog; private FunctionCatalog catalog;
private FunctionInspector inspector;
public static void set(Object value) {
ContextFunctionCatalogAutoConfigurationTests.value = value.toString();
}
@AfterEach @AfterEach
public void close() { public void close() {
if (this.context != null) { if (this.context != null) {
this.context.close(); this.context.close();
} }
ContextFunctionCatalogAutoConfigurationTests.value = null;
} }
@Test @Test
@@ -112,11 +104,9 @@ public class ContextFunctionCatalogAutoConfigurationTests {
assertThat(f.apply(Flux.just("hello")).blockFirst()) assertThat(f.apply(Flux.just("hello")).blockFirst())
.isEqualTo("HELLOfunction2function3"); .isEqualTo("HELLOfunction2function3");
assertThat(this.context.getBean("supplierFoo")).isInstanceOf(Supplier.class); assertThat(this.context.getBean("supplierFoo")).isInstanceOf(Supplier.class);
// assertThat((Supplier<?>) this.catalog.lookup(Supplier.class, "supplierFoo")) assertThat((Supplier<?>) this.catalog.lookup(Supplier.class, "supplierFoo"))
// .isInstanceOf(Supplier.class); .isInstanceOf(Supplier.class);
// assertThat(this.context.getBean("supplier_Foo")).isInstanceOf(Supplier.class); assertThat(this.context.getBean("supplier_Foo")).isInstanceOf(Supplier.class);
// assertThat((Supplier<?>) this.catalog.lookup(Supplier.class, "supplier_Foo"))
// .isInstanceOf(Supplier.class);
} }
@Test @Test
@@ -128,29 +118,23 @@ public class ContextFunctionCatalogAutoConfigurationTests {
.isInstanceOf(Function.class); .isInstanceOf(Function.class);
assertThat((Supplier<?>) this.catalog.lookup(Supplier.class, "foos")) assertThat((Supplier<?>) this.catalog.lookup(Supplier.class, "foos"))
.isInstanceOf(Supplier.class); .isInstanceOf(Supplier.class);
assertThat( Class<?> inputType = ((FunctionInvocationWrapper) this.catalog.lookup(Function.class, "foos")).getRawInputType();
this.inspector.getInputType(this.catalog.lookup(Function.class, "foos"))) assertThat(inputType).isEqualTo(String.class);
.isEqualTo(String.class); FunctionInvocationWrapper function = this.catalog.lookup("foos");
assertThat( Type outputType = function.getOutputType();
this.inspector.getOutputType(this.catalog.lookup(Supplier.class, "foos"))) assertThat((Class<?>) FunctionTypeUtils.getGenericType(outputType)).isEqualTo(Foo.class);
.isEqualTo(Foo.class);
} }
@Test @Test
public void configurationFunction() { public void configurationFunction() {
create(FunctionConfiguration.class); create(FunctionConfiguration.class);
assertThat(this.context.getBean("foos")).isInstanceOf(Function.class); assertThat(this.context.getBean("foos")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "foos")) FunctionInvocationWrapper function = this.catalog.lookup(Function.class, "foos");
.isInstanceOf(Function.class); assertThat(function).isInstanceOf(Function.class);
assertThat( Type inputType = function.getInputType();
this.inspector.getInputType(this.catalog.lookup(Function.class, "foos"))) assertThat(FunctionTypeUtils.getGenericType(inputType)).isEqualTo(String.class);
.isEqualTo(String.class); Type outputType = function.getOutputType();
assertThat( assertThat((Class<?>) FunctionTypeUtils.getGenericType(outputType)).isEqualTo(Foo.class);
this.inspector.getOutputType(this.catalog.lookup(Function.class, "foos")))
.isEqualTo(Foo.class);
assertThat(this.inspector
.getInputWrapper(this.catalog.lookup(Function.class, "foos")))
.isEqualTo(Flux.class);
} }
@Test @Test
@@ -159,9 +143,8 @@ public class ContextFunctionCatalogAutoConfigurationTests {
assertThat(this.context.getBean("foos")).isInstanceOf(Function.class); assertThat(this.context.getBean("foos")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "foos")) assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "foos"))
.isInstanceOf(Function.class); .isInstanceOf(Function.class);
// assertThat( Class<?> inputType = ((FunctionInvocationWrapper) this.catalog.lookup(Function.class, "foos")).getRawInputType();
// this.inspector.getInputType(this.catalog.lookup(Function.class, "foos"))) assertThat(inputType).isEqualTo(String.class);
// .isEqualTo(String.class);
} }
@Test @Test
@@ -170,200 +153,145 @@ public class ContextFunctionCatalogAutoConfigurationTests {
assertThat(this.context.getBean("foos")).isInstanceOf(Function.class); assertThat(this.context.getBean("foos")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "foos")) assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "foos"))
.isInstanceOf(Function.class); .isInstanceOf(Function.class);
// assertThat( Class<?> inputType = ((FunctionInvocationWrapper) this.catalog.lookup(Function.class, "foos")).getRawInputType();
// this.inspector.getInputType(this.catalog.lookup(Function.class, "foos"))) assertThat(inputType).isEqualTo(String.class);
// .isEqualTo(String.class);
} }
@Test @Test
public void composedFunction() { public void composedFunction() {
create(MultipleConfiguration.class); create(MultipleConfiguration.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "foos,bars")) FunctionInvocationWrapper function = this.catalog.lookup(Function.class, "foos");
.isInstanceOf(Function.class); assertThat(function).isInstanceOf(Function.class);
// assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "names,foos"))
// .isNull(); function = this.catalog.lookup(Function.class, "foos,bars");
// assertThat(this.inspector Class<?> inputType = function.getRawInputType();
// .getInputType(this.catalog.lookup(Function.class, "foos,bars"))) assertThat(inputType).isAssignableFrom(String.class);
// .isAssignableFrom(String.class); Class<?> outputType = function.getRawOutputType();
// assertThat(this.inspector assertThat(outputType).isAssignableFrom(Bar.class);
// .getOutputType(this.catalog.lookup(Function.class, "foos,bars")))
// .isAssignableFrom(Bar.class);
} }
@Test @Test
public void composedSupplier() { public void composedSupplier() {
create(MultipleConfiguration.class); create(MultipleConfiguration.class);
assertThat((Supplier<?>) this.catalog.lookup(Supplier.class, "names,foos")) FunctionInvocationWrapper function = this.catalog.lookup("names,foos");
.isInstanceOf(Supplier.class); assertThat(function).isInstanceOf(Supplier.class);
// assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "names,foos")) assertThat(function.getRawOutputType()).isAssignableFrom(Foo.class);
// .isNull(); assertThat(function.getRawInputType()).isNull();
// assertThat(this.inspector
// .getOutputType(this.catalog.lookup(Supplier.class, "names,foos")))
// .isAssignableFrom(Foo.class);
// The input type is the same as the input type of the first element in the chain
// assertThat(this.inspector
// .getInputType(this.catalog.lookup(Supplier.class, "names,foos")))
// .isAssignableFrom(Void.class);
} }
@Test @Test
public void composedConsumer() { public void composedConsumer() {
create(MultipleConfiguration.class); create(MultipleConfiguration.class);
assertThat((Consumer<?>) this.catalog.lookup(Consumer.class, "foos,print")) FunctionInvocationWrapper function = this.catalog.lookup("foos,print");
.isInstanceOf(Consumer.class); assertThat(function).isInstanceOf(Consumer.class);
// .isNull(); assertThat(function).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "foos,print")) assertThat(function.getRawInputType()).isAssignableFrom(String.class);
.isInstanceOf(Function.class); assertThat(function.getRawOutputType()).isNull();
// assertThat(this.inspector
// .getInputType(this.catalog.lookup(Function.class, "foos,print")))
// .isAssignableFrom(String.class);
// // The output type is the same as the output type of the last element in the chain
// assertThat(this.inspector
// .getOutputType(this.catalog.lookup(Function.class, "foos,print")))
// .isAssignableFrom(Void.class);
} }
@Test @Test
public void genericFunction() { public void genericFunction() {
create(GenericConfiguration.class); create(GenericConfiguration.class);
FunctionInvocationWrapper function = this.catalog.lookup("function");
assertThat(this.context.getBean("function")).isInstanceOf(Function.class); assertThat(this.context.getBean("function")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")) assertThat(function).isInstanceOf(Function.class);
.isInstanceOf(Function.class); assertThat(function.getRawInputType()).isAssignableFrom(Map.class);
// assertThat(this.inspector
// .getInputType(this.catalog.lookup(Function.class, "function")))
// .isAssignableFrom(Map.class);
// assertThat(this.inspector
// .getInputWrapper(this.catalog.lookup(Function.class, "function")))
// .isAssignableFrom(Map.class);
} }
@Test @Test
public void fluxMessageFunction() { public void fluxMessageFunction() {
create(FluxMessageConfiguration.class); create(FluxMessageConfiguration.class);
FunctionInvocationWrapper function = this.catalog.lookup("function");
assertThat(this.context.getBean("function")).isInstanceOf(Function.class); assertThat(this.context.getBean("function")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")) assertThat(function).isInstanceOf(Function.class);
.isInstanceOf(Function.class); assertThat(function.isInputTypeMessage()).isTrue();
// assertThat(
// this.inspector.isMessage(this.catalog.lookup(Function.class, "function"))) Type inputType = function.getInputType();
// .isTrue();
// assertThat(this.inspector assertThat((Class<?>) FunctionTypeUtils.getGenericType(inputType)).isAssignableFrom(String.class);
// .getInputType(this.catalog.lookup(Function.class, "function")))
// .isAssignableFrom(String.class);
// assertThat(this.inspector
// .getInputWrapper(this.catalog.lookup(Function.class, "function")))
// .isAssignableFrom(Flux.class);
} }
@Test @Test
public void publisherMessageFunction() { public void publisherMessageFunction() {
create(PublisherMessageConfiguration.class); create(PublisherMessageConfiguration.class);
assertThat(this.context.getBean("function")).isInstanceOf(Function.class); assertThat(this.context.getBean("function")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")) FunctionInvocationWrapper function = this.catalog.lookup("function");
.isInstanceOf(Function.class); assertThat(function).isInstanceOf(Function.class);
// assertThat( assertThat(function.isInputTypeMessage()).isTrue();
// this.inspector.isMessage(this.catalog.lookup(Function.class, "function")))
// .isTrue(); Type inputType = function.getInputType();
// assertThat(this.inspector assertThat((Class<?>) FunctionTypeUtils.getGenericType(inputType)).isAssignableFrom(String.class);
// .getInputType(this.catalog.lookup(Function.class, "function"))) assertThat(FunctionTypeUtils.getRawType(inputType)).isAssignableFrom(Publisher.class);
// .isAssignableFrom(String.class);
// assertThat(this.inspector
// .getInputWrapper(this.catalog.lookup(Function.class, "function")))
// .isAssignableFrom(Publisher.class);
} }
@Test @Test
public void monoFunction() { public void monoFunction() {
create(MonoConfiguration.class); create(MonoConfiguration.class);
assertThat(this.context.getBean("function")).isInstanceOf(Function.class); assertThat(this.context.getBean("function")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")) FunctionInvocationWrapper function = this.catalog.lookup("function");
.isInstanceOf(Function.class); assertThat(function).isInstanceOf(Function.class);
// assertThat( assertThat(function.isInputTypeMessage()).isFalse();
// this.inspector.isMessage(this.catalog.lookup(Function.class, "function"))) Type inputType = function.getInputType();
// .isFalse(); Type outputType = function.getOutputType();
// assertThat(this.inspector assertThat((Class<?>) FunctionTypeUtils.getGenericType(inputType)).isAssignableFrom(String.class);
// .getInputType(this.catalog.lookup(Function.class, "function"))) assertThat(FunctionTypeUtils.getRawType(inputType)).isAssignableFrom(Flux.class);
// .isAssignableFrom(String.class); assertThat(FunctionTypeUtils.getRawType(outputType)).isAssignableFrom(Mono.class);
// assertThat(this.inspector
// .getInputWrapper(this.catalog.lookup(Function.class, "function")))
// .isAssignableFrom(Flux.class);
// assertThat(this.inspector
// .getOutputWrapper(this.catalog.lookup(Function.class, "function")))
// .isAssignableFrom(Mono.class);
} }
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test @Test
public void monoToMonoNonVoidFunction() { public void monoToMonoNonVoidFunction() {
create(MonoToMonoNonVoidConfiguration.class); create(MonoToMonoNonVoidConfiguration.class);
assertThat(this.context.getBean("function")).isInstanceOf(Function.class); FunctionInvocationWrapper function = this.catalog.lookup("function");
// assertThat(this.inspector assertThat(function).isInstanceOf(Function.class);
// .getInputType(this.catalog.lookup(Function.class, "function"))) Type inputType = function.getInputType();
// .isAssignableFrom(String.class); assertThat((Class<?>) FunctionTypeUtils.getGenericType(inputType)).isAssignableFrom(String.class);
// assertThat(this.inspector Type outputType = function.getOutputType();
// .getOutputType(this.catalog.lookup(Function.class, "function"))) assertThat((Class<?>) FunctionTypeUtils.getGenericType(outputType)).isAssignableFrom(String.class);
// .isAssignableFrom(String.class);
Function function = this.context.getBean(FunctionCatalog.class).lookup("function");
Object result = ((Mono) function.apply(Mono.just("flux"))).block();
} }
@Test @Test
public void messageFunction() { public void messageFunction() {
create(MessageConfiguration.class); create(MessageConfiguration.class);
assertThat(this.context.getBean("function")).isInstanceOf(Function.class); assertThat(this.context.getBean("function")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")) FunctionInvocationWrapper function = this.catalog.lookup("function");
.isInstanceOf(Function.class); assertThat(function).isInstanceOf(Function.class);
// assertThat( assertThat(function.isInputTypeMessage()).isTrue();
// this.inspector.isMessage(this.catalog.lookup(Function.class, "function"))) assertThat(function.isOutputTypeMessage()).isTrue();
// .isTrue(); Type inputType = function.getInputType();
// assertThat(this.inspector assertThat((Class<?>) FunctionTypeUtils.getGenericType(inputType)).isAssignableFrom(String.class);
// .getInputType(this.catalog.lookup(Function.class, "function")))
// .isAssignableFrom(String.class);
// assertThat(this.inspector
// .getInputWrapper(this.catalog.lookup(Function.class, "function")))
// .isAssignableFrom(String.class);
} }
@Test @Test
public void genericFluxFunction() { public void genericFluxFunction() {
create(GenericFluxConfiguration.class); create(GenericFluxConfiguration.class);
assertThat(this.context.getBean("function")).isInstanceOf(Function.class); assertThat(this.context.getBean("function")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")) FunctionInvocationWrapper function = this.catalog.lookup("function");
.isInstanceOf(Function.class); assertThat(function).isInstanceOf(Function.class);
// assertThat(this.inspector Type inputType = function.getInputType();
// .getInputType(this.catalog.lookup(Function.class, "function"))) assertThat(FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(inputType))).isAssignableFrom(Map.class);
// .isAssignableFrom(Map.class); assertThat(FunctionTypeUtils.getRawType(inputType)).isAssignableFrom(Flux.class);
// assertThat(this.inspector
// .getInputWrapper(this.catalog.lookup(Function.class, "function")))
// .isAssignableFrom(Flux.class);
} }
@Test @Test
public void externalFunction() { public void externalFunction() {
create(ExternalConfiguration.class); create(ExternalConfiguration.class);
assertThat(this.context.getBean("function")).isInstanceOf(Function.class); assertThat(this.context.getBean("function")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")) FunctionInvocationWrapper function = this.catalog.lookup("function");
.isInstanceOf(Function.class); assertThat(function).isInstanceOf(Function.class);
// assertThat(this.inspector Type inputType = function.getInputType();
// .getInputType(this.catalog.lookup(Function.class, "function"))) assertThat(FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(inputType))).isAssignableFrom(Map.class);
// .isAssignableFrom(Map.class);
// assertThat(this.inspector
// .getInputWrapper(this.catalog.lookup(Function.class, "function")))
// .isAssignableFrom(Map.class);
} }
@Test @Test
public void singletonFunction() { public void singletonFunction() {
create(SingletonConfiguration.class); create(SingletonConfiguration.class);
assertThat(this.context.getBean("function")).isInstanceOf(Function.class); assertThat(this.context.getBean("function")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")) FunctionInvocationWrapper function = this.catalog.lookup("function");
.isInstanceOf(Function.class); assertThat(function).isInstanceOf(Function.class);
// assertThat(this.inspector assertThat(function.isInputTypePublisher()).isFalse();
// .getInputType(this.catalog.lookup(Function.class, "function"))) assertThat(function.isOutputTypePublisher()).isFalse();
// .isAssignableFrom(Integer.class); Type inputType = function.getInputType();
// assertThat(this.inspector assertThat(FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(inputType))).isAssignableFrom(Integer.class);
// .getInputWrapper(this.catalog.lookup(Function.class, "function")))
// .isAssignableFrom(Integer.class);
} }
@Test @Test
@@ -371,58 +299,41 @@ public class ContextFunctionCatalogAutoConfigurationTests {
public void singletonMessageFunction() { public void singletonMessageFunction() {
create(SingletonMessageConfiguration.class); create(SingletonMessageConfiguration.class);
assertThat(this.context.getBean("function")).isInstanceOf(Function.class); assertThat(this.context.getBean("function")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")) FunctionInvocationWrapper function = this.catalog.lookup("function");
.isInstanceOf(Function.class); assertThat(function).isInstanceOf(Function.class);
assertThat(this.inspector assertThat(function.isInputTypeMessage()).isTrue();
.getInputType(this.catalog.lookup(Function.class, "function"))) Type inputType = function.getInputType();
.isAssignableFrom(Integer.class); assertThat((Class<?>) FunctionTypeUtils.getGenericType(inputType)).isAssignableFrom(Integer.class);
assertThat(this.inspector
.getInputWrapper(this.catalog.lookup(Function.class, "function")))
.isAssignableFrom(Integer.class);
assertThat(((FunctionInvocationWrapper) this.catalog.lookup(Function.class, "function")).isInputTypeMessage())
.isTrue();
} }
@Test @Test
public void nonParametericTypeFunction() { public void nonParametericTypeFunction() {
create(NonParametricTypeSingletonConfiguration.class); create(NonParametricTypeSingletonConfiguration.class);
assertThat(this.context.getBean("function")).isInstanceOf(Function.class); assertThat(this.context.getBean("function")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")) FunctionInvocationWrapper function = this.catalog.lookup("function");
.isInstanceOf(Function.class); assertThat(function).isInstanceOf(Function.class);
// assertThat(this.inspector Type inputType = function.getInputType();
// .getInputType(this.catalog.lookup(Function.class, "function"))) assertThat((Class<?>) FunctionTypeUtils.getGenericType(inputType)).isAssignableFrom(Integer.class);
// .isAssignableFrom(Integer.class);
// assertThat(this.inspector
// .getInputWrapper(this.catalog.lookup(Function.class, "function")))
// .isAssignableFrom(Integer.class);
} }
@Test @Test
public void componentScanBeanFunction() { public void componentScanBeanFunction() {
create(ComponentScanBeanConfiguration.class); create(ComponentScanBeanConfiguration.class);
assertThat(this.context.getBean("function")).isInstanceOf(Function.class); assertThat(this.context.getBean("function")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")) FunctionInvocationWrapper function = this.catalog.lookup("function");
.isInstanceOf(Function.class); assertThat(function).isInstanceOf(Function.class);
// assertThat(this.inspector Type inputType = function.getInputType();
// .getInputType(this.catalog.lookup(Function.class, "function"))) assertThat(FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(inputType))).isAssignableFrom(Map.class);
// .isAssignableFrom(Map.class);
// assertThat(this.inspector
// .getInputWrapper(this.catalog.lookup(Function.class, "function")))
// .isAssignableFrom(Map.class);
} }
@Test @Test
public void componentScanFunction() { public void componentScanFunction() {
create(ComponentScanConfiguration.class); create(ComponentScanConfiguration.class);
assertThat(this.context.getBean("function")).isInstanceOf(Function.class); assertThat(this.context.getBean("function")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")) FunctionInvocationWrapper function = this.catalog.lookup("function");
.isInstanceOf(Function.class); assertThat(function).isInstanceOf(Function.class);
// assertThat(this.inspector Type inputType = function.getInputType();
// .getInputType(this.catalog.lookup(Function.class, "function"))) assertThat(FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(inputType))).isAssignableFrom(Map.class);
// .isAssignableFrom(Map.class);
// assertThat(this.inspector
// .getInputWrapper(this.catalog.lookup(Function.class, "function")))
// .isAssignableFrom(Map.class);
} }
@Test @Test
@@ -430,14 +341,10 @@ public class ContextFunctionCatalogAutoConfigurationTests {
try { try {
create("greeter.jar", ComponentScanJarConfiguration.class); create("greeter.jar", ComponentScanJarConfiguration.class);
assertThat(this.context.getBean("greeter")).isInstanceOf(Function.class); assertThat(this.context.getBean("greeter")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "greeter")) FunctionInvocationWrapper function = this.catalog.lookup("greeter");
.isInstanceOf(Function.class); assertThat(function).isInstanceOf(Function.class);
// assertThat(this.inspector Type inputType = function.getInputType();
// .getInputType(this.catalog.lookup(Function.class, "greeter"))) assertThat((Class<?>) FunctionTypeUtils.getGenericType(inputType)).isAssignableFrom(String.class);
// .isAssignableFrom(String.class);
// assertThat(this.inspector
// .getInputWrapper(this.catalog.lookup(Function.class, "greeter")))
// .isAssignableFrom(String.class);
} }
finally { finally {
ClassUtils.overrideThreadContextClassLoader(getClass().getClassLoader()); ClassUtils.overrideThreadContextClassLoader(getClass().getClassLoader());
@@ -465,9 +372,6 @@ public class ContextFunctionCatalogAutoConfigurationTests {
.lookup(Function.class, "function"); .lookup(Function.class, "function");
assertThat(function.apply(Flux.just("foo")).blockFirst()).isEqualTo("FOO"); assertThat(function.apply(Flux.just("foo")).blockFirst()).isEqualTo("FOO");
assertThat(bean).isNotSameAs(function); assertThat(bean).isNotSameAs(function);
// assertThat(this.inspector.getRegistration(function)).isNotNull();
// assertThat(this.inspector.getRegistration(function).getType())
// .isEqualTo(this.inspector.getRegistration(function).getType());
} }
@Test @Test
@@ -494,12 +398,9 @@ public class ContextFunctionCatalogAutoConfigurationTests {
public void qualifiedBean() { public void qualifiedBean() {
create(QualifiedConfiguration.class); create(QualifiedConfiguration.class);
assertThat(this.context.getBean("function")).isInstanceOf(Function.class); assertThat(this.context.getBean("function")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")) assertThat((Function<?, ?>) this.catalog.lookup("function")).isNull();
.isNull(); assertThat((Function<?, ?>) this.catalog.lookup("other")).isNotNull();
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "other")) assertThat(FunctionTypeUtils.getGenericType(((FunctionInvocationWrapper) this.catalog.lookup("other")).getInputType()))
.isInstanceOf(Function.class);
assertThat(
this.inspector.getInputType(this.catalog.lookup(Function.class, "other")))
.isEqualTo(String.class); .isEqualTo(String.class);
} }
@@ -518,12 +419,8 @@ public class ContextFunctionCatalogAutoConfigurationTests {
public void registrationBean() { public void registrationBean() {
create(RegistrationConfiguration.class); create(RegistrationConfiguration.class);
assertThat(this.context.getBean("function")).isInstanceOf(Function.class); assertThat(this.context.getBean("function")).isInstanceOf(Function.class);
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")) assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")).isInstanceOf(Function.class);
.isInstanceOf(Function.class); assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "registration")).isInstanceOf(Function.class);
// .isNull();
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "registration"))
.isInstanceOf(Function.class);
// .isNull();
assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "other")) assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "other"))
.isInstanceOf(Function.class); .isInstanceOf(Function.class);
} }
@@ -557,7 +454,7 @@ public class ContextFunctionCatalogAutoConfigurationTests {
private void create(Class<?>[] types, String... props) { private void create(Class<?>[] types, String... props) {
this.context = new SpringApplicationBuilder(types).properties(props).run(); this.context = new SpringApplicationBuilder(types).properties(props).run();
this.catalog = this.context.getBean(FunctionCatalog.class); this.catalog = this.context.getBean(FunctionCatalog.class);
this.inspector = this.context.getBean(FunctionInspector.class); // this.inspector = this.context.getBean(FunctionInspector.class);
} }
@EnableAutoConfiguration @EnableAutoConfiguration
@@ -616,7 +513,7 @@ public class ContextFunctionCatalogAutoConfigurationTests {
@Bean @Bean
public BeanFactoryPostProcessor someBeanFactoryPostProcessor(Environment environment, public BeanFactoryPostProcessor someBeanFactoryPostProcessor(Environment environment,
@Nullable FunctionRegistry functionCatalog, @Nullable FunctionInspector inspector) { @Nullable FunctionRegistry functionCatalog) {
return beanFactory -> { }; return beanFactory -> { };
} }
} }

View File

@@ -17,7 +17,8 @@
package org.springframework.cloud.function.web; package org.springframework.cloud.function.web;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.cloud.function.context.catalog.FunctionInspector; import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.DefaultConversionService;
@@ -32,11 +33,7 @@ public class BasicStringConverter implements StringConverter {
private ConfigurableListableBeanFactory registry; private ConfigurableListableBeanFactory registry;
private FunctionInspector inspector; public BasicStringConverter(ConfigurableListableBeanFactory registry) {
public BasicStringConverter(FunctionInspector inspector,
ConfigurableListableBeanFactory registry) {
this.inspector = inspector;
this.registry = registry; this.registry = registry;
} }
@@ -47,7 +44,8 @@ public class BasicStringConverter implements StringConverter {
this.conversionService = conversionService != null ? conversionService this.conversionService = conversionService != null ? conversionService
: new DefaultConversionService(); : new DefaultConversionService();
} }
Class<?> type = this.inspector.getInputType(function); //Class<?> type = this.inspector.getInputType(function);
Class<?> type = function == null ? Object.class : FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(((FunctionInvocationWrapper) function).getInputType()));
return this.conversionService.canConvert(String.class, type) return this.conversionService.canConvert(String.class, type)
? this.conversionService.convert(value, type) : value; ? this.conversionService.convert(value, type) : value;
} }

View File

@@ -41,7 +41,6 @@ import reactor.core.publisher.Mono;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.function.context.FunctionCatalog; import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.catalog.FunctionInspector;
import org.springframework.cloud.function.context.catalog.FunctionTypeUtils; import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper; import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
import org.springframework.cloud.function.context.config.RoutingFunction; import org.springframework.cloud.function.context.config.RoutingFunction;
@@ -81,7 +80,6 @@ public class RequestProcessor {
private static Log logger = LogFactory.getLog(RequestProcessor.class); private static Log logger = LogFactory.getLog(RequestProcessor.class);
private final FunctionInspector inspector;
private final FunctionCatalog functionCatalog; private final FunctionCatalog functionCatalog;
@@ -91,12 +89,10 @@ public class RequestProcessor {
private final List<HttpMessageReader<?>> messageReaders; private final List<HttpMessageReader<?>> messageReaders;
public RequestProcessor(FunctionInspector inspector, public RequestProcessor(FunctionCatalog functionCatalog,
FunctionCatalog functionCatalog,
ObjectProvider<JsonMapper> mapper, StringConverter converter, ObjectProvider<JsonMapper> mapper, StringConverter converter,
ObjectProvider<ServerCodecConfigurer> codecs) { ObjectProvider<ServerCodecConfigurer> codecs) {
this.mapper = mapper.getIfAvailable(); this.mapper = mapper.getIfAvailable();
this.inspector = inspector;
this.functionCatalog = functionCatalog; this.functionCatalog = functionCatalog;
this.converter = converter; this.converter = converter;
ServerCodecConfigurer source = codecs.getIfAvailable(); ServerCodecConfigurer source = codecs.getIfAvailable();
@@ -141,7 +137,9 @@ public class RequestProcessor {
public Mono<ResponseEntity<?>> post(FunctionWrapper wrapper, String body, public Mono<ResponseEntity<?>> post(FunctionWrapper wrapper, String body,
boolean stream) { boolean stream) {
Object function = wrapper.handler(); Object function = wrapper.handler();
Class<?> inputType = this.inspector.getInputType(function); Class<?> inputType = function == null
? Object.class
: FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(((FunctionInvocationWrapper) function).getInputType()));
Type itemType = getItemType(function); Type itemType = getItemType(function);
Object input = body == null && inputType.isAssignableFrom(String.class) ? "" : body; Object input = body == null && inputType.isAssignableFrom(String.class) ? "" : body;
@@ -218,9 +216,10 @@ public class RequestProcessor {
Function function = wrapper.function(); Function function = wrapper.function();
Flux<?> flux; Flux<?> flux;
Class<?> inputType = function == null ? Object.class : FunctionTypeUtils
.getRawType(FunctionTypeUtils.getGenericType(((FunctionInvocationWrapper) wrapper.handler()).getInputType()));
if (body != null) { if (body != null) {
if (Collection.class if (Collection.class.isAssignableFrom(inputType)) {
.isAssignableFrom(this.inspector.getInputType(wrapper.handler()))) {
flux = Flux.just(body); flux = Flux.just(body);
} }
else if (body instanceof Flux) { else if (body instanceof Flux) {
@@ -233,8 +232,7 @@ public class RequestProcessor {
flux = Flux.fromIterable(iterable); flux = Flux.fromIterable(iterable);
} }
} }
else if (MultiValueMap.class else if (MultiValueMap.class.isAssignableFrom(inputType)) {
.isAssignableFrom(this.inspector.getInputType(wrapper.handler()))) {
flux = Flux.just(wrapper.params()); flux = Flux.just(wrapper.params());
} }
else { else {
@@ -261,7 +259,6 @@ public class RequestProcessor {
} }
else if (function instanceof FunctionInvocationWrapper) { else if (function instanceof FunctionInvocationWrapper) {
Publisher<?> result = (Publisher<?>) function.apply(flux); Publisher<?> result = (Publisher<?>) function.apply(flux);
// Publisher<?> result = null;
if (((FunctionInvocationWrapper) function).isConsumer()) { if (((FunctionInvocationWrapper) function).isConsumer()) {
if (result != null) { if (result != null) {
((Mono) result).subscribe(); ((Mono) result).subscribe();
@@ -342,26 +339,41 @@ public class RequestProcessor {
private boolean isInputMultiple(Object handler) { private boolean isInputMultiple(Object handler) {
Class<?> type = this.inspector.getInputType(handler); FunctionInvocationWrapper function = (FunctionInvocationWrapper) handler;
Class<?> wrapper = this.inspector.getInputWrapper(handler); Class<?> type = function == null ? Object.class : FunctionTypeUtils
return Collection.class.isAssignableFrom(type) || Flux.class.equals(wrapper); .getRawType(FunctionTypeUtils.getGenericType(function.getInputType()));
return Collection.class.isAssignableFrom(type) || (function != null && FunctionTypeUtils.isFlux(function.getInputType()));
} }
private boolean isOutputSingle(Object handler) { private boolean isOutputSingle(Object handler) {
Class<?> type = this.inspector.getOutputType(handler); FunctionInvocationWrapper function = (FunctionInvocationWrapper) handler;
Class<?> wrapper = this.inspector.getOutputWrapper(handler); Type outputType = function.getOutputType();
// if (function.isOutputTypePublisher()) {
// outputType = FunctionTypeUtils.getGenericType(outputType);
// }
// if (function.isOutputTypeMessage()) {
// outputType = FunctionTypeUtils.getGenericType(outputType);
// }
Class<?> type = FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(outputType));
// Class<?> type1 = this.inspector.getOutputType(handler);
// Class<?> wrapper1 = this.inspector.getOutputWrapper(handler);
Class<?> wrapper = function.isOutputTypePublisher() ? FunctionTypeUtils.getRawType(outputType) : type;
if (Stream.class.isAssignableFrom(type)) { if (Stream.class.isAssignableFrom(type)) {
return false; return false;
} }
else { else {
return wrapper == type || Mono.class.equals(wrapper) return wrapper == type || Mono.class.equals(wrapper)
|| Optional.class.equals(wrapper); || Optional.class.equals(wrapper);
} }
} }
private Publisher<?> body(Object handler, ServerWebExchange exchange) { private Publisher<?> body(Object handler, ServerWebExchange exchange) {
ResolvableType elementType = ResolvableType FunctionInvocationWrapper function = (FunctionInvocationWrapper) handler;
.forClass(this.inspector.getInputType(handler)); Class<?> inputType = FunctionTypeUtils
.getRawType(FunctionTypeUtils.getGenericType(function.getInputType()));
ResolvableType elementType = ResolvableType.forClass(inputType);
// we effectively delegate type conversion to FunctionCatalog // we effectively delegate type conversion to FunctionCatalog
elementType = ResolvableType.forClass(String.class); elementType = ResolvableType.forClass(String.class);

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -27,7 +27,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration; import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.cloud.function.context.FunctionCatalog; import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.catalog.FunctionInspector;
import org.springframework.cloud.function.web.BasicStringConverter; import org.springframework.cloud.function.web.BasicStringConverter;
import org.springframework.cloud.function.web.RequestProcessor; import org.springframework.cloud.function.web.RequestProcessor;
import org.springframework.cloud.function.web.StringConverter; import org.springframework.cloud.function.web.StringConverter;
@@ -55,9 +54,8 @@ public class ReactorAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public StringConverter functionStringConverter(FunctionInspector inspector, public StringConverter functionStringConverter(ConfigurableListableBeanFactory beanFactory) {
ConfigurableListableBeanFactory beanFactory) { return new BasicStringConverter(beanFactory);
return new BasicStringConverter(inspector, beanFactory);
} }
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -38,7 +38,7 @@ import org.springframework.boot.web.reactive.error.DefaultErrorAttributes;
import org.springframework.boot.web.reactive.error.ErrorAttributes; import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.cloud.function.context.FunctionCatalog; import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.FunctionalSpringApplication; import org.springframework.cloud.function.context.FunctionalSpringApplication;
import org.springframework.cloud.function.context.catalog.FunctionInspector; import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper; import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
import org.springframework.cloud.function.context.config.ContextFunctionCatalogInitializer; import org.springframework.cloud.function.context.config.ContextFunctionCatalogInitializer;
import org.springframework.cloud.function.json.JsonMapper; import org.springframework.cloud.function.json.JsonMapper;
@@ -104,15 +104,14 @@ class FunctionEndpointInitializer implements ApplicationContextInitializer<Gener
private void registerEndpoint(GenericApplicationContext context) { private void registerEndpoint(GenericApplicationContext context) {
context.registerBean(StringConverter.class, context.registerBean(StringConverter.class,
() -> new BasicStringConverter(context.getBean(FunctionInspector.class), context.getBeanFactory())); () -> new BasicStringConverter(context.getBeanFactory()));
context.registerBean(RequestProcessor.class, context.registerBean(RequestProcessor.class,
() -> new RequestProcessor(context.getBean(FunctionInspector.class), () -> new RequestProcessor(
context.getBean(FunctionCatalog.class), context.getBean(FunctionCatalog.class),
context.getBeanProvider(JsonMapper.class), context.getBean(StringConverter.class), context.getBeanProvider(JsonMapper.class), context.getBean(StringConverter.class),
context.getBeanProvider(ServerCodecConfigurer.class))); context.getBeanProvider(ServerCodecConfigurer.class)));
context.registerBean(FunctionEndpointFactory.class, context.registerBean(FunctionEndpointFactory.class,
() -> new FunctionEndpointFactory(context.getBean(FunctionCatalog.class), () -> new FunctionEndpointFactory(context.getBean(FunctionCatalog.class), context.getBean(RequestProcessor.class),
context.getBean(FunctionInspector.class), context.getBean(RequestProcessor.class),
context.getEnvironment())); context.getEnvironment()));
context.registerBean(RouterFunction.class, context.registerBean(RouterFunction.class,
() -> context.getBean(FunctionEndpointFactory.class).functionEndpoints()); () -> context.getBean(FunctionEndpointFactory.class).functionEndpoints());
@@ -199,18 +198,18 @@ class FunctionEndpointFactory {
private final String handler; private final String handler;
private final FunctionInspector inspector; // private final FunctionInspector inspector;
private final RequestProcessor processor; private final RequestProcessor processor;
FunctionEndpointFactory(FunctionCatalog functionCatalog, FunctionInspector inspector, RequestProcessor processor, FunctionEndpointFactory(FunctionCatalog functionCatalog, RequestProcessor processor,
Environment environment) { Environment environment) {
String handler = environment.resolvePlaceholders("${function.handler}"); String handler = environment.resolvePlaceholders("${function.handler}");
if (handler.startsWith("$")) { if (handler.startsWith("$")) {
handler = null; handler = null;
} }
this.processor = processor; this.processor = processor;
this.inspector = inspector; // this.inspector = inspector;
this.functionCatalog = functionCatalog; this.functionCatalog = functionCatalog;
this.handler = handler; this.handler = handler;
} }
@@ -233,9 +232,12 @@ class FunctionEndpointFactory {
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public <T> RouterFunction<?> functionEndpoints() { public <T> RouterFunction<?> functionEndpoints() {
return route(POST("/**"), request -> { return route(POST("/**"), request -> {
Function<Flux<?>, Flux<?>> function = (Function<Flux<?>, Flux<?>>) extract(request); Object function = extract(request);
Class<T> outputType = (Class<T>) this.inspector.getOutputType(function); FunctionInvocationWrapper funcWrapper = (FunctionInvocationWrapper) function;
FunctionWrapper wrapper = RequestProcessor.wrapper(function, null, null); Class<?> outputType = funcWrapper == null
? Object.class
: FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(funcWrapper.getOutputType()));
FunctionWrapper wrapper = RequestProcessor.wrapper((Function<Flux<?>, Flux<?>>) function, null, null);
Mono<ResponseEntity<?>> stream = request.bodyToMono(String.class) Mono<ResponseEntity<?>> stream = request.bodyToMono(String.class)
.flatMap(content -> this.processor.post(wrapper, content, false)); .flatMap(content -> this.processor.post(wrapper, content, false));
return stream.flatMap(entity -> { return stream.flatMap(entity -> {
@@ -245,7 +247,8 @@ class FunctionEndpointFactory {
}) })
.andRoute(GET("/**"), request -> { .andRoute(GET("/**"), request -> {
Object functionComponent = extract(request); Object functionComponent = extract(request);
Class<T> outputType = (Class<T>) this.inspector.getOutputType(functionComponent); FunctionInvocationWrapper funcWrapper = (FunctionInvocationWrapper) functionComponent;
Class<?> outputType = FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(funcWrapper.getOutputType()));
if (((FunctionInvocationWrapper) functionComponent).isSupplier()) { if (((FunctionInvocationWrapper) functionComponent).isSupplier()) {
Supplier<? extends Flux<?>> supplier = (Supplier<Flux<?>>) functionComponent; Supplier<? extends Flux<?>> supplier = (Supplier<Flux<?>>) functionComponent;
FunctionWrapper wrapper = RequestProcessor.wrapper(null, null, supplier); FunctionWrapper wrapper = RequestProcessor.wrapper(null, null, supplier);

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -27,7 +27,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration; import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.cloud.function.context.FunctionCatalog; import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.catalog.FunctionInspector;
import org.springframework.cloud.function.web.BasicStringConverter; import org.springframework.cloud.function.web.BasicStringConverter;
import org.springframework.cloud.function.web.RequestProcessor; import org.springframework.cloud.function.web.RequestProcessor;
import org.springframework.cloud.function.web.StringConverter; import org.springframework.cloud.function.web.StringConverter;
@@ -55,9 +54,8 @@ public class ReactorAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public StringConverter functionStringConverter(FunctionInspector inspector, public StringConverter functionStringConverter(ConfigurableListableBeanFactory beanFactory) {
ConfigurableListableBeanFactory beanFactory) { return new BasicStringConverter(beanFactory);
return new BasicStringConverter(inspector, beanFactory);
} }
} }