]> source.dussan.org Git - vaadin-framework.git/commitdiff
fix: don't update bean property which is read-only bound (#12457) (#12463)
authorAnna Koskinen <Ansku@users.noreply.github.com>
Fri, 29 Oct 2021 12:37:06 +0000 (15:37 +0300)
committerGitHub <noreply@github.com>
Fri, 29 Oct 2021 12:37:06 +0000 (15:37 +0300)
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 e969a032f70f88e034ed495a0e947846920fe511..9726f5434568eb534a7f8f14fe01020aebd5e44d 100644 (file)
@@ -1258,7 +1258,7 @@ public class Binder<BEAN> implements Serializable {
                 TARGET originalValue = getter.apply(bean);
                 convertAndSetFieldValue(originalValue);
 
-                if (writeBackChangedValues && setter != null) {
+                if (writeBackChangedValues && setter != null && !readOnly) {
                     doConversion().ifOk(convertedValue -> {
                         if (!Objects.equals(originalValue, convertedValue)) {
                             setter.accept(bean, convertedValue);
index 62ac93ce8ec8974978854dc51e04feaab4ac35c3..9f20f0b246340d1f3662609caa4d8e2588db3cbb 100644 (file)
@@ -10,6 +10,7 @@ import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.io.Serializable;
 import java.math.BigDecimal;
 import java.text.DecimalFormat;
 import java.text.NumberFormat;
@@ -1540,6 +1541,76 @@ public class BinderTest extends BinderTestBase<Binder<Person>, Person> {
         assertEquals("Name", nameField.getValue());
     }
 
+    @Test
+    public void setBean_readOnlyBinding_propertyBinding_valueIsNotUpdated() {
+        Binder<ExampleBean> binder = new Binder<>(ExampleBean.class);
+
+        binder.forField(nameField).withNullRepresentation("")
+                .withConverter(new TestConverter()).bind("vals")
+                .setReadOnly(true);
+
+        ExampleBean bean = new ExampleBean();
+        SubPropClass val = new SubPropClass();
+        bean.setVals(val);
+        binder.setBean(bean);
+
+        assertSame(val, bean.getVals());
+    }
+
+    @Test
+    public void setBean_readOnlyBinding_accessorsBiding_valueIsNotUpdated() {
+        Binder<ExampleBean> binder = new Binder<>(ExampleBean.class);
+
+        binder.forField(nameField).withNullRepresentation("")
+                .withConverter(new TestConverter())
+                .bind(ExampleBean::getVals, ExampleBean::setVals)
+                .setReadOnly(true);
+
+        ExampleBean bean = new ExampleBean();
+        SubPropClass val = new SubPropClass();
+        bean.setVals(val);
+        binder.setBean(bean);
+
+        assertSame(val, bean.getVals());
+    }
+
+    public static class ExampleBean implements Serializable {
+        private SubPropClass vals;
+
+        public SubPropClass getVals() {
+            return vals;
+        }
+
+        public void setVals(SubPropClass vals) {
+            this.vals = vals;
+        }
+    }
+
+    public static class SubPropClass implements Serializable {
+        private String val1 = "Val1";
+
+        @Override
+        public String toString() {
+            return val1;
+        }
+    }
+
+    public static class TestConverter
+            implements Converter<String, SubPropClass> {
+
+        @Override
+        public Result<SubPropClass> convertToModel(String value,
+                ValueContext context) {
+            return Result.ok(null);
+        }
+
+        @Override
+        public String convertToPresentation(SubPropClass value,
+                ValueContext context) {
+            return value != null ? value.toString() : null;
+        }
+    };
+
     private TextField createNullAnd42RejectingFieldWithEmptyValue(
             String emptyValue) {
         return new TextField() {