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:
parent
be23a29651
commit
f5b93da90f
|
@ -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
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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 {
|
||||||
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");
|
||||||
|
|
Loading…
Reference in New Issue