]> source.dussan.org Git - vaadin-framework.git/commitdiff
fix: Ensure that side effects of readBean does not set hasChanges true (#12462)
authorAnna Koskinen <Ansku@users.noreply.github.com>
Fri, 29 Oct 2021 11:30:37 +0000 (14:30 +0300)
committerGitHub <noreply@github.com>
Fri, 29 Oct 2021 11:30:37 +0000 (14:30 +0300)
(#12455)

Authored-by: Tatu Lund <tatu@vaadin.com>
server/src/main/java/com/vaadin/data/Binder.java
server/src/test/java/com/vaadin/data/BinderTest.java

index 110e260b0c4d1b8e53a6572a07352c2dbb7a24e4..e969a032f70f88e034ed495a0e947846920fe511 100644 (file)
@@ -1891,7 +1891,6 @@ public class Binder<BEAN> 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<BEAN> implements Serializable {
                     binding.initFieldValue(bean, false);
                 }
             });
+            changedBindings.clear();
             getValidationStatusHandler().statusChange(
                     BinderValidationStatus.createUnresolvedStatus(this));
             fireStatusChangeEvent(false);
index fe529fa8f5db5e8a534ffa2009018466bd510463..62ac93ce8ec8974978854dc51e04feaab4ac35c3 100644 (file)
@@ -1518,6 +1518,28 @@ public class BinderTest extends BinderTestBase<Binder<Person>, 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() {