Skip to content

Commit

Permalink
HV-1713 Convert current group before checking if the bean is processed
Browse files Browse the repository at this point in the history
- We need to convert the group before checking if the bean was processed or not as group defines the processed status
  • Loading branch information
marko-bekhta authored and gsmet committed May 27, 2019
1 parent 37c322c commit 4678464
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -606,14 +606,16 @@ private void validateCascadedConstraints(ValidationContext<?> validationContext,

private void validateCascadedAnnotatedObjectForCurrentGroup(Object value, ValidationContext<?> validationContext, ValueContext<?, Object> valueContext,
CascadingMetaData cascadingMetaData) {
if ( validationContext.isBeanAlreadyValidated( value, valueContext.getCurrentGroup(), valueContext.getPropertyPath() ) ||
// We need to convert the group before checking if the bean was processed or not
// as group defines the processed status.
Class<?> originalGroup = valueContext.getCurrentGroup();
Class<?> currentGroup = cascadingMetaData.convertGroup( originalGroup );

if ( validationContext.isBeanAlreadyValidated( value, currentGroup, valueContext.getPropertyPath() ) ||
shouldFailFast( validationContext ) ) {
return;
}

Class<?> originalGroup = valueContext.getCurrentGroup();
Class<?> currentGroup = cascadingMetaData.convertGroup( originalGroup );

// expand the group only if was created by group conversion;
// otherwise we're looping through the right validation order
// already and need only to pass the current element
Expand Down Expand Up @@ -683,15 +685,17 @@ public void keyedValue(String nodeName, Object key, Object value) {
}

private void doValidate(Object value, String nodeName) {
// We need to convert the group before checking if the bean was processed or not
// as group defines the processed status.
Class<?> originalGroup = valueContext.getCurrentGroup();
Class<?> currentGroup = cascadingMetaData.convertGroup( originalGroup );

if ( value == null ||
validationContext.isBeanAlreadyValidated( value, valueContext.getCurrentGroup(), valueContext.getPropertyPath() ) ||
validationContext.isBeanAlreadyValidated( value, currentGroup, valueContext.getPropertyPath() ) ||
shouldFailFast( validationContext ) ) {
return;
}

Class<?> originalGroup = valueContext.getCurrentGroup();
Class<?> currentGroup = cascadingMetaData.convertGroup( originalGroup );

// expand the group only if was created by group conversion;
// otherwise we're looping through the right validation order
// already and need only to pass the current element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import javax.validation.GroupSequence;
import javax.validation.Valid;
import javax.validation.Validator;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.validation.groups.ConvertGroup;
import javax.validation.groups.Default;

import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
import org.hibernate.validator.testutils.CandidateForTck;
import org.testng.annotations.Test;

/**
Expand Down Expand Up @@ -214,6 +216,19 @@ public void conversionFromSequenceCausesException() {
validator.validate( new User8() );
}

@Test
@CandidateForTck
public void sameBeanDifferentGroups() {
Set<ConstraintViolation<User9>> violations = validator.validate( new User9() );
assertThat( violations ).containsOnlyViolations(
violationOf( AssertTrue.class ).withPropertyPath( pathWith()
.property( "a" )
.property( "b" )
)
);
}


public interface Complete extends Default {
}

Expand Down Expand Up @@ -341,4 +356,12 @@ private static class User8 {
@ConvertGroup(from = PostalSequence.class, to = BasicPostal.class)
private final List<Address> addresses = Arrays.asList( new Address() );
}

private static class User9 {
@Valid
@ConvertGroup(from = Default.class, to = BasicNumber.class)
User9 a = this;
@AssertTrue(groups = BasicNumber.class)
boolean b;
}
}

0 comments on commit 4678464

Please sign in to comment.