Fix issue with path matching options

Closes gh-23907
This commit is contained in:
Rossen Stoyanchev 2019-11-06 17:39:41 +00:00
parent 3a241b546f
commit 203977972b
3 changed files with 29 additions and 6 deletions

View File

@ -163,7 +163,7 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
if (this.embeddedValueResolver != null) { if (this.embeddedValueResolver != null) {
prefix = this.embeddedValueResolver.resolveStringValue(prefix); prefix = this.embeddedValueResolver.resolveStringValue(prefix);
} }
info = RequestMappingInfo.paths(prefix).build().combine(info); info = RequestMappingInfo.paths(prefix).options(this.config).build().combine(info);
break; break;
} }
} }

View File

@ -232,7 +232,7 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
} }
String prefix = getPathPrefix(handlerType); String prefix = getPathPrefix(handlerType);
if (prefix != null) { if (prefix != null) {
info = RequestMappingInfo.paths(prefix).build().combine(info); info = RequestMappingInfo.paths(prefix).options(this.config).build().combine(info);
} }
} }
return info; return info;

View File

@ -32,6 +32,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.core.annotation.AliasFor; import org.springframework.core.annotation.AliasFor;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.accept.ContentNegotiationManager;
@ -122,10 +123,12 @@ public class RequestMappingHandlerMappingTests {
assertThat(this.handlerMapping.useSuffixPatternMatch()).isFalse(); assertThat(this.handlerMapping.useSuffixPatternMatch()).isFalse();
this.handlerMapping.setUseRegisteredSuffixPatternMatch(false); 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); 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 @Test
@ -153,12 +156,32 @@ public class RequestMappingHandlerMappingTests {
assertThat(info.getPatternsCondition().getPatterns()).isEqualTo(Collections.singleton("/api/user/{id}")); 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 @Test
public void resolveRequestMappingViaComposedAnnotation() throws Exception { public void resolveRequestMappingViaComposedAnnotation() throws Exception {
RequestMappingInfo info = assertComposedAnnotationMapping("postJson", "/postJson", RequestMethod.POST); RequestMappingInfo info = assertComposedAnnotationMapping("postJson", "/postJson", RequestMethod.POST);
assertThat(info.getConsumesCondition().getConsumableMediaTypes().iterator().next().toString()).isEqualTo(MediaType.APPLICATION_JSON_VALUE); assertThat(info.getConsumesCondition().getConsumableMediaTypes().iterator().next().toString())
assertThat(info.getProducesCondition().getProducibleMediaTypes().iterator().next().toString()).isEqualTo(MediaType.APPLICATION_JSON_VALUE); .isEqualTo(MediaType.APPLICATION_JSON_VALUE);
assertThat(info.getProducesCondition().getProducibleMediaTypes().iterator().next().toString())
.isEqualTo(MediaType.APPLICATION_JSON_VALUE);
} }
@Test // SPR-14988 @Test // SPR-14988