Skip to content

Commit

Permalink
fix: don't update bean property which is read-only bound (#10768)
Browse files Browse the repository at this point in the history
fixes #9446
  • Loading branch information
Denis authored and vaadin-bot committed Apr 23, 2021
1 parent 91b9e8c commit d000448
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ private void initFieldValue(BEAN bean, boolean writeBackChangedValues) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.vaadin.flow.data.binder;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
Expand Down Expand Up @@ -1620,6 +1621,39 @@ public void addValueListenerFromStatusListener_listenerAdded() {
innerListenerInvoked.get());
}

@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);

Assert.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);

Assert.assertSame(val, bean.getVals());
}

private TestTextField createNullRejectingFieldWithEmptyValue(
String emptyValue) {
return new TestTextField() {
Expand Down Expand Up @@ -1649,6 +1683,43 @@ private Binder<AtomicReference<Integer>> createIntegerConverterBinder(
return binder;
}

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;
}
};

/**
* A converter that adds/removes the euro sign and formats currencies with
* two decimal places.
Expand Down

0 comments on commit d000448

Please sign in to comment.