Deprecate UrlPathHelper shortcuts on AbstractHandlerMapping

Given the availability of two alternatives mechanisms for URL path
matching, PathPatternParser and AntPathMatcher, and now that
parsed patterns are enabled by default, it makes sense to reduce the
proliferation of options on AbstractHandlerMapping by deprecating
shortcuts related to String path matching. Most applications rely
on Boot and on the MVC config to do all this.

See gh-28607
This commit is contained in:
rstoyanchev 2022-06-29 13:03:10 +01:00
parent 8d08b1d323
commit 14fd2605a3
11 changed files with 44 additions and 34 deletions

View File

@ -58,7 +58,6 @@ import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
import org.springframework.web.servlet.handler.MappedInterceptor;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
@ -68,6 +67,7 @@ import org.springframework.web.servlet.support.SessionFlashMapManager;
import org.springframework.web.servlet.theme.FixedThemeResolver;
import org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.util.UrlPathHelper;
import org.springframework.web.util.pattern.PathPatternParser;
/**
@ -353,7 +353,7 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
/**
* Set if ";" (semicolon) content should be stripped from the request URI. The value,
* if provided, is in turn set on
* {@link AbstractHandlerMapping#setRemoveSemicolonContent(boolean)}.
* {@link org.springframework.web.util.UrlPathHelper#setRemoveSemicolonContent(boolean)}.
*/
public StandaloneMockMvcBuilder setRemoveSemicolonContent(boolean removeSemicolonContent) {
this.removeSemicolonContent = removeSemicolonContent;
@ -477,7 +477,9 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
handlerMapping.setPatternParser(null);
handlerMapping.setUseSuffixPatternMatch(useSuffixPatternMatch);
if (removeSemicolonContent != null) {
handlerMapping.setRemoveSemicolonContent(removeSemicolonContent);
UrlPathHelper pathHelper = new UrlPathHelper();
pathHelper.setRemoveSemicolonContent(removeSemicolonContent);
handlerMapping.setUrlPathHelper(pathHelper);
}
}
else if (patternParser != null) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -46,6 +46,7 @@ import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UrlPathHelper;
import static org.hamcrest.core.Is.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@ -119,7 +120,9 @@ public class EncodedUriTests {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof RequestMappingHandlerMapping requestMappingHandlerMapping) {
// URL decode after request mapping, not before.
requestMappingHandlerMapping.setUrlDecode(false);
UrlPathHelper pathHelper = new UrlPathHelper();
pathHelper.setUrlDecode(false);
requestMappingHandlerMapping.setUrlPathHelper(pathHelper);
}
return bean;
}

View File

