Consistent annotation lookup in MvcUriComponentsBuilder

Previously, a UriComponents build based on a method would require the
given method to be the annotated one as method parameter resolution only
applied locally. This was a problem when a controller was specified on
a method whose mapping is defined in a parent.

This commit harmonies the lookup using AnnotatedMethod who provides
support for a synthesized method parameter that takes annotations from
parent parameter into account.

Closes gh-32553
This commit is contained in:
Stéphane Nicoll
2024-04-19 14:24:16 +02:00
parent d4ddbd537b
commit 4d34444a69
2 changed files with 38 additions and 11 deletions

View File

@@ -42,6 +42,7 @@ import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
@@ -334,6 +335,14 @@ public class MvcUriComponentsBuilderTests {
assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/something/custom/1/foo");
}
@Test
void fromMethodNameWithAnnotationsOnInterface() {
initWebApplicationContext(WebConfig.class);
UriComponents uriComponents = fromMethodName(HelloController.class, "get", "test").build();
assertThat(uriComponents.toString()).isEqualTo("http://localhost/hello/test");
}
@Test
void fromMethodCallOnSubclass() {
UriComponents uriComponents = fromMethodCall(on(ExtendedController.class).myMethod(null)).build();
@@ -855,4 +864,20 @@ public class MvcUriComponentsBuilderTests {
}
}
interface HelloInterface {
@GetMapping("/hello/{name}")
ResponseEntity<String> get(@PathVariable String name);
}
@Controller
static class HelloController implements HelloInterface {
@Override
public ResponseEntity<String> get(String name) {
return ResponseEntity.ok("Hello " + name);
}
}
}