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

Binder.removeBinding issue #11280

Closed
nbabb opened this issue Oct 29, 2018 · 4 comments
Closed

Binder.removeBinding issue #11280

nbabb opened this issue Oct 29, 2018 · 4 comments
Milestone

Comments

@nbabb
Copy link

nbabb commented Oct 29, 2018

In Vaadin flow 1.0.5, I have several fields bound to a Binder instance. When I remove a binding (where there are still other bound properties) it is calling unbind. It seems like it should only call unbind when the count of bound properties is zero? Here is the code.

public void removeBinding(String propertyName) {
    Objects.requireNonNull(propertyName, "Property name may not be null");
    Optional.ofNullable(boundProperties.get(propertyName))
            .ifPresent(Binding::unbind);
}

I end up receiving errors after I remove the binding

java.lang.NullPointerException: This Binding is no longer attached to a Binder
at java.base/java.util.Objects.requireNonNull(Objects.java:246) ~[na:na]
at com.vaadin.flow.data.binder.Binder$BindingImpl.validate(Binder.java:1027) ~[flow-data-1.0.5.jar:na]

from my other bound fields that are still using the binding.

Perhaps I'm misunderstanding how this should work?

@rsk-munendra
Copy link

rsk-munendra commented Nov 26, 2018

I am also getting similar issue when using removeBinding(field).
Seems like when bindings are removed, still the validators for the fields are no dereferenced from the binder.

@lulnope
Copy link

lulnope commented Mar 19, 2019

The issue is located in Binder class in its protected void removeBindingInternal(Binding<T, ?> binding) method. Currently it looks as follows:

protected void removeBindingInternal(Binding<BEAN, ?> binding) {
        if (bindings.remove(binding)) {
            boundProperties.entrySet()
                    .removeIf(entry -> entry.getValue().equals(binding));
        }
    }

it's missing:
changedBindings.remove(binding)
which should resolve this error:
java.lang.NullPointerException: This Binding is no longer attached to a Binder
Currently a dirty workaround might look like this:

public class WorkaroundBinder<T> extends Binder<T> {

	public WorkaroundBinder() {
		super();
	}

	public WorkaroundBinder(Class<T> beanType, boolean scanNestedDefinitions) {
		super(beanType, scanNestedDefinitions);
	}

	public WorkaroundBinder(Class<T> beanType) {
		super(beanType);
	}

	public WorkaroundBinder(PropertySet<T> propertySet) {
		super(propertySet);
	}
	
	@Override
	protected void removeBindingInternal(Binding<T, ?> binding) {		
		super.removeBindingInternal(binding);
		try {
// if binder does not validate changedBindings is not cleared (possibly a mistake in doWriteIfValid method) as such
// a reference to binding might be left hanging in that set.
// This hack removes hanging reference from changedBindings .
			Field field = Binder.class.getDeclaredField("changedBindings");
			field.setAccessible(true);
			Set<Binding<T, ?>> changedBindings = (Set<Binding<T, ?>>) field.get(this);
			changedBindings.remove(binding);
		} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
			e.printStackTrace();
		}
	}
	
}

@KatriHaapalinna
Copy link
Contributor

Hi, this is the repository for Vaadin Framework (i.e. versions 7 and 8). You seem to be using the newer version, Vaadin Flow, which is located in a different repository.
Please create the issue to the GH repository of Vaadin Flow here: https://github.com/vaadin/flow/issues

@kpy3no
Copy link

kpy3no commented Jun 4, 2019

instead of using remove binding i create only one binding for a field and judging by current state in runtime choose what i'm going to validate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants