Skip to content

Commit 8e717e4

Browse files
committed
HV-1220 Make programmatically defined cross parameter constraints on the parameters of a method returning void work as expected
The location used was the one of a return value so it kinda worked if the method had parameters as the return value type was Object which is supported for cross parameter constraints but it failed as long as the return value type was void.
1 parent 8b89047 commit 8e717e4

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

engine/src/main/java/org/hibernate/validator/internal/cfg/context/ConfiguredConstraint.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,18 @@ public static <A extends Annotation> ConfiguredConstraint<A> forParameter(Constr
5959
);
6060
}
6161

62-
public static <A extends Annotation> ConfiguredConstraint<A> forExecutable(ConstraintDef<?, A> constraint, ExecutableElement executable) {
62+
public static <A extends Annotation> ConfiguredConstraint<A> forReturnValue(ConstraintDef<?, A> constraint, ExecutableElement executable) {
6363
return new ConfiguredConstraint<A>(
6464
constraint, ConstraintLocation.forReturnValue( executable ), executable.getElementType()
6565
);
6666
}
6767

68+
public static <A extends Annotation> ConfiguredConstraint<A> forCrossParameter(ConstraintDef<?, A> constraint, ExecutableElement executable) {
69+
return new ConfiguredConstraint<A>(
70+
constraint, ConstraintLocation.forCrossParameter( executable ), executable.getElementType()
71+
);
72+
}
73+
6874
public ConstraintDef<?, A> getConstraint() {
6975
return constraint;
7076
}

engine/src/main/java/org/hibernate/validator/internal/cfg/context/CrossParameterConstraintMappingContextImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class CrossParameterConstraintMappingContextImpl
3232

3333
@Override
3434
public CrossParameterConstraintMappingContext constraint(ConstraintDef<?, ?> definition) {
35-
super.addConstraint( ConfiguredConstraint.forExecutable( definition, executableContext.getExecutable() ) );
35+
super.addConstraint( ConfiguredConstraint.forCrossParameter( definition, executableContext.getExecutable() ) );
3636
return this;
3737
}
3838

engine/src/main/java/org/hibernate/validator/internal/cfg/context/PropertyConstraintMappingContextImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public PropertyConstraintMappingContext constraint(ConstraintDef<?, ?> definitio
6262
}
6363
else {
6464
super.addConstraint(
65-
ConfiguredConstraint.forExecutable(
65+
ConfiguredConstraint.forReturnValue(
6666
definition, ExecutableElement.forMethod( (Method) member )
6767
)
6868
);

engine/src/main/java/org/hibernate/validator/internal/cfg/context/ReturnValueConstraintMappingContextImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected ReturnValueConstraintMappingContext getThis() {
3939

4040
@Override
4141
public ReturnValueConstraintMappingContext constraint(ConstraintDef<?, ?> definition) {
42-
super.addConstraint( ConfiguredConstraint.forExecutable( definition, executableContext.getExecutable() ) );
42+
super.addConstraint( ConfiguredConstraint.forReturnValue( definition, executableContext.getExecutable() ) );
4343
return this;
4444
}
4545

engine/src/test/java/org/hibernate/validator/test/cfg/MethodConstraintMappingTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,38 @@ public void crossParameterConstraint() {
666666
}
667667
}
668668

669+
@Test
670+
@TestForIssue(jiraKey = "HV-1220")
671+
public void crossParameterConstraintOnMethodReturningVoid() {
672+
ConstraintMapping mapping = config.createConstraintMapping();
673+
mapping.type( GreetingService.class )
674+
.method( "sayNothing", String.class )
675+
.crossParameter()
676+
.constraint(
677+
new GenericConstraintDef<GenericAndCrossParameterConstraint>(
678+
GenericAndCrossParameterConstraint.class
679+
)
680+
);
681+
config.addMapping( mapping );
682+
683+
try {
684+
GreetingService service = getValidatingProxy(
685+
wrappedObject,
686+
config.buildValidatorFactory().getValidator()
687+
);
688+
service.sayNothing( "" );
689+
690+
fail( "Expected exception wasn't thrown." );
691+
}
692+
catch (ConstraintViolationException e) {
693+
694+
assertCorrectConstraintViolationMessages(
695+
e, "default message"
696+
);
697+
assertCorrectPropertyPaths( e, "sayNothing.<cross-parameter>" );
698+
}
699+
}
700+
669701
private interface TestGroup {
670702
}
671703

@@ -708,6 +740,8 @@ public interface GreetingService {
708740

709741
User getUser();
710742

743+
void sayNothing(String string1);
744+
711745
}
712746

713747
public class GreetingServiceImpl implements GreetingService {
@@ -747,5 +781,10 @@ public Message getHello() {
747781
public User getUser() {
748782
return new User( null );
749783
}
784+
785+
@Override
786+
public void sayNothing(String string1) {
787+
// Nothing to do
788+
}
750789
}
751790
}

0 commit comments

Comments
 (0)