aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java
diff options
context:
space:
mode:
authorArtur <artur@vaadin.com>2017-05-08 14:37:21 +0300
committerHenri Sara <henri.sara@gmail.com>2017-05-08 14:37:21 +0300
commita9fdc693a081c42018b7808b077c4b19337659fa (patch)
tree55d9a679b7fb106c47ec061e262277f19596b110 /server/src/main/java
parent70d5940893075be7afb268b52904825cf55c1460 (diff)
downloadvaadin-framework-a9fdc693a081c42018b7808b077c4b19337659fa.tar.gz
vaadin-framework-a9fdc693a081c42018b7808b077c4b19337659fa.zip
Fix bean validation when using sub property bindings (#9248)
Fixes #9242
Diffstat (limited to 'server/src/main/java')
-rw-r--r--server/src/main/java/com/vaadin/data/BeanPropertySet.java22
-rw-r--r--server/src/main/java/com/vaadin/data/BeanValidationBinder.java32
2 files changed, 48 insertions, 6 deletions
diff --git a/server/src/main/java/com/vaadin/data/BeanPropertySet.java b/server/src/main/java/com/vaadin/data/BeanPropertySet.java
index f78945e9d1..0e94b14761 100644
--- a/server/src/main/java/com/vaadin/data/BeanPropertySet.java
+++ b/server/src/main/java/com/vaadin/data/BeanPropertySet.java
@@ -21,7 +21,6 @@ import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@@ -201,7 +200,17 @@ public class BeanPropertySet<T> implements PropertySet<T> {
}
}
- private static class NestedBeanPropertyDefinition<T, V>
+ /**
+ * Contains properties for a bean type which is nested in another
+ * definition.
+ *
+ * @since
+ * @param <T>
+ * the bean type
+ * @param <V>
+ * the value type returned by the getter and set by the setter
+ */
+ public static class NestedBeanPropertyDefinition<T, V>
extends AbstractBeanPropertyDefinition<T, V> {
private final PropertyDefinition<T, ?> parent;
@@ -250,6 +259,15 @@ public class BeanPropertySet<T> implements PropertySet<T> {
return new SerializedPropertyDefinition(getPropertySet().beanType,
parent.getName() + "." + getName());
}
+
+ /**
+ * Gets the parent property definition.
+ *
+ * @return the property definition for the parent
+ */
+ public PropertyDefinition<T, ?> getParent() {
+ return parent;
+ }
}
private static final ConcurrentMap<Class<?>, BeanPropertySet<?>> instances = new ConcurrentHashMap<>();
diff --git a/server/src/main/java/com/vaadin/data/BeanValidationBinder.java b/server/src/main/java/com/vaadin/data/BeanValidationBinder.java
index c152a67b0c..e125bc9862 100644
--- a/server/src/main/java/com/vaadin/data/BeanValidationBinder.java
+++ b/server/src/main/java/com/vaadin/data/BeanValidationBinder.java
@@ -19,6 +19,7 @@ import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.metadata.PropertyDescriptor;
+import com.vaadin.data.BeanPropertySet.NestedBeanPropertyDefinition;
import com.vaadin.data.util.BeanUtil;
import com.vaadin.data.validator.BeanValidator;
@@ -69,7 +70,7 @@ public class BeanValidationBinder<BEAN> extends Binder<BEAN> {
* <p>
* By default the {@link RequiredFieldConfigurator#DEFAULT} configurator is
* used.
- *
+ *
* @param configurator
* required indicator configurator, may be {@code null}
*/
@@ -80,9 +81,9 @@ public class BeanValidationBinder<BEAN> extends Binder<BEAN> {
/**
* Gets field required indicator configuration logic.
- *
+ *
* @see #setRequiredConfigurator(RequiredFieldConfigurator)
- *
+ *
* @return required indicator configurator, may be {@code null}
*/
public RequiredFieldConfigurator getRequiredConfigurator() {
@@ -93,7 +94,8 @@ public class BeanValidationBinder<BEAN> extends Binder<BEAN> {
protected BindingBuilder<BEAN, ?> configureBinding(
BindingBuilder<BEAN, ?> binding,
PropertyDefinition<BEAN, ?> definition) {
- BeanValidator validator = new BeanValidator(beanType,
+ Class<?> actualBeanType = findBeanType(beanType, definition);
+ BeanValidator validator = new BeanValidator(actualBeanType,
definition.getName());
if (requiredConfigurator != null) {
configureRequired(binding, definition, validator);
@@ -101,6 +103,28 @@ public class BeanValidationBinder<BEAN> extends Binder<BEAN> {
return binding.withValidator(validator);
}
+ /**
+ * Finds the bean type containing the property the given definition refers
+ * to.
+ *
+ * @param beanType
+ * the root beanType
+ * @param definition
+ * the definition for the property
+ * @return the bean type containing the given property
+ */
+ @SuppressWarnings({ "rawtypes" })
+ private Class<?> findBeanType(Class<BEAN> beanType,
+ PropertyDefinition<BEAN, ?> definition) {
+ if (definition instanceof NestedBeanPropertyDefinition) {
+ return ((NestedBeanPropertyDefinition) definition).getParent()
+ .getType();
+ } else {
+ // Non nested properties must be defined in the main type
+ return beanType;
+ }
+ }
+
private void configureRequired(BindingBuilder<BEAN, ?> binding,
PropertyDefinition<BEAN, ?> definition, BeanValidator validator) {
assert requiredConfigurator != null;