Skip to content

Commit 7ad01a9

Browse files
committed
Use MethodInvocationInfo class loader in case of JDK platform loader as well
Closes gh-30210 (cherry picked from commit 491ae1e)
1 parent 5d6d653 commit 7ad01a9

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,9 @@ private static <T> T initProxy(
777777

778778
else if (controllerType.isInterface()) {
779779
ClassLoader classLoader = controllerType.getClassLoader();
780-
if (classLoader == null) { // JDK interface type from bootstrap loader
780+
if (classLoader == null || classLoader.getParent() == null) {
781+
// JDK interface type from bootstrap loader or platform loader ->
782+
// use higher-level loader which can see Spring infrastructure classes
781783
classLoader = MethodInvocationInfo.class.getClassLoader();
782784
}
783785
Class<?>[] ifcs = new Class<?>[] {controllerType, MethodInvocationInfo.class};

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilderTests.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.annotation.Retention;
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.lang.annotation.Target;
24+
import java.sql.Savepoint;
2425
import java.util.Arrays;
2526
import java.util.List;
2627
import java.util.Optional;
@@ -378,14 +379,6 @@ public void fromMethodCallWithObjectReturnType() {
378379
assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
379380
}
380381

381-
@Test // SPR-16710
382-
public void fromMethodCallWithCharSequenceReturnType() {
383-
UriComponents uriComponents = fromMethodCall(
384-
on(BookingControllerWithCharSequence.class).getBooking(21L)).buildAndExpand(42);
385-
386-
assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
387-
}
388-
389382
@Test // SPR-16710
390383
public void fromMethodCallWithStringReturnType() {
391384
assertThatIllegalStateException().isThrownBy(() -> {
@@ -403,6 +396,22 @@ public void fromMethodNameWithStringReturnType() {
403396
assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
404397
}
405398

399+
@Test // gh-30210
400+
public void fromMethodCallWithCharSequenceReturnType() {
401+
UriComponents uriComponents = fromMethodCall(
402+
on(BookingControllerWithCharSequence.class).getBooking(21L)).buildAndExpand(42);
403+
404+
assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
405+
}
406+
407+
@Test // gh-30210
408+
public void fromMethodCallWithJdbc30115ReturnType() {
409+
UriComponents uriComponents = fromMethodCall(
410+
on(BookingControllerWithJdbcSavepoint.class).getBooking(21L)).buildAndExpand(42);
411+
412+
assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
413+
}
414+
406415
@Test
407416
public void fromMappingNamePlain() {
408417
initWebApplicationContext(WebConfig.class);
@@ -693,6 +702,17 @@ public Object getBooking(@PathVariable Long booking) {
693702
}
694703

695704

705+
@Controller
706+
@RequestMapping("/hotels/{hotel}")
707+
static class BookingControllerWithString {
708+
709+
@GetMapping("/bookings/{booking}")
710+
public String getBooking(@PathVariable Long booking) {
711+
return "url";
712+
}
713+
}
714+
715+
696716
@Controller
697717
@RequestMapping("/hotels/{hotel}")
698718
static class BookingControllerWithCharSequence {
@@ -706,11 +726,11 @@ public CharSequence getBooking(@PathVariable Long booking) {
706726

707727
@Controller
708728
@RequestMapping("/hotels/{hotel}")
709-
static class BookingControllerWithString {
729+
static class BookingControllerWithJdbcSavepoint {
710730

711731
@GetMapping("/bookings/{booking}")
712-
public String getBooking(@PathVariable Long booking) {
713-
return "url";
732+
public Savepoint getBooking(@PathVariable Long booking) {
733+
return null;
714734
}
715735
}
716736

0 commit comments

Comments
 (0)