Commit f5b93da9 authored by Phillip Webb's avatar Phillip Webb

Fix conversion failures when using DefaultResolver

Update `ConfigurationPropertySourcesPropertyResolver` so that calls to
the `DefaultResolver` do not attempt conversion.

Prior to this commit, the delegate resolver was accidentally called
with the target type which could cause a `ConversionFailedException`
to be thrown. We should have always used `Object.class` and let the
`convertValueIfNecessary` method perform conversion.

Fixes gh-26732
parent be23a296
...@@ -71,7 +71,7 @@ class ConfigurationPropertySourcesPropertyResolver extends AbstractPropertyResol ...@@ -71,7 +71,7 @@ class ConfigurationPropertySourcesPropertyResolver extends AbstractPropertyResol
} }
private <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) { private <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) {
Object value = findPropertyValue(key, targetValueType); Object value = findPropertyValue(key);
if (value == null) { if (value == null) {
return null; return null;
} }
...@@ -81,7 +81,7 @@ class ConfigurationPropertySourcesPropertyResolver extends AbstractPropertyResol ...@@ -81,7 +81,7 @@ class ConfigurationPropertySourcesPropertyResolver extends AbstractPropertyResol
return convertValueIfNecessary(value, targetValueType); return convertValueIfNecessary(value, targetValueType);
} }
private Object findPropertyValue(String key, Class<?> targetValueType) { private Object findPropertyValue(String key) {
ConfigurationPropertySourcesPropertySource attached = getAttached(); ConfigurationPropertySourcesPropertySource attached = getAttached();
if (attached != null) { if (attached != null) {
ConfigurationPropertyName name = ConfigurationPropertyName.of(key, true); ConfigurationPropertyName name = ConfigurationPropertyName.of(key, true);
...@@ -94,7 +94,7 @@ class ConfigurationPropertySourcesPropertyResolver extends AbstractPropertyResol ...@@ -94,7 +94,7 @@ class ConfigurationPropertySourcesPropertyResolver extends AbstractPropertyResol
} }
} }
} }
return this.defaultResolver.getProperty(key, targetValueType, false); return this.defaultResolver.getProperty(key, Object.class, false);
} }
private ConfigurationPropertySourcesPropertySource getAttached() { private ConfigurationPropertySourcesPropertySource getAttached() {
......
...@@ -102,6 +102,17 @@ class ConfigurationPropertySourcesPropertyResolverTests { ...@@ -102,6 +102,17 @@ class ConfigurationPropertySourcesPropertyResolverTests {
assertThat(propertySource.getCount("sprong")).isEqualTo(1); assertThat(propertySource.getCount("sprong")).isEqualTo(1);
} }
@Test // gh-26732
void getPropertyAsTypeWhenHasPlaceholder() {
ResolverEnvironment environment = new ResolverEnvironment();
MockPropertySource propertySource = new MockPropertySource();
propertySource.withProperty("v1", "1");
propertySource.withProperty("v2", "${v1}");
environment.getPropertySources().addFirst(propertySource);
assertThat(environment.getProperty("v2")).isEqualTo("1");
assertThat(environment.getProperty("v2", Integer.class)).isEqualTo(1);
}
private CountingMockPropertySource createMockPropertySource(StandardEnvironment environment, boolean attach) { private CountingMockPropertySource createMockPropertySource(StandardEnvironment environment, boolean attach) {
CountingMockPropertySource propertySource = new CountingMockPropertySource(); CountingMockPropertySource propertySource = new CountingMockPropertySource();
propertySource.withProperty("spring", "boot"); propertySource.withProperty("spring", "boot");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment