diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerMapping.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerMapping.java index c1b48d635b..e6e0751aba 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerMapping.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerMapping.java @@ -163,7 +163,7 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi if (this.embeddedValueResolver != null) { prefix = this.embeddedValueResolver.resolveStringValue(prefix); } - info = RequestMappingInfo.paths(prefix).build().combine(info); + info = RequestMappingInfo.paths(prefix).options(this.config).build().combine(info); break; } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java index 75c5481793..3dadc61a37 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java @@ -232,7 +232,7 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi } String prefix = getPathPrefix(handlerType); if (prefix != null) { - info = RequestMappingInfo.paths(prefix).build().combine(info); + info = RequestMappingInfo.paths(prefix).options(this.config).build().combine(info); } } return info; 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 9968cfe4a6..a1f5b19360 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 @@ -32,6 +32,7 @@ import org.junit.jupiter.api.Test; import org.springframework.core.annotation.AliasFor; import org.springframework.http.MediaType; +import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.util.ClassUtils; import org.springframework.web.accept.ContentNegotiationManager; @@ -122,10 +123,12 @@ public class RequestMappingHandlerMappingTests { assertThat(this.handlerMapping.useSuffixPatternMatch()).isFalse(); this.handlerMapping.setUseRegisteredSuffixPatternMatch(false); - assertThat(this.handlerMapping.useSuffixPatternMatch()).as("'false' registeredSuffixPatternMatch shouldn't impact suffixPatternMatch").isFalse(); + assertThat(this.handlerMapping.useSuffixPatternMatch()) + .as("'false' registeredSuffixPatternMatch shouldn't impact suffixPatternMatch").isFalse(); this.handlerMapping.setUseRegisteredSuffixPatternMatch(true); - assertThat(this.handlerMapping.useSuffixPatternMatch()).as("'true' registeredSuffixPatternMatch should enable suffixPatternMatch").isTrue(); + assertThat(this.handlerMapping.useSuffixPatternMatch()) + .as("'true' registeredSuffixPatternMatch should enable suffixPatternMatch").isTrue(); } @Test @@ -153,12 +156,32 @@ public class RequestMappingHandlerMappingTests { assertThat(info.getPatternsCondition().getPatterns()).isEqualTo(Collections.singleton("/api/user/{id}")); } + @Test // gh-23907 + public void pathPrefixPreservesPathMatchingSettings() throws NoSuchMethodException { + this.handlerMapping.setUseSuffixPatternMatch(false); + this.handlerMapping.setPathPrefixes(Collections.singletonMap("/api", HandlerTypePredicate.forAnyHandlerType())); + this.handlerMapping.afterPropertiesSet(); + + Method method = ComposedAnnotationController.class.getMethod("get"); + RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, ComposedAnnotationController.class); + + assertThat(info).isNotNull(); + + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/api/get"); + assertThat(info.getPatternsCondition().getMatchingCondition(request)).isNotNull(); + + request = new MockHttpServletRequest("GET", "/api/get.pdf"); + assertThat(info.getPatternsCondition().getMatchingCondition(request)).isNull(); + } + @Test public void resolveRequestMappingViaComposedAnnotation() throws Exception { RequestMappingInfo info = assertComposedAnnotationMapping("postJson", "/postJson", RequestMethod.POST); - assertThat(info.getConsumesCondition().getConsumableMediaTypes().iterator().next().toString()).isEqualTo(MediaType.APPLICATION_JSON_VALUE); - assertThat(info.getProducesCondition().getProducibleMediaTypes().iterator().next().toString()).isEqualTo(MediaType.APPLICATION_JSON_VALUE); + assertThat(info.getConsumesCondition().getConsumableMediaTypes().iterator().next().toString()) + .isEqualTo(MediaType.APPLICATION_JSON_VALUE); + assertThat(info.getProducesCondition().getProducibleMediaTypes().iterator().next().toString()) + .isEqualTo(MediaType.APPLICATION_JSON_VALUE); } @Test // SPR-14988