Closed
Description
Brian Clozel opened SPR-16692 and commented
Given the following RouterFunction
:
@Bean
public RouterFunction router(TestHandler testHandler) {
return RouterFunctions.nest(path("/test/{something}"),
RouterFunctions.route(GET("/info"), testHandler::withTemplateVariables))
.andRoute(method(HttpMethod.GET), testHandler::withoutTemplateVariables);
}
And its companion handler:
@Component
public class TestHandler {
public Mono<ServerResponse> withTemplateVariables(ServerRequest request) {
Map<String, String> attr = (Map<String, String>)
request.attribute(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE).orElse(Collections.emptyMap());
return ServerResponse.ok().render("index");
}
public Mono<ServerResponse> withoutTemplateVariables(ServerRequest request) {
Map<String, String> attr = (Map<String, String>)
request.attribute(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE).orElse(Collections.emptyMap());
return ServerResponse.ok().render("index");
}
}
When a client sends a request like GET /test/spring
, the following happens:
- first, the request matches the
path("/test/{something}")
predicate and adds the path variable to the request - then, the nested predicate is tested
GET("/info")
but does not match - finally,
.andRoute(method(HttpMethod.GET)
matches and its handler is called - calling
request.pathVariables()
inside that handler shows state that is relevant to the previously matched predicates, not the current one
Is there a way to invalidate that request attribute?
Affects: 5.0.5
Issue Links:
- Nested PathPatternPredicate doesn't expose path variables [SPR-16879] #21418 Nested PathPatternPredicate doesn't expose path variables
- PathVariable with nested router function throws IllegalArgumentException [SPR-16868] #21407 PathVariable with nested router function throws IllegalArgumentException
- PathPatternPredicate still changes variables after failed AndPredicate [SPR-17210] #21743 PathPatternPredicate still changes variables after failed AndPredicate