From 23174eb484e2b005608968468967721e2af77aee Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 12 Sep 2019 15:09:47 -0700 Subject: [PATCH] Remember annotations when using withExistingValue Update `Bindable` builder methods so that existing annotations are retained. Closes gh-18218 --- .../boot/context/properties/bind/Bindable.java | 4 ++-- .../context/properties/bind/BindableTests.java | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Bindable.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Bindable.java index 4f8f4069b84..a7f2bdbaaff 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Bindable.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Bindable.java @@ -162,7 +162,7 @@ public final class Bindable { existingValue == null || this.type.isArray() || this.boxedType.resolve().isInstance(existingValue), () -> "ExistingValue must be an instance of " + this.type); Supplier value = (existingValue != null) ? () -> existingValue : null; - return new Bindable<>(this.type, this.boxedType, value, NO_ANNOTATIONS); + return new Bindable<>(this.type, this.boxedType, value, this.annotations); } /** @@ -171,7 +171,7 @@ public final class Bindable { * @return an updated {@link Bindable} */ public Bindable withSuppliedValue(Supplier suppliedValue) { - return new Bindable<>(this.type, this.boxedType, suppliedValue, NO_ANNOTATIONS); + return new Bindable<>(this.type, this.boxedType, suppliedValue, this.annotations); } /** diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BindableTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BindableTests.java index 48be7b3f8dc..ecec89a8bd6 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BindableTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BindableTests.java @@ -160,6 +160,20 @@ public class BindableTests { assertThat(bindable1).isEqualTo(bindable3); } + @Test // gh-18218 + public void withExistingValueDoesNotForgetAnnotations() { + Annotation annotation = AnnotationUtils.synthesizeAnnotation(TestAnnotation.class); + Bindable bindable = Bindable.of(String.class).withAnnotations(annotation).withExistingValue(""); + assertThat(bindable.getAnnotations()).containsExactly(annotation); + } + + @Test // gh-18218 + public void withSuppliedValueValueDoesNotForgetAnnotations() { + Annotation annotation = AnnotationUtils.synthesizeAnnotation(TestAnnotation.class); + Bindable bindable = Bindable.of(String.class).withAnnotations(annotation).withSuppliedValue(() -> ""); + assertThat(bindable.getAnnotations()).containsExactly(annotation); + } + @Retention(RetentionPolicy.RUNTIME) @interface TestAnnotation {