ClassCastException in ValidationException.isAlreadyValidatedForCurrentGroup

Description

Wen trying to validate a pretty large list of beans, I sometimes encounter a ClassCastException in the ValidationException.isAlreadyValidatedForCurrentGroup method.

The issue is that we have two types of objects (BeanPathMetaConstraintProcessedUnit and BeanGroupProcessedUnit) in the processedUnits Set. Sometimes two instances of theses classes have the same hashcode value. When applying the equals method, I encounter a ClassCastException.

This part of the equals method is the problem :

1 2 3 4 5 if ( o == null || getClass() != BeanPathMetaConstraintProcessedUnit.class ) { return false; } BeanPathMetaConstraintProcessedUnit that = (BeanPathMetaConstraintProcessedUnit) o; // ClassCast because it's an instance of BeanGroupProcessedUnit

I think, the comparison should be o.getClass != BeanGroupProcessedUnit.class. There is the same problem in the BeanGroupProcessedUnit equals method.

Here is the stack trace:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 java.lang.ClassCastException: org.hibernate.validator.internal.engine.ValidationContext$BeanPathMetaConstraintProcessedUnit cannot be cast to org.hibernate.validator.internal.engine.ValidationContext$BeanGroupProcessedUnit at org.hibernate.validator.internal.engine.ValidationContext$BeanGroupProcessedUnit.equals(ValidationContext.java:620) at java.util.HashMap.getNode(HashMap.java:571) at java.util.HashMap.containsKey(HashMap.java:595) at java.util.HashSet.contains(HashSet.java:203) at org.hibernate.validator.internal.engine.ValidationContext.isAlreadyValidatedForCurrentGroup(ValidationContext.java:440) at org.hibernate.validator.internal.engine.ValidationContext.isBeanAlreadyValidated(ValidationContext.java:269) at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedAnnotatedObjectForCurrentGroup(ValidatorImpl.java:626) at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedConstraints(ValidatorImpl.java:604) at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:420) at org.hibernate.validator.internal.engine.ValidatorImpl.access$300(ValidatorImpl.java:85) at org.hibernate.validator.internal.engine.ValidatorImpl$CascadingValueReceiver.doValidate(ValidatorImpl.java:723) at org.hibernate.validator.internal.engine.ValidatorImpl$CascadingValueReceiver.indexedValue(ValidatorImpl.java:691) at org.hibernate.validator.internal.engine.valueextraction.ListValueExtractor.extractValues(ListValueExtractor.java:26) at org.hibernate.validator.internal.engine.valueextraction.ListValueExtractor.extractValues(ListValueExtractor.java:16) at org.hibernate.validator.internal.engine.valueextraction.ValueExtractorHelper.extractValues(ValueExtractorHelper.java:41)

Environment

6.0.4.Final, Java 8, BeanValidation 2.0

Status

Assignee

Unassigned

Reporter

Mickael Coquer

Labels

None

Worked in

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Community Help Wanted

None

Suitable for new contributors

Yes, likely

Requires Release Note

None

Pull Request

None

backportDecision

None

backportReEvaluate

None

Components

Affects versions

6.0.4.Final

Priority

Major