Skip to content

Commit c23af8f

Browse files
committed
Support of GenericConverter
This commit makes sure to also include any `GenericConverter` flagged for inclusion in the conversion service to use to convert configuration keys. Previously, those were ignored as `Converter` and `GenericConverter` are two separate interfaces. Closes gh-4988
1 parent 6f8d4c7 commit c23af8f

File tree

2 files changed

+65
-12
lines changed

2 files changed

+65
-12
lines changed

spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 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.
@@ -47,6 +47,7 @@
4747
import org.springframework.core.annotation.AnnotationUtils;
4848
import org.springframework.core.convert.ConversionService;
4949
import org.springframework.core.convert.converter.Converter;
50+
import org.springframework.core.convert.converter.GenericConverter;
5051
import org.springframework.core.convert.support.DefaultConversionService;
5152
import org.springframework.core.env.ConfigurableEnvironment;
5253
import org.springframework.core.env.Environment;
@@ -108,6 +109,8 @@ public class ConfigurationPropertiesBindingPostProcessor
108109

109110
private List<Converter<?, ?>> converters = Collections.emptyList();
110111

112+
private List<GenericConverter> genericConverters = Collections.emptyList();
113+
111114
private int order = Ordered.HIGHEST_PRECEDENCE + 1;
112115

113116
/**
@@ -121,6 +124,17 @@ public void setConverters(List<Converter<?, ?>> converters) {
121124
this.converters = converters;
122125
}
123126

127+
/**
128+
* A list of custom converters (in addition to the defaults) to use when converting
129+
* properties for binding.
130+
* @param converters the converters to set
131+
*/
132+
@Autowired(required = false)
133+
@ConfigurationPropertiesBinding
134+
public void setGenericConverters(List<GenericConverter> converters) {
135+
this.genericConverters = converters;
136+
}
137+
124138
/**
125139
* Set the order of the bean.
126140
* @param order the order
@@ -407,6 +421,9 @@ private ConversionService getDefaultConversionService() {
407421
for (Converter<?, ?> converter : this.converters) {
408422
conversionService.addConverter(converter);
409423
}
424+
for (GenericConverter genericConverter : this.genericConverters) {
425+
conversionService.addConverter(genericConverter);
426+
}
410427
this.defaultConversionService = conversionService;
411428
}
412429
return this.defaultConversionService;

spring-boot/src/test/java/org/springframework/boot/bind/ConverterBindingTests.java

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 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.
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.boot.bind;
1818

19+
import java.util.Collections;
20+
import java.util.Set;
21+
1922
import org.junit.Test;
2023
import org.junit.runner.RunWith;
2124

@@ -30,7 +33,9 @@
3033
import org.springframework.context.annotation.Bean;
3134
import org.springframework.context.annotation.Configuration;
3235
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
36+
import org.springframework.core.convert.TypeDescriptor;
3337
import org.springframework.core.convert.converter.Converter;
38+
import org.springframework.core.convert.converter.GenericConverter;
3439
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3540

3641
import static org.hamcrest.Matchers.is;
@@ -40,21 +45,26 @@
4045
* Tests for {@link ConfigurationProperties} binding with custom converters.
4146
*
4247
* @author Dave Syer
48+
* @author Stephane Nicoll
4349
*/
4450
@RunWith(SpringJUnit4ClassRunner.class)
4551
@SpringApplicationConfiguration(TestConfig.class)
46-
@IntegrationTest("foo=bar")
52+
@IntegrationTest({"foo=one", "bar=two"})
4753
public class ConverterBindingTests {
4854

4955
@Value("${foo:}")
5056
private String foo;
5157

58+
@Value("${bar:}")
59+
private String bar;
60+
5261
@Autowired
5362
private Wrapper properties;
5463

5564
@Test
5665
public void overridingOfPropertiesOrderOfAtPropertySources() {
57-
assertThat(this.properties.getFoo().getName(), is(this.foo));
66+
assertThat(this.properties.getFoo().name, is(this.foo));
67+
assertThat(this.properties.getBar().name, is(this.bar));
5868
}
5969

6070
@Configuration
@@ -68,9 +78,22 @@ public Converter<String, Foo> converter() {
6878

6979
@Override
7080
public Foo convert(String source) {
71-
Foo foo = new Foo();
72-
foo.setName(source);
73-
return foo;
81+
return new Foo(source);
82+
}
83+
};
84+
}
85+
86+
@Bean
87+
public GenericConverter genericConverter() {
88+
return new GenericConverter() {
89+
@Override
90+
public Set<ConvertiblePair> getConvertibleTypes() {
91+
return Collections.singleton(new ConvertiblePair(String.class, Bar.class));
92+
}
93+
94+
@Override
95+
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
96+
return new Bar((String) source);
7497
}
7598
};
7699
}
@@ -84,23 +107,29 @@ public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderCon
84107

85108
public static class Foo {
86109

87-
private String name;
110+
private final String name;
88111

89-
public String getName() {
90-
return this.name;
112+
public Foo(String name) {
113+
this.name = name;
91114
}
115+
}
116+
117+
public static class Bar {
118+
119+
private final String name;
92120

93-
public void setName(String name) {
121+
public Bar(String name) {
94122
this.name = name;
95123
}
96-
97124
}
98125

99126
@ConfigurationProperties
100127
public static class Wrapper {
101128

102129
private Foo foo;
103130

131+
private Bar bar;
132+
104133
public Foo getFoo() {
105134
return this.foo;
106135
}
@@ -109,6 +138,13 @@ public void setFoo(Foo foo) {
109138
this.foo = foo;
110139
}
111140

141+
public Bar getBar() {
142+
return this.bar;
143+
}
144+
145+
public void setBar(Bar bar) {
146+
this.bar = bar;
147+
}
112148
}
113149

114150
}

0 commit comments

Comments
 (0)