Refine empty path handling in MvcUriComponentsBuilder

Return "" from methods that obtain controller or method mapping to
avoid side effect of a trailing slash appearing.

Closes gh-29897
This commit is contained in:
rstoyanchev
2023-02-02 13:22:06 +00:00
parent 84bc1dfa89
commit 7851994a17
2 changed files with 30 additions and 10 deletions

View File

@@ -234,12 +234,19 @@ public class MvcUriComponentsBuilderTests {
}
@Test
public void fromMethodNameNotMapped() {
UriComponents uriComponents = fromMethodName(UnmappedController.class, "unmappedMethod").build();
public void fromMethodNameInUnmappedController() {
UriComponents uriComponents = fromMethodName(UnmappedController.class, "requestMappingMethod").build();
assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/");
}
@Test // gh-29897
public void fromMethodNameInUnmappedControllerMethod() {
UriComponents uriComponents = fromMethodName(UnmappedControllerMethod.class, "getMethod").build();
assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/path");
}
@Test
public void fromMethodNameWithCustomBaseUrlViaStaticCall() {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://example.org:9090/base");
@@ -535,7 +542,16 @@ public class MvcUriComponentsBuilderTests {
private class UnmappedController {
@RequestMapping
public void unmappedMethod() {
public void requestMappingMethod() {
}
}
@RequestMapping("/path")
private class UnmappedControllerMethod {
@GetMapping
public void getMethod() {
}
}