@ -179,8 +179,9 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
* <p><strong>Note:</strong> This property is mutually exclusive with and
* ignored when {@link #setPatternParser(PathPatternParser)} is set.
* @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath(boolean)
* @deprecated as of 6.0, in favor of using {@link #setUrlPathHelper(UrlPathHelper)}
*/
@SuppressWarnings("deprecation")
@Deprecated
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
if (this.corsConfigurationSource instanceof UrlBasedCorsConfigurationSource urlConfigSource) {
@ -193,8 +194,9 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
* <p><strong>Note:</strong> This property is mutually exclusive with and
* ignored when {@link #setPatternParser(PathPatternParser)} is set.
* @see org.springframework.web.util.UrlPathHelper#setUrlDecode(boolean)
* @deprecated as of 6.0, in favor of using {@link #setUrlPathHelper(UrlPathHelper)}
*/
@SuppressWarnings("deprecation")
@Deprecated
public void setUrlDecode(boolean urlDecode) {
this.urlPathHelper.setUrlDecode(urlDecode);
if (this.corsConfigurationSource instanceof UrlBasedCorsConfigurationSource urlConfigSource) {
@ -207,8 +209,9 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
* <p><strong>Note:</strong> This property is mutually exclusive with and
* ignored when {@link #setPatternParser(PathPatternParser)} is set.
* @see org.springframework.web.util.UrlPathHelper#setRemoveSemicolonContent(boolean)
* @deprecated as of 6.0, in favor of using {@link #setUrlPathHelper(UrlPathHelper)}
*/
@SuppressWarnings("deprecation")
@Deprecated
public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
this.urlPathHelper.setRemoveSemicolonContent(removeSemicolonContent);
if (this.corsConfigurationSource instanceof UrlBasedCorsConfigurationSource urlConfigSource) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -37,11 +37,9 @@ import org.springframework.util.StringUtils;
* a single handler.
*
* <p>Supports direct matches (given "/test" -&gt; registered "/test") and "*"
* matches (given "/test" -&gt; registered "/t*"). Note that the default is
* to map within the current servlet mapping if applicable; see the
* {@link #setAlwaysUseFullPath "alwaysUseFullPath"} property for details.
* For details on the pattern options, see the
* {@link org.springframework.util.AntPathMatcher} javadoc.
* matches (given "/test" -&gt; registered "/t*"). For details on the pattern
* options, see the {@link org.springframework.web.util.pattern.PathPattern}
* javadoc.
*
* @author Rod Johnson
* @author Juergen Hoeller

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -44,10 +44,9 @@ import org.springframework.util.CollectionUtils;
* with a slash, one is prepended.
*
* <p>Supports direct matches (given "/test" -&gt; registered "/test") and "*"
* pattern matches (given "/test" -&gt; registered "/t*"). Note that the default
* is to map within the current servlet mapping if applicable; see the
* {@link #setAlwaysUseFullPath "alwaysUseFullPath"} property. For details on the
* pattern options, see the {@link org.springframework.util.AntPathMatcher} javadoc.
* matches (given "/test" -&gt; registered "/t*"). For details on the pattern
* options, see the {@link org.springframework.web.util.pattern.PathPattern}
* javadoc.
* @author Rod Johnson
* @author Juergen Hoeller

View File

@ -27,6 +27,7 @@ import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockServletContext;
import org.springframework.web.util.UrlPathHelper;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@ -118,9 +119,13 @@ public class BeanNameUrlHandlerMappingTests {
@Test
public void requestsWithFullPaths() throws Exception {
UrlPathHelper pathHelper = new UrlPathHelper();
pathHelper.setAlwaysUseFullPath(true);
BeanNameUrlHandlerMapping hm = new BeanNameUrlHandlerMapping();
hm.setPatternParser(null); // the test targets AntPathPatcher-specific feature
hm.setAlwaysUseFullPath(true);
hm.setUrlPathHelper(pathHelper);
hm.setApplicationContext(wac);
Object bean = wac.getBean("godCtrl");

View File

@ -31,6 +31,7 @@ import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockServletContext;
import org.springframework.web.util.UrlPathHelper;
import org.springframework.web.util.WebUtils;
import static org.assertj.core.api.Assertions.assertThat;
@ -70,8 +71,10 @@ public class SimpleUrlHandlerMappingTests {
@Test
public void testNewlineInRequest() throws Exception {
Object controller = new Object();
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setUrlDecode(false);
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping(Collections.singletonMap("/*/baz", controller));
mapping.setUrlDecode(false);
mapping.setUrlPathHelper(urlPathHelper);
mapping.setApplicationContext(new StaticApplicationContext());
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo%0a%0dbar/baz");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-202 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -57,7 +57,6 @@ import org.springframework.web.servlet.handler.PathPatternsParameterizedTest;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.util.ServletRequestPathUtils;
import org.springframework.web.util.UrlPathHelper;
import org.springframework.web.util.pattern.PathPatternParser;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@ -75,10 +74,12 @@ class RequestMappingInfoHandlerMappingTests {
TestController controller = new TestController();
TestRequestMappingInfoHandlerMapping mapping1 = new TestRequestMappingInfoHandlerMapping();
mapping1.setPatternParser(new PathPatternParser());
UrlPathHelper pathHelper = new UrlPathHelper();
pathHelper.setRemoveSemicolonContent(false);
TestRequestMappingInfoHandlerMapping mapping2 = new TestRequestMappingInfoHandlerMapping();
mapping2.setRemoveSemicolonContent(false);
mapping2.setUrlPathHelper(pathHelper);
return Stream.of(mapping1, mapping2).peek(mapping -> {
mapping.setApplicationContext(new StaticWebApplicationContext());

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -51,7 +51,6 @@ import org.springframework.web.servlet.handler.PathPatternsParameterizedTest;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.util.ServletRequestPathUtils;
import org.springframework.web.util.pattern.PathPatternParser;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@ -78,10 +77,10 @@ class CrossOriginTests {
wac.refresh();
TestRequestMappingInfoHandlerMapping mapping1 = new TestRequestMappingInfoHandlerMapping();
mapping1.setPatternParser(new PathPatternParser());
wac.getAutowireCapableBeanFactory().initializeBean(mapping1, "mapping1");
TestRequestMappingInfoHandlerMapping mapping2 = new TestRequestMappingInfoHandlerMapping();
mapping2.setPatternParser(null);
wac.getAutowireCapableBeanFactory().initializeBean(mapping2, "mapping2");
wac.close();

View File

@ -69,11 +69,12 @@ public class RequestMappingHandlerMappingTests {
static Stream<Arguments> pathPatternsArguments() {
RequestMappingHandlerMapping mapping1 = new RequestMappingHandlerMapping();
StaticWebApplicationContext wac1 = new StaticWebApplicationContext();
mapping1.setPatternParser(new PathPatternParser());
mapping1.setApplicationContext(wac1);
RequestMappingHandlerMapping mapping2 = new RequestMappingHandlerMapping();
StaticWebApplicationContext wac2 = new StaticWebApplicationContext();
RequestMappingHandlerMapping mapping2 = new RequestMappingHandlerMapping();
mapping2.setPatternParser(null);
mapping2.setApplicationContext(wac2);
return Stream.of(Arguments.of(mapping1, wac1), Arguments.of(mapping2, wac2));

View File

@ -4,15 +4,11 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="urlMapping1" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="patternParser" >
<bean class="org.springframework.web.util.pattern.PathPatternParser"/>
</property>
<property name="mappings"><ref bean="mappings"/></property>
</bean>
<bean id="urlMapping2" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="patternParser"><null/></property>
<property name="urlDecode"><value>true</value></property>
<property name="mappings"><ref bean="mappings"/></property>
</bean>