From f5b93da90ff3ac4338797319f2cdbd21091a316c Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 7 Jun 2021 22:02:58 -0700 Subject: [PATCH] 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 --- .../ConfigurationPropertySourcesPropertyResolver.java | 6 +++--- ...igurationPropertySourcesPropertyResolverTests.java | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesPropertyResolver.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesPropertyResolver.java index 1815da7535dd..40c4158ed086 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesPropertyResolver.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesPropertyResolver.java @@ -71,7 +71,7 @@ protected String getPropertyAsRawString(String key) { } private T getProperty(String key, Class targetValueType, boolean resolveNestedPlaceholders) { - Object value = findPropertyValue(key, targetValueType); + Object value = findPropertyValue(key); if (value == null) { return null; } @@ -81,7 +81,7 @@ private T getProperty(String key, Class 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); @@ -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() { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesPropertyResolverTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesPropertyResolverTests.java index 2447ae9c2926..a93d00c364aa 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesPropertyResolverTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesPropertyResolverTests.java @@ -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");