Skip to content

Commit 41606b7

Browse files
committed
Initial support for Supplier functions in server-webmvc
See gh-3646
1 parent adf32f6 commit 41606b7

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/HandlerFunctions.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static HandlerFunction<ServerResponse> fn(String functionName) {
6161
FunctionInvocationWrapper function = functionCatalog.lookup(expandedFunctionName,
6262
request.headers().accept().stream().map(MimeType::toString).toArray(String[]::new));
6363
if (function != null) {
64-
Object body = request.body(function.getRawInputType());
64+
Object body = function.isSupplier() ? null : request.body(function.getRawInputType());
6565
return processRequest(request, function, body, false, Collections.emptyList(), Collections.emptyList());
6666
}
6767
return ServerResponse.notFound().build();

spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/handler/FunctionHandlerTests.java

+23
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import java.util.Locale;
2020
import java.util.function.Function;
21+
import java.util.function.Supplier;
2122

23+
import org.junit.jupiter.api.Disabled;
2224
import org.junit.jupiter.api.Test;
2325

2426
import org.springframework.beans.factory.annotation.Autowired;
@@ -66,6 +68,19 @@ public void testTemplatedFunctionWorks() {
6668
.isEqualTo("HELLO");
6769
}
6870

71+
@Disabled
72+
@Test
73+
public void testSupplierFunctionWorks() {
74+
restClient.get()
75+
.uri("/supplierfunction")
76+
.accept(MediaType.TEXT_PLAIN)
77+
.exchange()
78+
.expectStatus()
79+
.isOk()
80+
.expectBody(String.class)
81+
.isEqualTo("hello");
82+
}
83+
6984
@SpringBootConfiguration
7085
@EnableAutoConfiguration
7186
protected static class TestConfiguration {
@@ -75,6 +90,11 @@ Function<String, String> upper() {
7590
return s -> s.toUpperCase(Locale.ROOT);
7691
}
7792

93+
@Bean
94+
Supplier<String> hello() {
95+
return () -> "hello";
96+
}
97+
7898
@Bean
7999
public RouterFunction<ServerResponse> gatewayRouterFunctionsSimpleFunction() {
80100
// @formatter:off
@@ -83,6 +103,9 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsSimpleFunction() {
83103
.build()
84104
.and(route("testtemplatedfunction")
85105
.POST("/templatedfunction/{fnName}", fn("{fnName}"))
106+
.build())
107+
.and(route("testsupplierfunction")
108+
.POST("/supplierfunction", fn("hello"))
86109
.build());
87110
// @formatter:on
88111
}

0 commit comments

Comments
 (0)