Skip to content

Commit c1ad943

Browse files
committed
Use ConversionService from Environment in PropertySourcesPlaceholderConfigurer
This commit fixes a regression in PropertySourcesPlaceholderConfigurer that was introduced in Spring Framework 6.2.7. See spring-projectsgh-34861 Closes spring-projectsgh-34936
1 parent 53844b0 commit c1ad943

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

spring-context/src/main/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2525
import org.springframework.beans.factory.config.PlaceholderConfigurerSupport;
2626
import org.springframework.context.EnvironmentAware;
27+
import org.springframework.core.convert.ConversionService;
28+
import org.springframework.core.convert.support.DefaultConversionService;
2729
import org.springframework.core.env.ConfigurableEnvironment;
2830
import org.springframework.core.env.ConfigurablePropertyResolver;
2931
import org.springframework.core.env.Environment;
@@ -247,12 +249,33 @@ public Object getProperty(String name) {
247249
for (PropertySource<?> propertySource : super.source.getPropertySources()) {
248250
Object candidate = propertySource.getProperty(name);
249251
if (candidate != null) {
250-
return candidate;
252+
return convertToString(candidate);
251253
}
252254
}
253255
return null;
254256
}
255257

258+
/**
259+
* Convert the supplied value to a {@link String} using the {@link ConversionService}
260+
* from the {@link Environment}.
261+
* <p>This is a modified version of
262+
* {@link org.springframework.core.env.AbstractPropertyResolver#convertValueIfNecessary(Object, Class)}.
263+
* @param value the value to convert
264+
* @return the converted value, or the original value if no conversion is necessary
265+
* @since 6.2.8
266+
*/
267+
@Nullable
268+
private String convertToString(Object value) {
269+
if (value instanceof String string) {
270+
return string;
271+
}
272+
ConversionService conversionService = super.source.getConversionService();
273+
if (conversionService == null) {
274+
conversionService = DefaultConversionService.getSharedInstance();
275+
}
276+
return conversionService.convert(value, String.class);
277+
}
278+
256279
@Override
257280
public String toString() {
258281
return "ConfigurableEnvironmentPropertySource {propertySources=" + super.source.getPropertySources() + "}";

spring-context/src/test/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurerTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import org.junit.jupiter.api.AfterAll;
2727
import org.junit.jupiter.api.BeforeEach;
28-
import org.junit.jupiter.api.Disabled;
2928
import org.junit.jupiter.api.Nested;
3029
import org.junit.jupiter.api.Test;
3130
import org.junit.jupiter.params.ParameterizedTest;
@@ -98,7 +97,6 @@ void replacementFromEnvironmentProperties() {
9897
* used by the {@code Environment} is applied during placeholder resolution
9998
* against a {@link PropertySource} registered in the {@code Environment}.
10099
*/
101-
@Disabled("Disabled until gh-34936 is resolved")
102100
@Test // gh-34936
103101
void replacementFromEnvironmentPropertiesWithConversion() {
104102
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();

0 commit comments

Comments
 (0)