Skip to content

Commit 729535d

Browse files
committed
Ensure presence of cached lookupPath
Closes gh-26546
1 parent 634c771 commit 729535d

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -501,6 +501,11 @@ public final HandlerExecutionChain getHandler(HttpServletRequest request) throws
501501
handler = obtainApplicationContext().getBean(handlerName);
502502
}
503503

504+
// Ensure presence of cached lookupPath for interceptors and others
505+
if (!ServletRequestPathUtils.hasCachedPath(request)) {
506+
initLookupPath(request);
507+
}
508+
504509
HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);
505510

506511
if (logger.isTraceEnabled()) {

spring-webmvc/src/test/java/org/springframework/web/servlet/handler/HandlerMappingTests.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323

2424
import javax.servlet.http.HttpServletRequest;
2525

26+
import org.junit.jupiter.api.Test;
2627
import org.junit.jupiter.params.provider.Arguments;
2728

2829
import org.springframework.web.context.support.StaticWebApplicationContext;
@@ -66,16 +67,30 @@ void orderedInterceptors(
6667
mapping.setApplicationContext(new StaticWebApplicationContext());
6768

6869
HandlerExecutionChain chain = mapping.getHandler(requestFactory.apply("/"));
70+
6971
assertThat(chain).isNotNull();
7072
assertThat(chain.getInterceptorList()).contains(i1.getInterceptor(), i2, i3.getInterceptor(), i4);
7173
}
7274

75+
@Test // gh-26546
76+
void abstractHandlerMappingEnsuresCachedLookupPath() throws Exception {
77+
MappedInterceptor interceptor = new MappedInterceptor(new String[] {"/**"}, mock(HandlerInterceptor.class));
78+
TestHandlerMapping mapping = new TestHandlerMapping();
79+
mapping.setInterceptors(interceptor);
80+
mapping.setApplicationContext(new StaticWebApplicationContext());
81+
82+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
83+
HandlerExecutionChain chain = mapping.getHandler(request);
84+
85+
assertThat(chain).isNotNull();
86+
assertThat(chain.getInterceptorList()).contains(interceptor.getInterceptor());
87+
}
88+
7389

7490
private static class TestHandlerMapping extends AbstractHandlerMapping {
7591

7692
@Override
7793
protected Object getHandlerInternal(HttpServletRequest request) {
78-
initLookupPath(request);
7994
return new Object();
8095
}
8196
}

0 commit comments

Comments
 (0)