Skip to content

PathPatternPredicate leaves path variables in request even if predicate doesn't match [SPR-16692] #21233

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

  1. first, the request matches the path("/test/{something}") predicate and adds the path variable to the request
  2. then, the nested predicate is tested GET("/info") but does not match
  3. finally, .andRoute(method(HttpMethod.GET) matches and its handler is called
  4. 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:

Referenced from: commits a63f04d, 51325af

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions