Hibernate SVN: r17223 - validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine and 1 other directories.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2009-08-01 15:58:59 -0400 (Sat, 01 Aug 2009)
New Revision: 17223
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateValueTest.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaData.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaDataImpl.java
Log:
HV-196 validateValue / validateProperty now let properties wo constraint pass wo raising IAE
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateValueTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateValueTest.java 2009-07-31 17:53:18 UTC (rev 17222)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateValueTest.java 2009-08-01 19:58:59 UTC (rev 17223)
@@ -18,8 +18,11 @@
package org.hibernate.jsr303.tck.tests.validation;
import java.util.Set;
+import java.util.ArrayList;
+import java.util.List;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
+import javax.validation.constraints.NotNull;
import org.jboss.test.audit.annotations.SpecAssertion;
import org.jboss.testharness.AbstractTest;
@@ -120,6 +123,13 @@
}
@Test
+ @SpecAssertion(section = "4.1.1", id = "i")
+ public void testExistingPropertyWoConstraintsNorCascaded() {
+ Validator validator = TestUtil.getValidatorUnderTest();
+ validator.validateValue( Customer.class, "middleName", new ArrayList<String>() );
+ }
+
+ @Test
@SuppressWarnings("NullArgumentToVariableArgMethod")
@SpecAssertion(section = "4.1.1", id = "i")
public void testValidateValuePassingNullAsGroup() {
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java 2009-07-31 17:53:18 UTC (rev 17222)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java 2009-08-01 19:58:59 UTC (rev 17223)
@@ -677,16 +677,15 @@
Path.Node elem = propertyIter.next();
final BeanMetaData<T> metaData = getBeanMetaData( clazz );
- if ( !propertyIter.hasNext() ) {
+ //use precomputed method list as ReflectionHelper#containsMember is slow
+ if ( ! metaData.isPropertyPresent( elem.getName() ) ) {
+ throw new IllegalArgumentException(
+ "Invalid property path. There is no property " + elem.getName() + " in entity " + metaData.getBeanClass()
+ .getName()
+ );
+ }
- //use metadata first as ReflectionHelper#containsMember is slow
- if ( metaData.getPropertyDescriptor( elem.getName() ) == null ) {
- throw new IllegalArgumentException(
- "Invalid property path. There is no property " + elem.getName() + " in entity " + metaData.getBeanClass()
- .getName()
- );
- }
-
+ if ( !propertyIter.hasNext() ) {
List<MetaConstraint<T, ? extends Annotation>> metaConstraintList = metaData.geMetaConstraintsAsList();
for ( MetaConstraint<T, ?> metaConstraint : metaConstraintList ) {
if ( elem.getName() != null && elem.getName().equals( metaConstraint.getPropertyName() ) ) {
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaData.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaData.java 2009-07-31 17:53:18 UTC (rev 17222)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaData.java 2009-08-01 19:58:59 UTC (rev 17223)
@@ -80,6 +80,12 @@
PropertyDescriptor getPropertyDescriptor(String property);
/**
+ * return true if the property exists on the object
+ * even if the property does not host any constraint nor is cascaded
+ */
+ boolean isPropertyPresent(String name);
+
+ /**
* @return the property descriptors having at least one constraint defined or which are marked
* as cascaded (@Valid).
*/
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaDataImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaDataImpl.java 2009-07-31 17:53:18 UTC (rev 17222)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaDataImpl.java 2009-08-01 19:58:59 UTC (rev 17223)
@@ -31,6 +31,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
import javax.validation.GroupDefinitionException;
import javax.validation.GroupSequence;
import javax.validation.Valid;
@@ -92,6 +93,9 @@
*/
private final ConstraintHelper constraintHelper;
+ //updated on the fly, needs to be thread safe
+ //property name
+ private final Set<String> propertyNames = new HashSet<String>(30);
public BeanMetaDataImpl(Class<T> beanClass, ConstraintHelper constraintHelper) {
this(
@@ -151,6 +155,10 @@
return propertyDescriptors.get( property );
}
+ public boolean isPropertyPresent(String name) {
+ return propertyNames.contains( name ); //To change body of implemented methods use File | Settings | File Templates.
+ }
+
public List<Class<?>> getDefaultGroupSequence() {
return Collections.unmodifiableList( defaultGroupSequence );
}
@@ -255,6 +263,10 @@
if ( Modifier.isStatic( field.getModifiers() ) ) {
continue;
}
+ String name = ReflectionHelper.getPropertyName( field );
+ if (name != null) {
+ propertyNames.add( name );
+ }
List<ConstraintDescriptorImpl<?>> fieldMetadata = findConstraints( field );
for ( ConstraintDescriptorImpl<?> constraintDescription : fieldMetadata ) {
if ( annotationIgnores.isIgnoreAnnotations( field ) ) {
@@ -278,6 +290,11 @@
if ( Modifier.isStatic( method.getModifiers() ) ) {
continue;
}
+ String name = ReflectionHelper.getPropertyName( method );
+ if (name != null) {
+ propertyNames.add( name );
+ }
+
List<ConstraintDescriptorImpl<?>> methodMetadata = findConstraints( method );
for ( ConstraintDescriptorImpl<?> constraintDescription : methodMetadata ) {
if ( annotationIgnores.isIgnoreAnnotations( method ) ) {
15 years, 4 months