Skip to content

Commit 8937924

Browse files
committed
Merge branch '2.7.x'
Closes gh-31342
2 parents cb745b4 + beb1c15 commit 8937924

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/BindConverter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -106,7 +106,10 @@ private Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor
106106
for (ConversionService delegate : this.delegates) {
107107
try {
108108
if (delegate.canConvert(sourceType, targetType)) {
109-
return delegate.convert(source, sourceType, targetType);
109+
Object converted = delegate.convert(source, sourceType, targetType);
110+
if (targetType.getType().isInstance(converted)) {
111+
return converted;
112+
}
110113
}
111114
}
112115
catch (ConversionException ex) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.springframework.boot.context.properties.bind.DefaultValue;
5757
import org.springframework.boot.context.properties.bind.validation.BindValidationException;
5858
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
59+
import org.springframework.boot.convert.ApplicationConversionService;
5960
import org.springframework.boot.convert.DataSizeUnit;
6061
import org.springframework.boot.convert.DurationFormat;
6162
import org.springframework.boot.convert.DurationStyle;
@@ -1090,6 +1091,15 @@ void boundPropertiesShouldBeRecorded() {
10901091
assertThat(keys.stream().map(ConfigurationPropertyName::toString)).contains("name", "nested.name");
10911092
}
10921093

1094+
@Test // gh-28592
1095+
void loadWhenBindingWithCustomConverterAndObjectToObjectMethod() {
1096+
this.context.getBeanFactory().setConversionService(ApplicationConversionService.getSharedInstance());
1097+
load(WithCustomConverterAndObjectToObjectMethodConfiguration.class, "test.item=foo");
1098+
WithCustomConverterAndObjectToObjectMethodProperties bean = this.context
1099+
.getBean(WithCustomConverterAndObjectToObjectMethodProperties.class);
1100+
assertThat(bean.getItem().getValue()).isEqualTo("foo");
1101+
}
1102+
10931103
private AnnotationConfigApplicationContext load(Class<?> configuration, String... inlinedProperties) {
10941104
return load(new Class<?>[] { configuration }, inlinedProperties);
10951105
}
@@ -2744,4 +2754,40 @@ static class WithPublicStringConstructorPropertiesConfiguration {
27442754

27452755
}
27462756

2757+
@Configuration(proxyBeanMethods = false)
2758+
@EnableConfigurationProperties(WithCustomConverterAndObjectToObjectMethodProperties.class)
2759+
static class WithCustomConverterAndObjectToObjectMethodConfiguration {
2760+
2761+
@Bean
2762+
@ConfigurationPropertiesBinding
2763+
WithObjectToObjectMethodConverter withObjectToObjectMethodConverter() {
2764+
return new WithObjectToObjectMethodConverter();
2765+
}
2766+
2767+
}
2768+
2769+
@ConfigurationProperties("test")
2770+
static class WithCustomConverterAndObjectToObjectMethodProperties {
2771+
2772+
private WithPublicObjectToObjectMethod item;
2773+
2774+
WithPublicObjectToObjectMethod getItem() {
2775+
return this.item;
2776+
}
2777+
2778+
void setItem(WithPublicObjectToObjectMethod item) {
2779+
this.item = item;
2780+
}
2781+
2782+
}
2783+
2784+
static class WithObjectToObjectMethodConverter implements Converter<String, WithPublicObjectToObjectMethod> {
2785+
2786+
@Override
2787+
public WithPublicObjectToObjectMethod convert(String source) {
2788+
return new WithPublicObjectToObjectMethod(source);
2789+
}
2790+
2791+
}
2792+
27472793
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.context.properties;
18+
19+
import java.util.Optional;
20+
21+
/**
22+
* Data object with a pubic method picked up by the {@code ObjectToObjectConverter}. Used
23+
* in {@link ConfigurationPropertiesTests}.
24+
*
25+
* @author Phillip Webb
26+
*/
27+
public class WithPublicObjectToObjectMethod {
28+
29+
private final String value;
30+
31+
WithPublicObjectToObjectMethod(String value) {
32+
this.value = value;
33+
}
34+
35+
String getValue() {
36+
return this.value;
37+
}
38+
39+
public static Optional<WithPublicObjectToObjectMethod> from(String value) {
40+
return Optional.of(new WithPublicObjectToObjectMethod(value));
41+
}
42+
43+
}

0 commit comments

Comments
 (0)