From 9b3c92e8d2a9a284ec031687fdb6aad5c2407f67 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 21 May 2019 12:07:57 +0200 Subject: [PATCH] Add leading slash for path predicate if not present This commit adds a leading slash for path predicates in both WebFlux.fn and WebMvc.fn. Closes gh-22795 --- .../web/reactive/function/server/RequestPredicates.java | 3 +++ .../function/server/NestedRouteIntegrationTests.java | 2 +- .../reactive/function/server/RequestPredicatesTests.java | 8 ++++++++ .../web/servlet/function/RequestPredicates.java | 3 +++ .../web/servlet/function/RequestPredicatesTests.java | 8 ++++++++ 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java index a705e0e4b4..f098c57a6c 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java @@ -111,6 +111,9 @@ public abstract class RequestPredicates { */ public static RequestPredicate path(String pattern) { Assert.notNull(pattern, "'pattern' must not be null"); + if (!pattern.isEmpty() && !pattern.startsWith("/")) { + pattern = "/" + pattern; + } return pathPredicates(DEFAULT_PATTERN_PARSER).apply(pattern); } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/NestedRouteIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/NestedRouteIntegrationTests.java index 39ff4b60db..b2fe758404 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/NestedRouteIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/NestedRouteIntegrationTests.java @@ -52,7 +52,7 @@ public class NestedRouteIntegrationTests extends AbstractRouterFunctionIntegrati return nest(path("/foo/"), route(GET("/bar"), nestedHandler::pattern) .andRoute(GET("/baz"), nestedHandler::pattern)) - .andNest(GET("/{foo}"), + .andNest(GET("{foo}"), route(GET("/bar"), nestedHandler::variables).and( nest(GET("/{bar}"), route(GET("/{baz}"), nestedHandler::variables)))) diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java index cb9bfe24aa..8576604112 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java @@ -109,6 +109,14 @@ public class RequestPredicatesTests { assertFalse(predicate.test(request)); } + @Test + public void pathNoLeadingSlash() { + URI uri = URI.create("http://localhost/path"); + RequestPredicate predicate = RequestPredicates.path("p*"); + MockServerRequest request = MockServerRequest.builder().uri(uri).build(); + assertTrue(predicate.test(request)); + } + @Test public void pathEncoded() { URI uri = URI.create("http://localhost/foo%20bar"); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java index 57d42f45d3..672da39e29 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java @@ -107,6 +107,9 @@ public abstract class RequestPredicates { */ public static RequestPredicate path(String pattern) { Assert.notNull(pattern, "'pattern' must not be null"); + if (!pattern.isEmpty() && !pattern.startsWith("/")) { + pattern = "/" + pattern; + } return pathPredicates(DEFAULT_PATTERN_PARSER).apply(pattern); } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/function/RequestPredicatesTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/function/RequestPredicatesTests.java index b1e434d455..f1fa58fe5a 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/function/RequestPredicatesTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/function/RequestPredicatesTests.java @@ -117,6 +117,14 @@ public class RequestPredicatesTests { assertFalse(predicate.test(request)); } + @Test + public void pathNoLeadingSlash() { + MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/path"); + ServerRequest request = new DefaultServerRequest(servletRequest, emptyList()); + RequestPredicate predicate = RequestPredicates.path("p*"); + assertTrue(predicate.test(request)); + } + @Test public void pathEncoded() { MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/foo%20bar");