From 3569a33239693461d588dbfd1005730943118702 Mon Sep 17 00:00:00 2001 From: schaerl Date: Wed, 22 Nov 2017 06:26:57 +0100 Subject: Fix nested bean property name (#10271) * Changed retrieval of names of nested properties (#10159) The getName() function in NestedBeanPropertyDefinition will return only the top level name, which causes problems when adding a nested property that has the same name as a non-nested (e.g "value" & "sub.value") This change should fix this behaviour by changing the return value of "getName()" of NestedBeanPropertyDefinition and introducing a function "getTopLevelName()" that only returns the last part of the full name for where it's needed --- .../main/java/com/vaadin/data/BeanPropertySet.java | 50 ++++++---------------- .../java/com/vaadin/data/BeanValidationBinder.java | 4 +- .../java/com/vaadin/data/PropertyDefinition.java | 9 +++- .../com/vaadin/data/NestedPropertyNameTest.java | 48 +++++++++++++++++++++ 4 files changed, 72 insertions(+), 39 deletions(-) create mode 100644 server/src/test/java/com/vaadin/data/NestedPropertyNameTest.java (limited to 'server/src') diff --git a/server/src/main/java/com/vaadin/data/BeanPropertySet.java b/server/src/main/java/com/vaadin/data/BeanPropertySet.java index b302c83709..b877fdf939 100644 --- a/server/src/main/java/com/vaadin/data/BeanPropertySet.java +++ b/server/src/main/java/com/vaadin/data/BeanPropertySet.java @@ -170,37 +170,13 @@ public class BeanPropertySet implements PropertySet { private final PropertyDefinition parent; - private boolean useLongFormName = false; - public NestedBeanPropertyDefinition(BeanPropertySet propertySet, PropertyDefinition parent, PropertyDescriptor descriptor) { super(propertySet, parent.getType(), descriptor); this.parent = parent; } - - /** - * Create nested property definition. Allows use of a long form name. - * - * @param propertySet - * property set this property belongs to - * @param parent - * parent property for this nested property - * @param descriptor - * property descriptor - * @param useLongFormName - * use format grandparent.parent.property for name if - * {@code true}, needed when creating nested definitions - * recursively like in findNestedDefinitions - * @since 8.2 - */ - public NestedBeanPropertyDefinition(BeanPropertySet propertySet, - PropertyDefinition parent, PropertyDescriptor descriptor, - boolean useLongFormName) { - this(propertySet, parent, descriptor); - this.useLongFormName = useLongFormName; - } - + @Override public ValueProvider getGetter() { return bean -> { @@ -229,6 +205,16 @@ public class BeanPropertySet implements PropertySet { return Optional.of(setter); } + @Override + public String getName() { + return parent.getName() + "." + super.getName(); + } + + @Override + public String getTopLevelName() { + return super.getName(); + } + private Object writeReplace() { /* * Instead of serializing this actual property definition, only @@ -236,8 +222,9 @@ public class BeanPropertySet implements PropertySet { * property definition from the cache. */ return new SerializedPropertyDefinition(getPropertySet().beanType, - parent.getName() + "." + super.getName()); + getName()); } + /** * Gets the parent property definition. @@ -247,15 +234,6 @@ public class BeanPropertySet implements PropertySet { public PropertyDefinition getParent() { return parent; } - - @Override - public String getName() { - if (useLongFormName) { - return parent.getName() + "." + super.getName(); - } - return super.getName(); - } - } /** @@ -394,7 +372,7 @@ public class BeanPropertySet implements PropertySet { PropertyDescriptor subDescriptor = BeanUtil .getPropertyDescriptor(beanType, name); moreProps.put(name, new NestedBeanPropertyDefinition<>(this, - parentProperty, subDescriptor, true)); + parentProperty, subDescriptor)); } } catch (IntrospectionException e) { diff --git a/server/src/main/java/com/vaadin/data/BeanValidationBinder.java b/server/src/main/java/com/vaadin/data/BeanValidationBinder.java index c358d64ae6..630eef15b7 100644 --- a/server/src/main/java/com/vaadin/data/BeanValidationBinder.java +++ b/server/src/main/java/com/vaadin/data/BeanValidationBinder.java @@ -96,7 +96,7 @@ public class BeanValidationBinder extends Binder { PropertyDefinition definition) { Class actualBeanType = findBeanType(beanType, definition); BeanValidator validator = new BeanValidator(actualBeanType, - definition.getName()); + definition.getTopLevelName()); if (requiredConfigurator != null) { configureRequired(binding, definition, validator); } @@ -132,7 +132,7 @@ public class BeanValidationBinder extends Binder { BeanDescriptor descriptor = validator.getJavaxBeanValidator() .getConstraintsForClass(propertyHolderType); PropertyDescriptor propertyDescriptor = descriptor - .getConstraintsForProperty(definition.getName()); + .getConstraintsForProperty(definition.getTopLevelName()); if (propertyDescriptor == null) { return; } diff --git a/server/src/main/java/com/vaadin/data/PropertyDefinition.java b/server/src/main/java/com/vaadin/data/PropertyDefinition.java index 06252f8a74..68be55a48b 100644 --- a/server/src/main/java/com/vaadin/data/PropertyDefinition.java +++ b/server/src/main/java/com/vaadin/data/PropertyDefinition.java @@ -64,12 +64,19 @@ public interface PropertyDefinition extends Serializable { public Class getPropertyHolderType(); /** - * Gets the name of this property. + * Gets the full name of this property. * * @return the property name, not null */ public String getName(); + /** + * Gets the top level name of this property + * + * @return the top level property name, not null + */ + public default String getTopLevelName() {return getName();} + /** * Gets the human readable caption to show for this property. * diff --git a/server/src/test/java/com/vaadin/data/NestedPropertyNameTest.java b/server/src/test/java/com/vaadin/data/NestedPropertyNameTest.java new file mode 100644 index 0000000000..070a3de40a --- /dev/null +++ b/server/src/test/java/com/vaadin/data/NestedPropertyNameTest.java @@ -0,0 +1,48 @@ +package com.vaadin.data; + +import org.junit.Test; + +import com.vaadin.ui.Grid; + +public class NestedPropertyNameTest { + + @Test + public void nestedProperty_sameNameCanBeAdded() { + Grid grid = new Grid<>(Person.class); + grid.addColumn("street.name"); + } + + private class Person{ + String name; + Street street; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Street getStreet() { + return street; + } + + public void setStreet(Street street) { + this.street = street; + } + + } + + private class Street{ + String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +} -- cgit v1.2.3