Skip to content

Commit

Permalink
Fix issue with path matching options
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev authored and pull[bot] committed Nov 6, 2019
1 parent 50b75ba commit baae184
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handler
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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handler
}
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

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;
Expand Down Expand Up @@ -122,10 +123,12 @@ public void useSuffixPatternMatch() {
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
Expand Down Expand Up @@ -153,12 +156,32 @@ public void pathPrefix() throws NoSuchMethodException {
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
Expand Down

0 comments on commit baae184

Please sign in to comment.