#269 - Make sure URL parts are joined by exactly one slash.

Original pull request: #423.
This commit is contained in:
Kevin Conaway
2016-01-08 09:32:55 -05:00
committed by Oliver Gierke
parent 792b5fcd22
commit 720ff797de
2 changed files with 52 additions and 1 deletions

View File

@@ -109,7 +109,17 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
return typeMapping;
}
return typeMapping == null || "/".equals(typeMapping) ? mapping[0] : typeMapping + mapping[0];
return typeMapping == null || "/".equals(typeMapping) ? mapping[0] : join(typeMapping, mapping[0]);
}
private String join(String typeMapping, String mapping) {
StringBuilder builder = new StringBuilder();
builder.append(typeMapping.endsWith("/") ? typeMapping.substring(0, typeMapping.length() - 1) : typeMapping);
builder.append('/');
builder.append(mapping.startsWith("/") ? mapping.substring(1) : mapping);
return builder.toString();
}
private String[] getMappingFrom(Annotation annotation) {

View File

@@ -21,7 +21,9 @@ import static org.junit.Assert.*;
import java.lang.reflect.Method;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* Unit tests for {@link AnnotationMappingDiscoverer}.
@@ -97,6 +99,25 @@ public class AnnotationMappingDiscovererUnitTest {
assertThat(discoverer.getMapping(ChildWithTypeMapping.class, method), is("/child/parent"));
}
/**
* @see #269
*/
@Test
public void handlesSlashes() throws Exception {
Method method = ControllerWithoutSlashes.class.getMethod("noslash");
assertThat(discoverer.getMapping(method), is("slashes/noslash"));
method = ControllerWithoutSlashes.class.getMethod("withslash");
assertThat(discoverer.getMapping(method), is("slashes/withslash"));
method = ControllerWithTrailingSlashes.class.getMethod("noslash");
assertThat(discoverer.getMapping(method), is("trailing/noslash"));
method = ControllerWithTrailingSlashes.class.getMethod("withslash");
assertThat(discoverer.getMapping(method), is("trailing/withslash"));
}
@RequestMapping("/type")
interface MyController {
@@ -139,4 +160,24 @@ public class AnnotationMappingDiscovererUnitTest {
@RequestMapping("/child")
interface ChildWithTypeMapping extends ParentWithMethod {}
@RequestMapping("slashes")
interface ControllerWithoutSlashes {
@RequestMapping("noslash")
void noslash();
@RequestMapping("/withslash")
void withslash();
}
@RequestMapping("trailing/")
interface ControllerWithTrailingSlashes {
@RequestMapping("noslash")
void noslash();
@RequestMapping("/withslash")
void withslash();
}
}