From b897f96e0ff61ec8965ebd51bbad0d5b798e5e7f Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 19 Apr 2017 10:39:51 +0200 Subject: [PATCH] Use PathRemainingMatchInfo in RequestPredicates This commit uses the newly introduced PathRemainingMatchInfo (316a680577a392464c6da60d989b00acd2a5e472) in path-based RequestPredicates. Issue: SPR-15419 --- .../function/server/RequestPredicates.java | 28 +++++++++---------- .../server/NestedRouteIntegrationTests.java | 2 -- 2 files changed, 13 insertions(+), 17 deletions(-) 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 5ab9d2cc5d3..d7a69747684 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 @@ -42,7 +42,6 @@ import org.springframework.web.server.WebSession; import org.springframework.web.util.UriUtils; import org.springframework.web.util.patterns.PathPattern; import org.springframework.web.util.patterns.PathPatternParser; -import org.springframework.web.util.patterns.PathRemainingMatchInfo; /** * Implementations of {@link RequestPredicate} that implement various useful @@ -344,7 +343,7 @@ public abstract class RequestPredicates { boolean match = this.pattern.matches(path); traceMatch("Pattern", this.pattern.getPatternString(), path, match); if (match) { - mergeTemplateVariables(request); + mergeTemplateVariables(request, this.pattern.matchAndExtract(request.path())); return true; } else { @@ -354,25 +353,24 @@ public abstract class RequestPredicates { @Override public Optional nest(ServerRequest request) { - PathRemainingMatchInfo info = this.pattern.getPathRemaining(request.path()); - String remainingPath = (info == null ? null : info.getPathRemaining()); - return Optional.ofNullable(remainingPath) - .map(path -> !path.startsWith("/") ? "/" + path : path) - .map(path -> { - // TODO: re-enable when SPR-15419 has been fixed. - // mergeTemplateVariables(request); + return Optional.ofNullable(this.pattern.getPathRemaining(request.path())) + .map(info -> { + mergeTemplateVariables(request, info.getMatchingVariables()); + String path = info.getPathRemaining(); + if (!path.startsWith("/")) { + path = "/" + path; + } return new SubPathServerRequestWrapper(request, path); }); } - private void mergeTemplateVariables(ServerRequest request) { - Map newVariables = this.pattern.matchAndExtract(request.path()); - if (!newVariables.isEmpty()) { + private void mergeTemplateVariables(ServerRequest request, Map variables) { + if (!variables.isEmpty()) { Map oldVariables = request.pathVariables(); - Map variables = new LinkedHashMap<>(oldVariables); - variables.putAll(newVariables); + Map mergedVariables = new LinkedHashMap<>(oldVariables); + mergedVariables.putAll(variables); request.attributes().put(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE, - Collections.unmodifiableMap(variables)); + Collections.unmodifiableMap(mergedVariables)); } } 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 b11d465e1b1..ae5a3d4affb 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 @@ -16,7 +16,6 @@ package org.springframework.web.reactive.function.server; -import org.junit.Ignore; import org.junit.Test; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -71,7 +70,6 @@ public class NestedRouteIntegrationTests extends AbstractRouterFunctionIntegrati } @Test - @Ignore("SPR-15419") public void variables() throws Exception { ResponseEntity result = restTemplate.getForEntity("http://localhost:" + port + "/1/2/3", String.class);