Skip to content
Browse files

HV-902 Validating properties of Iterable and Map subclasses when perf…

…orming cascaded validation
  • Loading branch information...
1 parent fcd7667 commit 9f83f54e2bfbb4e897423705cfbf22f210c52646 @khalidq khalidq committed Jul 8, 2014
View
27 engine/src/main/java/org/hibernate/validator/internal/engine/ValidatorImpl.java
@@ -565,9 +565,6 @@ private void validateCascadedConstraints(ValidationContext<?> validationContext,
}
if ( value != null ) {
- Type type = value.getClass();
- Iterator<?> iter = createIteratorForCascadedValue( type, value, valueContext );
- boolean isIndexable = isIndexable( type );
// expand the group only if was created by group conversion;
// otherwise we're looping through the right validation order
@@ -577,9 +574,24 @@ private void validateCascadedConstraints(ValidationContext<?> validationContext,
group != originalGroup
);
+ // HV-902: First, validate the value itself
+ Iterator<?> valueIter = Arrays.asList( value ).iterator();
validateCascadedConstraint(
validationContext,
- iter,
+ valueIter,
+ false,
+ valueContext,
+ validationOrder
+ );
+
+ // Second, validate elements contained in the value if it is Iterable, Map, or an Array
+ Type type = value.getClass();
+ Iterator<?> elementsIter = createIteratorForCascadedValue( type, value, valueContext );
+ boolean isIndexable = isIndexable( type );
+
+ validateCascadedConstraint(
+ validationContext,
+ elementsIter,
isIndexable,
valueContext,
validationOrder
@@ -607,7 +619,7 @@ private void validateCascadedConstraints(ValidationContext<?> validationContext,
* @return An iterator over the value of a cascaded property.
*/
private Iterator<?> createIteratorForCascadedValue(Type type, Object value, ValueContext<?, ?> valueContext) {
- Iterator<?> iter;
+ Iterator<?> iter = Collections.emptyIterator();
if ( ReflectionHelper.isIterable( type ) ) {
iter = ( (Iterable<?>) value ).iterator();
valueContext.markCurrentPropertyAsIterable();
@@ -622,11 +634,6 @@ else if ( TypeHelper.isArray( type ) ) {
iter = arrayList.iterator();
valueContext.markCurrentPropertyAsIterable();
}
- else {
- List<Object> list = newArrayList();
- list.add( value );
- iter = list.iterator();
- }
return iter;
}
View
92 .../hibernate/validator/test/internal/engine/cascaded/CascadedIterableMapPropertiesTest.java
@@ -0,0 +1,92 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+* http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.validator.test.internal.engine.cascaded;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Valid;
+import javax.validation.Validator;
+import javax.validation.constraints.Min;
+import javax.validation.constraints.NotNull;
+
+import org.testng.annotations.Test;
+
+import org.hibernate.validator.testutil.TestForIssue;
+
+import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertCorrectPropertyPaths;
+import static org.hibernate.validator.testutil.ValidatorUtil.getValidator;
+import static org.testng.Assert.assertEquals;
+
+/**
+ * @author Khalid Alqinyah
+ */
+public class CascadedIterableMapPropertiesTest {
+
+ @Test
+ @TestForIssue( jiraKey = "HV-902")
+ public void testValidateCascadingIterableAndMapProperties() {
+ Validator validator = getValidator();
+ Set<ConstraintViolation<CascadingIterMap>> constraintViolations = validator.validate( new CascadingIterMap() );
+ assertEquals( constraintViolations.size(), 4 );
+ assertCorrectPropertyPaths( constraintViolations,
+ "iterableExt.value",
+ "iterableExt[].number",
+ "mapExt.value",
+ "mapExt[second].number" );
+ }
+
+ class CascadingIterMap {
+ @Valid
+ IterableExt iterableExt = new IterableExt();
+
+ @Valid
+ MapExt mapExt = new MapExt();
+ }
+
+ class IterableExt implements Iterable<IntWrapper> {
+ @NotNull
+ Integer value = null;
+
+ @Override
+ public Iterator<IntWrapper> iterator() {
+ return Arrays.asList( new IntWrapper( 2 ), new IntWrapper( 1 ), new IntWrapper( 5 ) ).iterator();
+ }
+ }
+
+ class MapExt extends HashMap<String, IntWrapper> {
+ @NotNull
+ Integer value = null;
+
+ public MapExt() {
+ this.put( "first", new IntWrapper( 2 ) );
+ this.put( "second", new IntWrapper( 1 ) );
+ this.put( "third", new IntWrapper( 4 ) );
+ }
+ }
+
+ class IntWrapper {
+ @Min(value = 2)
+ Integer number;
+
+ public IntWrapper(Integer number) {
+ this.number = number;
+ }
+ }
+}

0 comments on commit 9f83f54

Please sign in to comment.
Something went wrong with that request. Please try again.