From: Tatu Lund Date: Wed, 27 Oct 2021 10:18:46 +0000 (+0300) Subject: fix: Ensure that side effects of readBean does not set hasChanges true (#12455) X-Git-Tag: 8.15.0~23 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=51d403f07d33add4332ad97823218a1040422ac8;p=vaadin-framework.git fix: Ensure that side effects of readBean does not set hasChanges true (#12455) --- diff --git a/server/src/main/java/com/vaadin/data/Binder.java b/server/src/main/java/com/vaadin/data/Binder.java index 110e260b0c..e969a032f7 100644 --- a/server/src/main/java/com/vaadin/data/Binder.java +++ b/server/src/main/java/com/vaadin/data/Binder.java @@ -1891,7 +1891,6 @@ public class Binder implements Serializable { if (bean == null) { clearFields(); } else { - changedBindings.clear(); getBindings().forEach(binding -> { // Some bindings may have been removed from binder // during readBean. We should skip those bindings to @@ -1902,6 +1901,7 @@ public class Binder implements Serializable { binding.initFieldValue(bean, false); } }); + changedBindings.clear(); getValidationStatusHandler().statusChange( BinderValidationStatus.createUnresolvedStatus(this)); fireStatusChangeEvent(false); diff --git a/server/src/test/java/com/vaadin/data/BinderTest.java b/server/src/test/java/com/vaadin/data/BinderTest.java index fe529fa8f5..62ac93ce8e 100644 --- a/server/src/test/java/com/vaadin/data/BinderTest.java +++ b/server/src/test/java/com/vaadin/data/BinderTest.java @@ -1518,6 +1518,28 @@ public class BinderTest extends BinderTestBase, Person> { assertEquals(new Double(2000), item.getSalaryDouble()); } + // See: https://github.com/vaadin/framework/issues/9581 + @Test + public void withConverter_hasChangesFalse() { + TextField nameField = new TextField(); + nameField.setValue(""); + TextField rentField = new TextField(); + rentField.setValue(""); + rentField.addValueChangeListener(event -> { + nameField.setValue("Name"); + }); + item.setRent(BigDecimal.valueOf(10)); + binder.forField(nameField).bind(Person::getFirstName, Person::setFirstName); + binder.forField(rentField).withConverter(new EuroConverter("")) + .withNullRepresentation(BigDecimal.valueOf(0d)) + .bind(Person::getRent, Person::setRent); + binder.readBean(item); + + assertFalse(binder.hasChanges()); + assertEquals("€ 10.00", rentField.getValue()); + assertEquals("Name", nameField.getValue()); + } + private TextField createNullAnd42RejectingFieldWithEmptyValue( String emptyValue) { return new TextField() {