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
This commit is contained in:
Phillip Webb 2021-06-07 22:02:58 -07:00
parent be23a29651
commit f5b93da90f
2 changed files with 14 additions and 3 deletions

View File

@ -71,7 +71,7 @@ class ConfigurationPropertySourcesPropertyResolver extends AbstractPropertyResol
}
private <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) {
Object value = findPropertyValue(key, targetValueType);
Object value = findPropertyValue(key);
if (value == null) {
return null;
}
@ -81,7 +81,7 @@ class ConfigurationPropertySourcesPropertyResolver extends AbstractPropertyResol
return convertValueIfNecessary(value, targetValueType);
}
private Object findPropertyValue(String key, Class<?> targetValueType) {
private Object findPropertyValue(String key) {
ConfigurationPropertySourcesPropertySource attached = getAttached();
if (attached != null) {
ConfigurationPropertyName name = ConfigurationPropertyName.of(key, true);
@ -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() {

View File

@ -102,6 +102,17 @@ class ConfigurationPropertySourcesPropertyResolverTests {
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) {
CountingMockPropertySource propertySource = new CountingMockPropertySource();
propertySource.withProperty("spring", "boot");