Browse Source

Polishing contribution

Closes gh-35348
pull/35641/head
rstoyanchev 2 months ago
parent
commit
43c13ca043
  1. 27
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java
  2. 10
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilderTests.java

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

@ -629,18 +629,31 @@ public class MvcUriComponentsBuilder { @@ -629,18 +629,31 @@ public class MvcUriComponentsBuilder {
}
private static String resolveEmbeddedValue(String value) {
WebApplicationContext webApplicationContext = getWebApplicationContext();
if (webApplicationContext != null &&
webApplicationContext.getAutowireCapableBeanFactory() instanceof ConfigurableBeanFactory cbf) {
EmbeddedValueResolver embeddedValueResolver = new EmbeddedValueResolver(cbf);
String resolvedEmbeddedValue = embeddedValueResolver.resolveStringValue(value);
if (resolvedEmbeddedValue != null) {
return resolvedEmbeddedValue;
if (hasPlaceholderOrExpression(value)) {
WebApplicationContext wac = getWebApplicationContext();
if (wac != null && wac.getAutowireCapableBeanFactory() instanceof ConfigurableBeanFactory cbf) {
EmbeddedValueResolver valueResolver = new EmbeddedValueResolver(cbf);
String resolvedValue = valueResolver.resolveStringValue(value);
if (resolvedValue != null) {
return resolvedValue;
}
}
}
return value;
}
private static boolean hasPlaceholderOrExpression(String value) {
char prev = 0;
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (c == '{' && (prev == '$' || prev == '#')) {
return true;
}
prev = c;
}
return false;
}
private static @Nullable WebApplicationContext getWebApplicationContext() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes == null) {

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

@ -335,20 +335,18 @@ public class MvcUriComponentsBuilderTests { @@ -335,20 +335,18 @@ public class MvcUriComponentsBuilderTests {
assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/something/custom/1/foo");
}
@Test
@Test // gh-35348
void fromMethodNameConfigurablePathSpEL() {
try {
System.setProperty("customMapping", "custom");
StandardEnvironment environment = new StandardEnvironment();
initWebApplicationContext(WebConfig.class, environment);
UriComponents uriComponents = fromMethodName(ControllerWithMethods.class,
"methodWithConfigurableMappingThroughSpEL", "1").build();
assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/something/custom/1/foo");
UriComponents uric = fromMethodName(ControllerWithMethods.class, "methodWithSpEL", "1").build();
assertThat(uric.toUriString()).isEqualTo("http://localhost/something/custom/1/foo");
}
finally {
System.clearProperty("customMapping");
}
}
@Test
@ -721,7 +719,7 @@ public class MvcUriComponentsBuilderTests { @@ -721,7 +719,7 @@ public class MvcUriComponentsBuilderTests {
}
@RequestMapping("/#{systemProperties.customMapping}/{id}/foo")
HttpEntity<Void> methodWithConfigurableMappingThroughSpEL(@PathVariable String id) {
HttpEntity<Void> methodWithSpEL(@PathVariable String id) {
return null;
}
}

Loading…
Cancel
Save