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 {
} }
private static String resolveEmbeddedValue(String value) { private static String resolveEmbeddedValue(String value) {
WebApplicationContext webApplicationContext = getWebApplicationContext(); if (hasPlaceholderOrExpression(value)) {
if (webApplicationContext != null && WebApplicationContext wac = getWebApplicationContext();
webApplicationContext.getAutowireCapableBeanFactory() instanceof ConfigurableBeanFactory cbf) { if (wac != null && wac.getAutowireCapableBeanFactory() instanceof ConfigurableBeanFactory cbf) {
EmbeddedValueResolver embeddedValueResolver = new EmbeddedValueResolver(cbf); EmbeddedValueResolver valueResolver = new EmbeddedValueResolver(cbf);
String resolvedEmbeddedValue = embeddedValueResolver.resolveStringValue(value); String resolvedValue = valueResolver.resolveStringValue(value);
if (resolvedEmbeddedValue != null) { if (resolvedValue != null) {
return resolvedEmbeddedValue; return resolvedValue;
}
} }
} }
return value; 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() { private static @Nullable WebApplicationContext getWebApplicationContext() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes == null) { 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 {
assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/something/custom/1/foo"); assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/something/custom/1/foo");
} }
@Test @Test // gh-35348
void fromMethodNameConfigurablePathSpEL() { void fromMethodNameConfigurablePathSpEL() {
try { try {
System.setProperty("customMapping", "custom"); System.setProperty("customMapping", "custom");
StandardEnvironment environment = new StandardEnvironment(); StandardEnvironment environment = new StandardEnvironment();
initWebApplicationContext(WebConfig.class, environment); initWebApplicationContext(WebConfig.class, environment);
UriComponents uriComponents = fromMethodName(ControllerWithMethods.class, UriComponents uric = fromMethodName(ControllerWithMethods.class, "methodWithSpEL", "1").build();
"methodWithConfigurableMappingThroughSpEL", "1").build(); assertThat(uric.toUriString()).isEqualTo("http://localhost/something/custom/1/foo");
assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/something/custom/1/foo");
} }
finally { finally {
System.clearProperty("customMapping"); System.clearProperty("customMapping");
} }
} }
@Test @Test
@ -721,7 +719,7 @@ public class MvcUriComponentsBuilderTests {
} }
@RequestMapping("/#{systemProperties.customMapping}/{id}/foo") @RequestMapping("/#{systemProperties.customMapping}/{id}/foo")
HttpEntity<Void> methodWithConfigurableMappingThroughSpEL(@PathVariable String id) { HttpEntity<Void> methodWithSpEL(@PathVariable String id) {
return null; return null;
} }
} }

Loading…
Cancel
Save