mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 16:39:29 +00:00
Polishing contribution
Closes gh-35348
This commit is contained in:
+20
-7
@@ -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) {
|
||||
|
||||
+4
-6
@@ -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 {
|
||||
}
|
||||
|
||||
@RequestMapping("/#{systemProperties.customMapping}/{id}/foo")
|
||||
HttpEntity<Void> methodWithConfigurableMappingThroughSpEL(@PathVariable String id) {
|
||||
HttpEntity<Void> methodWithSpEL(@PathVariable String id) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user