GH-792 Fix Supplier streaming in s-c-function-web

Resolves #792
This commit is contained in:
Oleg Zhurakousky
2022-01-24 15:36:24 +01:00
parent f054d4534c
commit 4699f69be5
10 changed files with 109 additions and 51 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.function.web.flux;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -74,11 +75,17 @@ public class FunctionController {
return (Mono<ResponseEntity<?>>) FunctionWebRequestProcessingHelper.processRequest(wrapper(request), body, false);
}
@SuppressWarnings("unchecked")
@PostMapping(path = "/**", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@ResponseBody
public Mono<ResponseEntity<?>> postStream(ServerWebExchange request, @RequestBody(required = false) Flux<String> body) {
return (Mono<ResponseEntity<?>>) FunctionWebRequestProcessingHelper.processRequest(wrapper(request), body, false);
public Publisher<?> postStream(ServerWebExchange request, @RequestBody(required = false) Flux<String> body) {
return FunctionWebRequestProcessingHelper.processRequest(wrapper(request), body, true);
}
@GetMapping(path = "/**", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@ResponseBody
public Publisher<?> getStream(ServerWebExchange request) {
FunctionWrapper wrapper = wrapper(request);
return FunctionWebRequestProcessingHelper.processRequest(wrapper, wrapper.getArgument(), true);
}
@SuppressWarnings("unchecked")
@@ -89,14 +96,6 @@ public class FunctionController {
return (Mono<ResponseEntity<?>>) FunctionWebRequestProcessingHelper.processRequest(wrapper, wrapper.getArgument(), false);
}
@SuppressWarnings("unchecked")
@GetMapping(path = "/**", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@ResponseBody
public Mono<ResponseEntity<?>> getStream(ServerWebExchange request) {
FunctionWrapper wrapper = wrapper(request);
return (Mono<ResponseEntity<?>>) FunctionWebRequestProcessingHelper.processRequest(wrapper, wrapper.getArgument(), true);
}
private FunctionWrapper wrapper(ServerWebExchange request) {
FunctionInvocationWrapper function = (FunctionInvocationWrapper) request
.getAttribute(WebRequestConstants.HANDLER);

View File

@@ -94,21 +94,18 @@ public class FunctionController {
.headers(response.getHeaders()).body((Publisher<?>) response.getBody()));
}
@SuppressWarnings("unchecked")
@GetMapping(path = "/**", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@ResponseBody
public Mono<ResponseEntity<Publisher<?>>> getStream(WebRequest request) {
public Publisher<?> getStream(WebRequest request) {
FunctionWrapper wrapper = wrapper(request);
return ((Mono<ResponseEntity<?>>) FunctionWebRequestProcessingHelper
.processRequest(wrapper, wrapper.getArgument(), true)).map(response -> ResponseEntity.ok()
.headers(response.getHeaders()).body((Publisher<?>) response.getBody()));
return FunctionWebRequestProcessingHelper
.processRequest(wrapper, wrapper.getArgument(), true);
}
@PostMapping(path = "/**")
@ResponseBody
public Object post(WebRequest request, @RequestBody(required = false) String body) {
String argument = StringUtils.hasText(body) ? body : "";
return FunctionWebRequestProcessingHelper.processRequest(wrapper(request), argument, false);
return FunctionWebRequestProcessingHelper.processRequest(wrapper(request), body, false);
}
@GetMapping(path = "/**")

View File

@@ -84,7 +84,7 @@ public final class FunctionWebRequestProcessingHelper {
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object processRequest(FunctionWrapper wrapper, Object argument, boolean eventStream) {
public static Publisher<?> processRequest(FunctionWrapper wrapper, Object argument, boolean eventStream) {
FunctionInvocationWrapper function = wrapper.getFunction();
if (function == null) {
@@ -99,7 +99,7 @@ public final class FunctionWebRequestProcessingHelper {
function.setSkipOutputConversion(true);
}
Object input = argument == null ? Flux.empty() : (argument instanceof Publisher ? Flux.from((Publisher) argument) : inputMessage);
Object input = argument == null ? "" : (argument instanceof Publisher ? Flux.from((Publisher) argument) : inputMessage);
Object result = function.apply(input);
if (function.isConsumer()) {
@@ -115,7 +115,7 @@ public final class FunctionWebRequestProcessingHelper {
if (result instanceof Publisher) {
pResult = (Publisher) result;
if (eventStream) {
return Flux.from(pResult).then(Mono.fromSupplier(() -> responseOkBuilder.body(result)));
return Flux.from(pResult);
}
if (pResult instanceof Flux) {