Skip to content

Commit beb1c15

Browse files
committed
Merge branch '2.6.x' into 2.7.x
Closes gh-31341
2 parents 6492299 + 31b0264 commit beb1c15

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
@@ -57,6 +57,7 @@
5757
import org.springframework.boot.context.properties.bind.DefaultValue;
5858
import org.springframework.boot.context.properties.bind.validation.BindValidationException;
5959
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
60+
import org.springframework.boot.convert.ApplicationConversionService;
6061
import org.springframework.boot.convert.DataSizeUnit;
6162
import org.springframework.boot.convert.DurationFormat;
6263
import org.springframework.boot.convert.DurationStyle;
@@ -1081,6 +1082,15 @@ void boundPropertiesShouldBeRecorded() {
10811082
assertThat(keys.stream().map(ConfigurationPropertyName::toString)).contains("name", "nested.name");
10821083
}
10831084

1085+
@Test // gh-28592
1086+
void loadWhenBindingWithCustomConverterAndObjectToObjectMethod() {
1087+
this.context.getBeanFactory().setConversionService(ApplicationConversionService.getSharedInstance());
1088+
load(WithCustomConverterAndObjectToObjectMethodConfiguration.class, "test.item=foo");
1089+
WithCustomConverterAndObjectToObjectMethodProperties bean = this.context
1090+
.getBean(WithCustomConverterAndObjectToObjectMethodProperties.class);
1091+
assertThat(bean.getItem().getValue()).isEqualTo("foo");
1092+
}
1093+
10841094
private AnnotationConfigApplicationContext load(Class<?> configuration, String... inlinedProperties) {
10851095
return load(new Class<?>[] { configuration }, inlinedProperties);
10861096
}
@@ -2710,4 +2720,40 @@ static class WithPublicStringConstructorPropertiesConfiguration {
27102720

27112721
}
27122722

2723+
@Configuration(proxyBeanMethods = false)
2724+
@EnableConfigurationProperties(WithCustomConverterAndObjectToObjectMethodProperties.class)
2725+
static class WithCustomConverterAndObjectToObjectMethodConfiguration {
2726+
2727+
@Bean
2728+
@ConfigurationPropertiesBinding
2729+
WithObjectToObjectMethodConverter withObjectToObjectMethodConverter() {
2730+
return new WithObjectToObjectMethodConverter();
2731+
}
2732+
2733+
}
2734+
2735+
@ConfigurationProperties("test")
2736+
static class WithCustomConverterAndObjectToObjectMethodProperties {
2737+
2738+
private WithPublicObjectToObjectMethod item;
2739+
2740+
WithPublicObjectToObjectMethod getItem() {
2741+
return this.item;
2742+
}
2743+
2744+
void setItem(WithPublicObjectToObjectMethod item) {
2745+
this.item = item;
2746+
}
2747+
2748+
}
2749+
2750+
static class WithObjectToObjectMethodConverter implements Converter<String, WithPublicObjectToObjectMethod> {
2751+
2752+
@Override
2753+
public WithPublicObjectToObjectMethod convert(String source) {
2754+
return new WithPublicObjectToObjectMethod(source);
2755+
}
2756+
2757+
}
2758+
27132759
}
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)