From c2d1cb2c701f3c0dd06a6db298cd78e06d039c8f Mon Sep 17 00:00:00 2001 From: ayudovin Date: Mon, 17 Jun 2019 09:38:59 -0700 Subject: [PATCH] Chain predicates in PropertyMapper when methods Update `PropertyMapper` to correctly combine predicates when repeated calls are made to `when` and `whenNot`. Prior to this commit, subsequent invocations would replace the previous predicate. Fixes gh-17225 --- .../boot/context/properties/PropertyMapper.java | 5 +++-- .../boot/context/properties/PropertyMapperTests.java | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/PropertyMapper.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/PropertyMapper.java index f5486abb291..72e8cd50ecd 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/PropertyMapper.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/PropertyMapper.java @@ -50,6 +50,7 @@ import org.springframework.util.StringUtils; * {@link Source#toInstance(Function) new instance}. * * @author Phillip Webb + * @author Artsiom Yudovin * @since 2.0.0 */ public final class PropertyMapper { @@ -288,7 +289,7 @@ public final class PropertyMapper { */ public Source whenNot(Predicate predicate) { Assert.notNull(predicate, "Predicate must not be null"); - return new Source<>(this.supplier, predicate.negate()); + return when(predicate.negate()); } /** @@ -299,7 +300,7 @@ public final class PropertyMapper { */ public Source when(Predicate predicate) { Assert.notNull(predicate, "Predicate must not be null"); - return new Source<>(this.supplier, predicate); + return new Source<>(this.supplier, (this.predicate != null) ? this.predicate.and(predicate) : predicate); } /** diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java index 49a8b04aa25..0e7cd005a45 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java @@ -28,6 +28,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException * Tests for {@link PropertyMapper}. * * @author Phillip Webb + * @author Artsiom Yudovin */ public class PropertyMapperTests { @@ -190,6 +191,17 @@ public class PropertyMapperTests { assertThat(source.getCount()).isOne(); } + @Test + public void whenWhenValueNotMatchesShouldSupportChainedCalls() { + this.map.from("123").when("456"::equals).when("123"::equals).toCall(Assert::fail); + } + + @Test + public void whenWhenValueMatchesShouldSupportChainedCalls() { + String result = this.map.from("123").when((s) -> s.contains("2")).when("123"::equals).toInstance(String::new); + assertThat(result).isEqualTo("123"); + } + @Test public void alwaysApplyingWhenNonNullShouldAlwaysApplyNonNullToSource() { this.map.alwaysApplyingWhenNonNull().from(() -> null).toCall(Assert::fail);