#1886 - Avoid trailing slash for links pointing to empty mapping path.

This commit is contained in:
Oliver Drotbohm
2023-01-12 08:52:28 +01:00
parent 9bd19f74df
commit 698ec7cf1c
2 changed files with 24 additions and 2 deletions

View File

@@ -207,7 +207,7 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
* @return
*/
private static String join(String typeMapping, String mapping) {
return typeMapping.concat("/").concat(mapping);
return mapping.isBlank() ? typeMapping : typeMapping.concat("/").concat(mapping);
}
/**

View File

@@ -748,7 +748,18 @@ class WebMvcLinkBuilderUnitTest extends TestUtils {
linkTo(methodOn(ControllerWithPathVariableCatchAll.class).test("first", it[0])).withSelfRel().getHref())
.endsWith(it[1]);
});
}
@Test // #1886
void doesNotAppendTrailingSlashForEmptyMapping() {
var controller = methodOn(PersonController.class);
assertThat(linkTo(controller.getMappingWithoutPath()).toString())
.isEqualTo("http://localhost/people");
assertThat(linkTo(controller.getMappingWithEmptyPath()).toString())
.isEqualTo("http://localhost/people");
}
private static UriComponents toComponents(Link link) {
@@ -760,7 +771,18 @@ class WebMvcLinkBuilderUnitTest extends TestUtils {
}
@RequestMapping("/people")
interface PersonController {}
interface PersonController {
@GetMapping
default Object getMappingWithoutPath() {
return null;
}
@GetMapping("")
default Object getMappingWithEmptyPath() {
return null;
}
}
class PersonControllerImpl implements PersonController {}