Skip to content

Commit 8070108

Browse files
committed
Set favorPathExtension to false by default
Applies a change that was intended in #23915 but wasn't. Closes gh-26119
1 parent 3612990 commit 8070108

File tree

8 files changed

+64
-33
lines changed

8 files changed

+64
-33
lines changed

spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public class ContentNegotiationManagerFactoryBean
108108

109109
private String parameterName = "format";
110110

111-
private boolean favorPathExtension = true;
111+
private boolean favorPathExtension = false;
112112

113113
private Map<String, MediaType> mediaTypes = new HashMap<>();
114114

spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ void defaultSettings() throws Exception {
7272
this.servletRequest.setRequestURI("/flower.gif");
7373

7474
assertThat(manager.resolveMediaTypes(this.webRequest))
75-
.as("Should be able to resolve file extensions by default")
76-
.isEqualTo(Collections.singletonList(MediaType.IMAGE_GIF));
75+
.as("Should not resolve file extensions by default")
76+
.containsExactly(MediaType.ALL);
7777

7878
this.servletRequest.setRequestURI("/flower.foobarbaz");
7979

@@ -226,6 +226,7 @@ void fileExtensions() {
226226
@Test
227227
void ignoreAcceptHeader() throws Exception {
228228
this.factoryBean.setIgnoreAcceptHeader(true);
229+
this.factoryBean.setFavorParameter(true);
229230
this.factoryBean.afterPropertiesSet();
230231
ContentNegotiationManager manager = this.factoryBean.getObject();
231232

spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ public void testDefaultConfig() throws Exception {
206206
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json");
207207
NativeWebRequest webRequest = new ServletWebRequest(request);
208208
ContentNegotiationManager manager = mapping.getContentNegotiationManager();
209-
assertThat(manager.resolveMediaTypes(webRequest)).isEqualTo(Collections.singletonList(MediaType.APPLICATION_JSON));
209+
assertThat(manager.resolveMediaTypes(webRequest))
210+
.as("Should not resolve file extensions by default")
211+
.containsExactly(MediaType.ALL);
210212

211213
RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class);
212214
assertThat(adapter).isNotNull();
@@ -743,13 +745,17 @@ public void testContentNegotiationManager() throws Exception {
743745
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
744746
ContentNegotiationManager manager = mapping.getContentNegotiationManager();
745747

746-
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.xml");
748+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
749+
request.setParameter("format", "xml");
747750
NativeWebRequest webRequest = new ServletWebRequest(request);
748-
assertThat(manager.resolveMediaTypes(webRequest)).isEqualTo(Collections.singletonList(MediaType.valueOf("application/rss+xml")));
751+
assertThat(manager.resolveMediaTypes(webRequest))
752+
.containsExactly(MediaType.valueOf("application/rss+xml"));
749753

750754
ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
751755
assertThat(compositeResolver).isNotNull();
752-
assertThat(compositeResolver.getViewResolvers().size()).as("Actual: " + compositeResolver.getViewResolvers()).isEqualTo(1);
756+
assertThat(compositeResolver.getViewResolvers().size())
757+
.as("Actual: " + compositeResolver.getViewResolvers())
758+
.isEqualTo(1);
753759

754760
ViewResolver resolver = compositeResolver.getViewResolvers().get(0);
755761
assertThat(resolver.getClass()).isEqualTo(ContentNegotiatingViewResolver.class);

spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurerTests.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -59,26 +59,34 @@ public void defaultSettings() throws Exception {
5959

6060
this.servletRequest.setRequestURI("/flower.gif");
6161

62-
assertThat(manager.resolveMediaTypes(this.webRequest).get(0)).as("Should be able to resolve file extensions by default").isEqualTo(MediaType.IMAGE_GIF);
62+
assertThat(manager.resolveMediaTypes(this.webRequest))
63+
.as("Should not resolve file extensions by default")
64+
.containsExactly(MediaType.ALL);
6365

6466
this.servletRequest.setRequestURI("/flower?format=gif");
6567
this.servletRequest.addParameter("format", "gif");
6668

67-
assertThat(manager.resolveMediaTypes(this.webRequest)).as("Should not resolve request parameters by default").isEqualTo(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST);
69+
assertThat(manager.resolveMediaTypes(this.webRequest))
70+
.as("Should not resolve request parameters by default")
71+
.isEqualTo(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST);
6872

6973
this.servletRequest.setRequestURI("/flower");
7074
this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);
7175

72-
assertThat(manager.resolveMediaTypes(this.webRequest).get(0)).as("Should resolve Accept header by default").isEqualTo(MediaType.IMAGE_GIF);
76+
assertThat(manager.resolveMediaTypes(this.webRequest))
77+
.as("Should resolve Accept header by default")
78+
.containsExactly(MediaType.IMAGE_GIF);
7379
}
7480

7581
@Test
7682
public void addMediaTypes() throws Exception {
83+
this.configurer.favorParameter(true);
7784
this.configurer.mediaTypes(Collections.singletonMap("json", MediaType.APPLICATION_JSON));
7885
ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();
7986

80-
this.servletRequest.setRequestURI("/flower.json");
81-
assertThat(manager.resolveMediaTypes(this.webRequest).get(0)).isEqualTo(MediaType.APPLICATION_JSON);
87+
this.servletRequest.setRequestURI("/flower");
88+
this.servletRequest.addParameter("format", "json");
89+
assertThat(manager.resolveMediaTypes(this.webRequest)).containsExactly(MediaType.APPLICATION_JSON);
8290
}
8391

8492
@Test
@@ -97,6 +105,7 @@ public void favorParameter() throws Exception {
97105
@Test
98106
public void ignoreAcceptHeader() throws Exception {
99107
this.configurer.ignoreAcceptHeader(true);
108+
this.configurer.favorParameter(true);
100109
ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();
101110

102111
this.servletRequest.setRequestURI("/flower");

spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.springframework.core.io.FileSystemResourceLoader;
3434
import org.springframework.format.FormatterRegistry;
3535
import org.springframework.http.HttpStatus;
36-
import org.springframework.http.MediaType;
3736
import org.springframework.http.converter.HttpMessageConverter;
3837
import org.springframework.http.converter.StringHttpMessageConverter;
3938
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
@@ -91,7 +90,6 @@
9190
import static com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION;
9291
import static org.assertj.core.api.Assertions.assertThat;
9392
import static org.mockito.Mockito.mock;
94-
import static org.springframework.http.MediaType.APPLICATION_ATOM_XML;
9593
import static org.springframework.http.MediaType.APPLICATION_JSON;
9694
import static org.springframework.http.MediaType.APPLICATION_XML;
9795

@@ -268,34 +266,28 @@ public void webBindingInitializer() throws Exception {
268266
@Test
269267
@SuppressWarnings("deprecation")
270268
public void contentNegotiation() throws Exception {
271-
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json");
269+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
272270
NativeWebRequest webRequest = new ServletWebRequest(request);
273271

274272
RequestMappingHandlerMapping mapping = this.config.requestMappingHandlerMapping(
275273
this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(),
276274
this.config.mvcResourceUrlProvider());
275+
276+
request.setParameter("f", "json");
277277
ContentNegotiationManager manager = mapping.getContentNegotiationManager();
278278
assertThat(manager.resolveMediaTypes(webRequest)).isEqualTo(Collections.singletonList(APPLICATION_JSON));
279279

280-
request.setRequestURI("/foo.xml");
280+
request.setParameter("f", "xml");
281281
assertThat(manager.resolveMediaTypes(webRequest)).isEqualTo(Collections.singletonList(APPLICATION_XML));
282282

283-
request.setRequestURI("/foo.rss");
284-
assertThat(manager.resolveMediaTypes(webRequest)).isEqualTo(Collections.singletonList(MediaType.valueOf("application/rss+xml")));
285-
286-
request.setRequestURI("/foo.atom");
287-
assertThat(manager.resolveMediaTypes(webRequest)).isEqualTo(Collections.singletonList(APPLICATION_ATOM_XML));
288-
289-
request.setRequestURI("/foo");
290-
request.setParameter("f", "json");
291-
assertThat(manager.resolveMediaTypes(webRequest)).isEqualTo(Collections.singletonList(APPLICATION_JSON));
292-
293-
request.setRequestURI("/resources/foo.gif");
294283
SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) this.config.resourceHandlerMapping(
295284
this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(),
296285
this.config.mvcResourceUrlProvider());
297286
handlerMapping.setApplicationContext(this.context);
287+
288+
request = new MockHttpServletRequest("GET", "/resources/foo.gif");
298289
HandlerExecutionChain chain = handlerMapping.getHandler(request);
290+
299291
assertThat(chain).isNotNull();
300292
ResourceHttpRequestHandler handler = (ResourceHttpRequestHandler) chain.getHandler();
301293
assertThat(handler).isNotNull();

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,7 @@ void responseBodyAsHtml(boolean usePathPatterns) throws Exception {
17421742
}
17431743

17441744
ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
1745+
factoryBean.setFavorPathExtension(true);
17451746
factoryBean.afterPropertiesSet();
17461747

17471748
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
@@ -1773,6 +1774,7 @@ void responseBodyAsHtml(boolean usePathPatterns) throws Exception {
17731774
void responseBodyAsHtmlWithSuffixPresent(boolean usePathPatterns) throws Exception {
17741775
initDispatcherServlet(TextRestController.class, usePathPatterns, wac -> {
17751776
ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
1777+
factoryBean.setFavorPathExtension(true);
17761778
factoryBean.afterPropertiesSet();
17771779
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
17781780
adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
@@ -1833,14 +1835,22 @@ void responseBodyAsHtmlWithProducesCondition(boolean usePathPatterns) throws Exc
18331835
void responseBodyAsTextWithCssExtension(boolean usePathPatterns) throws Exception {
18341836
initDispatcherServlet(TextRestController.class, usePathPatterns, wac -> {
18351837
ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
1838+
factoryBean.setFavorParameter(true);
1839+
factoryBean.addMediaType("css", MediaType.parseMediaType("text/css"));
18361840
factoryBean.afterPropertiesSet();
1841+
1842+
RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
1843+
mappingDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
1844+
wac.registerBeanDefinition("handlerMapping", mappingDef);
1845+
18371846
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
18381847
adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
18391848
wac.registerBeanDefinition("handlerAdapter", adapterDef);
18401849
});
18411850

18421851
byte[] content = "body".getBytes(StandardCharsets.ISO_8859_1);
1843-
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a4.css");
1852+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a4");
1853+
request.addParameter("format", "css");
18441854
request.setContent(content);
18451855
MockHttpServletResponse response = new MockHttpServletResponse();
18461856

@@ -3826,7 +3836,7 @@ public String a3(@RequestBody String body) throws IOException {
38263836
return body;
38273837
}
38283838

3829-
@RequestMapping(path = "/a4.css", method = RequestMethod.GET)
3839+
@RequestMapping(path = "/a4", method = RequestMethod.GET)
38303840
public String a4(@RequestBody String body) {
38313841
return body;
38323842
}

spring-webmvc/src/test/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolverTests.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
3535
import org.springframework.web.accept.MappingMediaTypeFileExtensionResolver;
3636
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
37+
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
3738
import org.springframework.web.context.request.RequestContextHolder;
3839
import org.springframework.web.context.request.ServletRequestAttributes;
3940
import org.springframework.web.context.support.StaticWebApplicationContext;
@@ -85,9 +86,16 @@ public void getMediaTypeAcceptHeaderWithProduces() throws Exception {
8586

8687
@Test
8788
public void resolveViewNameWithPathExtension() throws Exception {
88-
request.setRequestURI("/test.xls");
89+
request.setRequestURI("/test");
90+
request.setParameter("format", "xls");
91+
92+
String mediaType = "application/vnd.ms-excel";
93+
ContentNegotiationManager manager = new ContentNegotiationManager(
94+
new ParameterContentNegotiationStrategy(
95+
Collections.singletonMap("xls", MediaType.parseMediaType(mediaType))));
8996

9097
ViewResolver viewResolverMock = mock(ViewResolver.class);
98+
viewResolver.setContentNegotiationManager(manager);
9199
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
92100
viewResolver.afterPropertiesSet();
93101

@@ -98,7 +106,7 @@ public void resolveViewNameWithPathExtension() throws Exception {
98106

99107
given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(null);
100108
given(viewResolverMock.resolveViewName(viewName + ".xls", locale)).willReturn(viewMock);
101-
given(viewMock.getContentType()).willReturn("application/vnd.ms-excel");
109+
given(viewMock.getContentType()).willReturn(mediaType);
102110

103111
View result = viewResolver.resolveViewName(viewName, locale);
104112
assertThat(result).as("Invalid view").isSameAs(viewMock);
@@ -307,8 +315,12 @@ public void resolveViewNameAcceptHeaderDefaultView() throws Exception {
307315
public void resolveViewNameFilename() throws Exception {
308316
request.setRequestURI("/test.html");
309317

318+
ContentNegotiationManager manager =
319+
new ContentNegotiationManager(new PathExtensionContentNegotiationStrategy());
320+
310321
ViewResolver viewResolverMock1 = mock(ViewResolver.class, "viewResolver1");
311322
ViewResolver viewResolverMock2 = mock(ViewResolver.class, "viewResolver2");
323+
viewResolver.setContentNegotiationManager(manager);
312324
viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
313325

314326
viewResolver.afterPropertiesSet();
@@ -336,7 +348,7 @@ public void resolveViewNameFilenameDefaultView() throws Exception {
336348
request.setRequestURI("/test.json");
337349

338350
Map<String, MediaType> mapping = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
339-
org.springframework.web.accept.PathExtensionContentNegotiationStrategy pathStrategy = new org.springframework.web.accept.PathExtensionContentNegotiationStrategy(mapping);
351+
PathExtensionContentNegotiationStrategy pathStrategy = new PathExtensionContentNegotiationStrategy(mapping);
340352
viewResolver.setContentNegotiationManager(new ContentNegotiationManager(pathStrategy));
341353

342354
ViewResolver viewResolverMock1 = mock(ViewResolver.class);

spring-webmvc/src/test/resources/org/springframework/web/servlet/config/mvc-config-content-negotiation-manager.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
</mvc:view-resolvers>
1515

1616
<bean id="mvcContentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
17+
<property name="favorParameter" value="true"/>
1718
<property name="mediaTypes">
1819
<value>
1920
xml=application/rss+xml

0 commit comments

Comments
 (0)