Skip to content

Commit e03117c

Browse files
committed
Merge branch '6.2.x'
2 parents b5d153f + 90be94a commit e03117c

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2727
import org.springframework.beans.factory.config.PlaceholderConfigurerSupport;
2828
import org.springframework.context.EnvironmentAware;
29+
import org.springframework.core.convert.ConversionService;
30+
import org.springframework.core.convert.support.DefaultConversionService;
2931
import org.springframework.core.env.ConfigurableEnvironment;
3032
import org.springframework.core.env.ConfigurablePropertyResolver;
3133
import org.springframework.core.env.Environment;
@@ -240,16 +242,37 @@ public boolean containsProperty(String name) {
240242
}
241243

242244
@Override
243-
public @Nullable Object getProperty(String name) {
245+
// Declare String as covariant return type, since a String is actually required.
246+
public @Nullable String getProperty(String name) {
244247
for (PropertySource<?> propertySource : super.source.getPropertySources()) {
245248
Object candidate = propertySource.getProperty(name);
246249
if (candidate != null) {
247-
return candidate;
250+
return convertToString(candidate);
248251
}
249252
}
250253
return null;
251254
}
252255

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

277300
@Override
278-
public @Nullable Object getProperty(String name) {
301+
// Declare String as covariant return type, since a String is actually required.
302+
public @Nullable String getProperty(String name) {
279303
return super.source.getProperty(name);
280304
}
281305

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)