From 6b3329b274ffa80ec22e1a13570a01f4cefc8d6f Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Tue, 11 Jun 2019 16:32:55 -0700 Subject: [PATCH] Add support for annotations on constructor parameter binding Closes gh-17109 --- .../bind/ConstructorParametersBinder.java | 2 +- .../ConstructorParametersBinderTests.java | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/ConstructorParametersBinder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/ConstructorParametersBinder.java index cf0d158b23..8be6c6d2b0 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/ConstructorParametersBinder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/ConstructorParametersBinder.java @@ -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 { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/ConstructorParametersBinderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/ConstructorParametersBinderTests.java index e8f8a56145..5d35092245 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/ConstructorParametersBinderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/ConstructorParametersBinderTests.java @@ -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; + } + + } + }