From e3e404f9f942fc50226c032608e90e78d86d7fd6 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Mon, 23 Apr 2018 13:48:42 +0200 Subject: [PATCH] Fix optional ContentNegotiationStrategy for Actuator Since https://jira.spring.io/browse/SPR-16624, the contract for `ContentNegotiationStrategy` has been refined and should never return an empty list if it's got no preference for a media type, but it should rather respond with a `"*/*"` instead. This commit fixes the `OptionalPathExtensionContentNegotiationStrategy` defined in the Spring MVC auto-configuration to have that behavior. Fixes gh-12900 --- .../web/servlet/WebMvcAutoConfiguration.java | 2 +- .../servlet/WebMvcAutoConfigurationTests.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java index 03338d2844..cb6dc3fc01 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java @@ -653,7 +653,7 @@ public class WebMvcAutoConfiguration { Object skip = webRequest.getAttribute(SKIP_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); if (skip != null && Boolean.parseBoolean(skip.toString())) { - return Collections.emptyList(); + return MEDIA_TYPE_ALL_LIST; } return this.delegate.resolveMediaTypes(webRequest); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java index 365b2637b7..a024aab3f8 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java @@ -55,6 +55,7 @@ import org.springframework.core.io.Resource; import org.springframework.format.support.FormattingConversionService; import org.springframework.http.CacheControl; import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.test.util.ReflectionTestUtils; @@ -62,8 +63,11 @@ import org.springframework.util.StringUtils; import org.springframework.validation.Validator; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.web.accept.ContentNegotiationManager; +import org.springframework.web.accept.ContentNegotiationStrategy; import org.springframework.web.accept.ParameterContentNegotiationStrategy; +import org.springframework.web.accept.PathExtensionContentNegotiationStrategy; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; +import org.springframework.web.context.request.ServletWebRequest; import org.springframework.web.filter.HttpPutFormContentFilter; import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerExceptionResolver; @@ -822,6 +826,20 @@ public class WebMvcAutoConfigurationTests { }); } + @Test + public void contentNegotiationStrategySkipsPathExtension() throws Exception { + ContentNegotiationStrategy delegate = mock(ContentNegotiationStrategy.class); + ContentNegotiationStrategy strategy = new WebMvcAutoConfiguration + .OptionalPathExtensionContentNegotiationStrategy(delegate); + + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setAttribute(PathExtensionContentNegotiationStrategy.class + .getName() + ".SKIP", Boolean.TRUE); + ServletWebRequest webRequest = new ServletWebRequest(request); + List mediaTypes = strategy.resolveMediaTypes(webRequest); + assertThat(mediaTypes).containsOnly(MediaType.ALL); + } + private void assertCacheControl(AssertableWebApplicationContext context) { Map handlerMap = getHandlerMap( context.getBean("resourceHandlerMapping", HandlerMapping.class));