Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix setReadonly when using binder with records #20855

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,7 @@ private void convertAndSetFieldValue(TARGET modelValue) {

@Override
public void setReadOnly(boolean readOnly) {
if (setter == null && !readOnly) {
if (!binder.isRecord && this.setter == null && !readOnly) {
throw new IllegalStateException(
"Binding with a null setter has to be read-only");
}
Expand Down Expand Up @@ -3535,7 +3535,8 @@ public boolean hasChanges(Binding<BEAN, ?> binding) {
* to set them to read-write
*/
public void setReadOnly(boolean readOnly) {
getBindings().stream().filter(binding -> binding.getSetter() != null)
getBindings().stream()
.filter(binding -> isRecord || binding.getSetter() != null)
.forEach(field -> field.setReadOnly(readOnly));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,23 @@ public void setReadonlyShouldIgnoreBindingsWithNullSetter() {
assertTrue("Age field should be readonly", ageField.isReadOnly());
}

@Test
public void setReadonly_record_allFieldsAreReadonly() {
Binder<TestRecord> binder = new Binder<>(TestRecord.class);
binder.forField(nameField).bind("name");
binder.forField(ageField).bind("age");

binder.getBinding("name").ifPresent(b -> b.setReadOnly(true));
binder.setReadOnly(true);
assertTrue("Name field should be readonly", nameField.isReadOnly());
assertTrue("Age field should be readonly", ageField.isReadOnly());

binder.setReadOnly(false);
assertFalse("Name field should not be readonly",
nameField.isReadOnly());
assertFalse("Age field should not be readonly", ageField.isReadOnly());
}

@Test
public void isValidTest_bound_binder() {
binder.forField(nameField)
Expand Down
Loading