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

@@ -17,7 +17,8 @@
package org.springframework.cloud.function.web;
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.support.DefaultConversionService;
@@ -32,11 +33,7 @@ public class BasicStringConverter implements StringConverter {
private ConfigurableListableBeanFactory registry;
private FunctionInspector inspector;
public BasicStringConverter(FunctionInspector inspector,
ConfigurableListableBeanFactory registry) {
this.inspector = inspector;
public BasicStringConverter(ConfigurableListableBeanFactory registry) {
this.registry = registry;
}
@@ -47,7 +44,8 @@ public class BasicStringConverter implements StringConverter {
this.conversionService = conversionService != null ? conversionService
: 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)
? 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.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.SimpleFunctionRegistry.FunctionInvocationWrapper;
import org.springframework.cloud.function.context.config.RoutingFunction;
@@ -81,7 +80,6 @@ public class RequestProcessor {
private static Log logger = LogFactory.getLog(RequestProcessor.class);
private final FunctionInspector inspector;
private final FunctionCatalog functionCatalog;
@@ -91,12 +89,10 @@ public class RequestProcessor {
private final List<HttpMessageReader<?>> messageReaders;
public RequestProcessor(FunctionInspector inspector,
FunctionCatalog functionCatalog,
public RequestProcessor(FunctionCatalog functionCatalog,
ObjectProvider<JsonMapper> mapper, StringConverter converter,
ObjectProvider<ServerCodecConfigurer> codecs) {
this.mapper = mapper.getIfAvailable();
this.inspector = inspector;
this.functionCatalog = functionCatalog;
this.converter = converter;
ServerCodecConfigurer source = codecs.getIfAvailable();
@@ -141,7 +137,9 @@ public class RequestProcessor {
public Mono<ResponseEntity<?>> post(FunctionWrapper wrapper, String body,
boolean stream) {
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);
Object input = body == null && inputType.isAssignableFrom(String.class) ? "" : body;
@@ -218,9 +216,10 @@ public class RequestProcessor {
Function function = wrapper.function();
Flux<?> flux;
Class<?> inputType = function == null ? Object.class : FunctionTypeUtils
.getRawType(FunctionTypeUtils.getGenericType(((FunctionInvocationWrapper) wrapper.handler()).getInputType()));
if (body != null) {
if (Collection.class
.isAssignableFrom(this.inspector.getInputType(wrapper.handler()))) {
if (Collection.class.isAssignableFrom(inputType)) {
flux = Flux.just(body);
}
else if (body instanceof Flux) {
@@ -233,8 +232,7 @@ public class RequestProcessor {
flux = Flux.fromIterable(iterable);
}
}
else if (MultiValueMap.class
.isAssignableFrom(this.inspector.getInputType(wrapper.handler()))) {
else if (MultiValueMap.class.isAssignableFrom(inputType)) {
flux = Flux.just(wrapper.params());
}
else {
@@ -261,7 +259,6 @@ public class RequestProcessor {
}
else if (function instanceof FunctionInvocationWrapper) {
Publisher<?> result = (Publisher<?>) function.apply(flux);
// Publisher<?> result = null;
if (((FunctionInvocationWrapper) function).isConsumer()) {
if (result != null) {
((Mono) result).subscribe();
@@ -342,26 +339,41 @@ public class RequestProcessor {
private boolean isInputMultiple(Object handler) {
Class<?> type = this.inspector.getInputType(handler);
Class<?> wrapper = this.inspector.getInputWrapper(handler);
return Collection.class.isAssignableFrom(type) || Flux.class.equals(wrapper);
FunctionInvocationWrapper function = (FunctionInvocationWrapper) handler;
Class<?> type = function == null ? Object.class : FunctionTypeUtils
.getRawType(FunctionTypeUtils.getGenericType(function.getInputType()));
return Collection.class.isAssignableFrom(type) || (function != null && FunctionTypeUtils.isFlux(function.getInputType()));
}
private boolean isOutputSingle(Object handler) {
Class<?> type = this.inspector.getOutputType(handler);
Class<?> wrapper = this.inspector.getOutputWrapper(handler);
FunctionInvocationWrapper function = (FunctionInvocationWrapper) 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)) {
return false;
}
else {
return wrapper == type || Mono.class.equals(wrapper)
|| Optional.class.equals(wrapper);
}
}
private Publisher<?> body(Object handler, ServerWebExchange exchange) {
ResolvableType elementType = ResolvableType
.forClass(this.inspector.getInputType(handler));
FunctionInvocationWrapper function = (FunctionInvocationWrapper) handler;
Class<?> inputType = FunctionTypeUtils
.getRawType(FunctionTypeUtils.getGenericType(function.getInputType()));
ResolvableType elementType = ResolvableType.forClass(inputType);
// we effectively delegate type conversion to FunctionCatalog
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");
* 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.jackson.JacksonAutoConfiguration;
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.RequestProcessor;
import org.springframework.cloud.function.web.StringConverter;
@@ -55,9 +54,8 @@ public class ReactorAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public StringConverter functionStringConverter(FunctionInspector inspector,
ConfigurableListableBeanFactory beanFactory) {
return new BasicStringConverter(inspector, beanFactory);
public StringConverter functionStringConverter(ConfigurableListableBeanFactory beanFactory) {
return new BasicStringConverter(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");
* 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.cloud.function.context.FunctionCatalog;
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.config.ContextFunctionCatalogInitializer;
import org.springframework.cloud.function.json.JsonMapper;
@@ -104,15 +104,14 @@ class FunctionEndpointInitializer implements ApplicationContextInitializer<Gener
private void registerEndpoint(GenericApplicationContext context) {
context.registerBean(StringConverter.class,
() -> new BasicStringConverter(context.getBean(FunctionInspector.class), context.getBeanFactory()));
() -> new BasicStringConverter(context.getBeanFactory()));
context.registerBean(RequestProcessor.class,
() -> new RequestProcessor(context.getBean(FunctionInspector.class),
() -> new RequestProcessor(
context.getBean(FunctionCatalog.class),
context.getBeanProvider(JsonMapper.class), context.getBean(StringConverter.class),
context.getBeanProvider(ServerCodecConfigurer.class)));
context.registerBean(FunctionEndpointFactory.class,
() -> new FunctionEndpointFactory(context.getBean(FunctionCatalog.class),
context.getBean(FunctionInspector.class), context.getBean(RequestProcessor.class),
() -> new FunctionEndpointFactory(context.getBean(FunctionCatalog.class), context.getBean(RequestProcessor.class),
context.getEnvironment()));
context.registerBean(RouterFunction.class,
() -> context.getBean(FunctionEndpointFactory.class).functionEndpoints());
@@ -199,18 +198,18 @@ class FunctionEndpointFactory {
private final String handler;
private final FunctionInspector inspector;
// private final FunctionInspector inspector;
private final RequestProcessor processor;
FunctionEndpointFactory(FunctionCatalog functionCatalog, FunctionInspector inspector, RequestProcessor processor,
FunctionEndpointFactory(FunctionCatalog functionCatalog, RequestProcessor processor,
Environment environment) {
String handler = environment.resolvePlaceholders("${function.handler}");
if (handler.startsWith("$")) {
handler = null;
}
this.processor = processor;
this.inspector = inspector;
// this.inspector = inspector;
this.functionCatalog = functionCatalog;
this.handler = handler;
}
@@ -233,9 +232,12 @@ class FunctionEndpointFactory {
@SuppressWarnings({ "unchecked" })
public <T> RouterFunction<?> functionEndpoints() {
return route(POST("/**"), request -> {
Function<Flux<?>, Flux<?>> function = (Function<Flux<?>, Flux<?>>) extract(request);
Class<T> outputType = (Class<T>) this.inspector.getOutputType(function);
FunctionWrapper wrapper = RequestProcessor.wrapper(function, null, null);
Object function = extract(request);
FunctionInvocationWrapper funcWrapper = (FunctionInvocationWrapper) function;
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)
.flatMap(content -> this.processor.post(wrapper, content, false));
return stream.flatMap(entity -> {
@@ -245,7 +247,8 @@ class FunctionEndpointFactory {
})
.andRoute(GET("/**"), 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()) {
Supplier<? extends Flux<?>> supplier = (Supplier<Flux<?>>) functionComponent;
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");
* 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.jackson.JacksonAutoConfiguration;
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.RequestProcessor;
import org.springframework.cloud.function.web.StringConverter;
@@ -55,9 +54,8 @@ public class ReactorAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public StringConverter functionStringConverter(FunctionInspector inspector,
ConfigurableListableBeanFactory beanFactory) {
return new BasicStringConverter(inspector, beanFactory);
public StringConverter functionStringConverter(ConfigurableListableBeanFactory beanFactory) {
return new BasicStringConverter(beanFactory);
}
}