Add support for annotations on constructor parameter binding

Closes gh-17109
pull/17111/head
Madhura Bhave 6 years ago
parent e6151a6478
commit 6b3329b274

@ -83,7 +83,7 @@ class ConstructorParametersBinder implements BeanBinder {
private Object bind(ConstructorParameter parameter, BeanPropertyBinder propertyBinder) {
String propertyName = parameter.getName();
ResolvableType type = parameter.getType();
return propertyBinder.bindProperty(propertyName, Bindable.of(type));
return propertyBinder.bindProperty(propertyName, Bindable.of(type).withAnnotations(parameter.getAnnotations()));
}
private static final class Bean {

@ -15,6 +15,7 @@
*/
package org.springframework.boot.context.properties.bind;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
@ -23,6 +24,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.MockConfigurationPropertySource;
import org.springframework.format.annotation.DateTimeFormat;
import static org.assertj.core.api.Assertions.assertThat;
@ -137,6 +139,16 @@ class ConstructorParametersBinderTests {
assertThat(bean.getCustomList()).containsOnly("x,y,z");
}
@Test
void bindWithAnnotations() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.date", "2014-04-01");
this.sources.add(source);
ConverterAnnotatedExampleBean bean = this.binder.bind("foo", Bindable.of(ConverterAnnotatedExampleBean.class))
.get();
assertThat(bean.getDate().toString()).isEqualTo("2014-04-01");
}
public static class ExampleValueBean {
private final int intValue;
@ -265,4 +277,18 @@ class ConstructorParametersBinderTests {
}
public static class ConverterAnnotatedExampleBean {
private final LocalDate date;
ConverterAnnotatedExampleBean(@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
this.date = date;
}
public LocalDate getDate() {
return this.date;
}
}
}

Loading…
Cancel
Save