Enable use of parsed patterns by default in Spring MVC

Closes gh-28607
This commit is contained in:
rstoyanchev
2022-06-28 20:37:14 +01:00
parent 8a9b082d8a
commit 92cf1e13e8
16 changed files with 234 additions and 146 deletions

View File

@@ -37,6 +37,7 @@ import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.MappingMatch;
import jakarta.validation.constraints.NotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -137,6 +138,7 @@ import org.springframework.web.servlet.view.groovy.GroovyMarkupConfigurer;
import org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver;
import org.springframework.web.servlet.view.script.ScriptTemplateConfigurer;
import org.springframework.web.servlet.view.script.ScriptTemplateViewResolver;
import org.springframework.web.testfixture.servlet.MockHttpServletMapping;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
import org.springframework.web.testfixture.servlet.MockRequestDispatcher;
@@ -688,6 +690,7 @@ public class MvcNamespaceTests {
request.setRequestURI("/myapp/app/");
request.setContextPath("/myapp");
request.setServletPath("/app/");
request.setHttpServletMapping(new MockHttpServletMapping("", "", "", MappingMatch.PATH));
chain = mapping2.getHandler(request);
assertThat(chain.getInterceptorList().size()).isEqualTo(4);
assertThat(chain.getInterceptorList().get(1) instanceof ConversionServiceExposingInterceptor).isTrue();

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.
@@ -181,6 +181,7 @@ public class DelegatingWebMvcConfigurationIntegrationTests {
this.context = webContext;
}
@Configuration
static class ViewControllerConfiguration implements WebMvcConfigurer {
@@ -188,8 +189,16 @@ public class DelegatingWebMvcConfigurationIntegrationTests {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/test");
}
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
// tests need to check the "mvcPathMatcher" and "mvcUrlPathHelper" instances
configurer.setPatternParser(null);
}
}
@Configuration
static class ResourceHandlerConfiguration implements WebMvcConfigurer {
@@ -197,5 +206,13 @@ public class DelegatingWebMvcConfigurationIntegrationTests {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**");
}
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
// tests need to check the "mvcPathMatcher" and "mvcUrlPathHelper" instances
configurer.setPatternParser(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.
@@ -37,8 +37,6 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
*/
public class BeanNameUrlHandlerMappingTests {
public static final String CONF = "/org/springframework/web/servlet/handler/map1.xml";
private ConfigurableWebApplicationContext wac;
@@ -47,7 +45,7 @@ public class BeanNameUrlHandlerMappingTests {
MockServletContext sc = new MockServletContext("");
wac = new XmlWebApplicationContext();
wac.setServletContext(sc);
wac.setConfigLocations(new String[] {CONF});
wac.setConfigLocations("/org/springframework/web/servlet/handler/map1.xml");
wac.refresh();
}
@@ -55,7 +53,7 @@ public class BeanNameUrlHandlerMappingTests {
public void requestsWithoutHandlers() throws Exception {
HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping");
MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/nonsense.html");
MockHttpServletRequest req = new MockHttpServletRequest("GET", "/myapp/mypath/nonsense.html");
req.setContextPath("/myapp");
Object h = hm.getHandler(req);
assertThat(h == null).as("Handler is null").isTrue();
@@ -121,6 +119,7 @@ public class BeanNameUrlHandlerMappingTests {
@Test
public void requestsWithFullPaths() throws Exception {
BeanNameUrlHandlerMapping hm = new BeanNameUrlHandlerMapping();
hm.setPatternParser(null); // the test targets AntPathPatcher-specific feature
hm.setAlwaysUseFullPath(true);
hm.setApplicationContext(wac);
Object bean = wac.getBean("godCtrl");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 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.
@@ -29,7 +29,6 @@ import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
import org.springframework.web.testfixture.servlet.MockServletConfig;
import org.springframework.web.util.pattern.PathPatternParser;
import static org.assertj.core.api.Assertions.assertThat;
@@ -97,9 +96,8 @@ public abstract class AbstractServletHandlerMethodTests {
}
BeanDefinition mappingDef = wac.getBeanDefinition("handlerMapping");
if (usePathPatterns && !mappingDef.hasAttribute("patternParser")) {
BeanDefinition parserDef = register("parser", PathPatternParser.class, wac);
mappingDef.getPropertyValues().add("patternParser", parserDef);
if (!usePathPatterns) {
mappingDef.getPropertyValues().add("patternParser", null);
}
register("handlerAdapter", RequestMappingHandlerAdapter.class, wac);