From 14fd2605a39462f7ae3d0377aac249a171908a91 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Wed, 29 Jun 2022 13:03:10 +0100 Subject: [PATCH] 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 --- .../web/servlet/setup/StandaloneMockMvcBuilder.java | 8 +++++--- .../test/web/servlet/samples/spr/EncodedUriTests.java | 7 +++++-- .../web/servlet/handler/AbstractHandlerMapping.java | 9 ++++++--- .../web/servlet/handler/BeanNameUrlHandlerMapping.java | 10 ++++------ .../web/servlet/handler/SimpleUrlHandlerMapping.java | 9 ++++----- .../handler/BeanNameUrlHandlerMappingTests.java | 7 ++++++- .../servlet/handler/SimpleUrlHandlerMappingTests.java | 5 ++++- .../method/RequestMappingInfoHandlerMappingTests.java | 9 +++++---- .../mvc/method/annotation/CrossOriginTests.java | 5 ++--- .../annotation/RequestMappingHandlerMappingTests.java | 5 +++-- .../org/springframework/web/servlet/handler/map3.xml | 4 ---- 11 files changed, 44 insertions(+), 34 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java index c1661584f1..a03bf1dc11 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java @@ -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 AbstractMockMvcBuilderNote: 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 *

Note: 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 *

Note: 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) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/BeanNameUrlHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/BeanNameUrlHandlerMapping.java index 4a22560382..772489eb73 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/BeanNameUrlHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/BeanNameUrlHandlerMapping.java @@ -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. * *

Supports direct matches (given "/test" -> registered "/test") and "*" - * matches (given "/test" -> 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" -> registered "/t*"). For details on the pattern + * options, see the {@link org.springframework.web.util.pattern.PathPattern} + * javadoc. * * @author Rod Johnson * @author Juergen Hoeller diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMapping.java index 8e37bbf337..9a61f1d98e 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMapping.java @@ -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. * *

Supports direct matches (given "/test" -> registered "/test") and "*" - * pattern matches (given "/test" -> 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" -> registered "/t*"). For details on the pattern + * options, see the {@link org.springframework.web.util.pattern.PathPattern} + * javadoc. * @author Rod Johnson * @author Juergen Hoeller diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/BeanNameUrlHandlerMappingTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/BeanNameUrlHandlerMappingTests.java index 91f40c2740..61de1b0616 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/BeanNameUrlHandlerMappingTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/BeanNameUrlHandlerMappingTests.java @@ -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"); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMappingTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMappingTests.java index c47a5e0448..3da3bacd7c 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMappingTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMappingTests.java @@ -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"); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java index fb36283ffa..afe58ecd52 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java @@ -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()); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/CrossOriginTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/CrossOriginTests.java index 8a4fbb7f93..56d6801128 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/CrossOriginTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/CrossOriginTests.java @@ -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(); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMappingTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMappingTests.java index 9ed74e0781..4240823457 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMappingTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMappingTests.java @@ -69,11 +69,12 @@ public class RequestMappingHandlerMappingTests { static Stream 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)); diff --git a/spring-webmvc/src/test/resources/org/springframework/web/servlet/handler/map3.xml b/spring-webmvc/src/test/resources/org/springframework/web/servlet/handler/map3.xml index ff8552c57c..8d3e62ed04 100644 --- a/spring-webmvc/src/test/resources/org/springframework/web/servlet/handler/map3.xml +++ b/spring-webmvc/src/test/resources/org/springframework/web/servlet/handler/map3.xml @@ -4,15 +4,11 @@ xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> - - - - true