Skip to content

Commit 23174eb

Browse files
committed
Remember annotations when using withExistingValue
Update `Bindable` builder methods so that existing annotations are retained. Closes gh-18218
1 parent 4f21b51 commit 23174eb

File tree

2 files changed

+16
-2
lines changed
  • spring-boot-project/spring-boot/src

2 files changed

+16
-2
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Bindable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public Bindable<T> withExistingValue(T existingValue) {
162162
existingValue == null || this.type.isArray() || this.boxedType.resolve().isInstance(existingValue),
163163
() -> "ExistingValue must be an instance of " + this.type);
164164
Supplier<T> value = (existingValue != null) ? () -> existingValue : null;
165-
return new Bindable<>(this.type, this.boxedType, value, NO_ANNOTATIONS);
165+
return new Bindable<>(this.type, this.boxedType, value, this.annotations);
166166
}
167167

168168
/**
@@ -171,7 +171,7 @@ public Bindable<T> withExistingValue(T existingValue) {
171171
* @return an updated {@link Bindable}
172172
*/
173173
public Bindable<T> withSuppliedValue(Supplier<T> suppliedValue) {
174-
return new Bindable<>(this.type, this.boxedType, suppliedValue, NO_ANNOTATIONS);
174+
return new Bindable<>(this.type, this.boxedType, suppliedValue, this.annotations);
175175
}
176176

177177
/**

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BindableTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ public void equalsAndHashCode() {
160160
assertThat(bindable1).isEqualTo(bindable3);
161161
}
162162

163+
@Test // gh-18218
164+
public void withExistingValueDoesNotForgetAnnotations() {
165+
Annotation annotation = AnnotationUtils.synthesizeAnnotation(TestAnnotation.class);
166+
Bindable<?> bindable = Bindable.of(String.class).withAnnotations(annotation).withExistingValue("");
167+
assertThat(bindable.getAnnotations()).containsExactly(annotation);
168+
}
169+
170+
@Test // gh-18218
171+
public void withSuppliedValueValueDoesNotForgetAnnotations() {
172+
Annotation annotation = AnnotationUtils.synthesizeAnnotation(TestAnnotation.class);
173+
Bindable<?> bindable = Bindable.of(String.class).withAnnotations(annotation).withSuppliedValue(() -> "");
174+
assertThat(bindable.getAnnotations()).containsExactly(annotation);
175+
}
176+
163177
@Retention(RetentionPolicy.RUNTIME)
164178
@interface TestAnnotation {
165179

0 commit comments

Comments
 (0)