Skip to content

Commit

Permalink
Fix conversion failures when using DefaultResolver
Browse files Browse the repository at this point in the history
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
  • Loading branch information
philwebb committed Jun 8, 2021
1 parent be23a29 commit f5b93da
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Expand Up @@ -71,7 +71,7 @@ protected String getPropertyAsRawString(String key) {
}

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;
}
Expand All @@ -81,7 +81,7 @@ private <T> T getProperty(String key, Class<T> targetValueType, boolean resolveN
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);
Expand All @@ -94,7 +94,7 @@ private Object findPropertyValue(String key, Class<?> targetValueType) {
}
}
}
return this.defaultResolver.getProperty(key, targetValueType, false);
return this.defaultResolver.getProperty(key, Object.class, false);
}

private ConfigurationPropertySourcesPropertySource getAttached() {
Expand Down
Expand Up @@ -102,6 +102,17 @@ void getPropertyWhenNotAttached() {
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");
Expand Down

0 comments on commit f5b93da

Please sign in to comment.