Bean validation with groups doesn't appear to work for constraints in Default group

Description

What I would like to do is define a class with constraints which are all enforced by default, but some select constraints can be relaxed in certain circumstances. This seemed like a perfect application of groups.

So I define the following for example:

public interface Relaxed {};
public interface Complete {};

@GroupSequence({Complete.class, A.class})
class A {
@NotNull
public String a1;

@NotNull(groups=Complete.class)
public String a2;
}

My understanding was that default validation should cause both constraints to be enforced. However with this set up the constraint on a1 is never enforced. If I remove the groups=Complete.class setting from the constraint on a2 then the constraint on a1 is enforced. Why does a2's constraint's groups affect evaluation of constraints on a1?

Even assuming the above behavior is incorrect, then I don't think there's any way to actually achieve what I want since there doesn't seem to be any way to invoke validation such that only the constraint on a1 is enforced. I thought I'd be able to invoke validate as:

A a = new A();
a.a1 = "not null";
a.a2 = null;
violations = validator.validate(a, Relaxed.class, A.class};

kind of like an inline GroupSequence but this throws an exception saying groups can only be interfaces.

I want the default to be such that all constraints are enforced so that consumers of this class only relax the constraints when they know they need to.

So questions are why

1. Why doesn't the @NotNull constraint on a1 ever get enforced?
2. Is there a way to have only constraints with empty group definition enforced?

Cheers,

Tim

PS: The program attached outputs the following for me. I expected to see reported violations for a1 and b1 in the "all nulls" cases.

 

Jun 07, 2017 10:19:19 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.4.1.Final
a (no nulls):
default: no violations
G1: no violations
G2: no violations
a (all nulls):
default: a2 may not be null (was null)
G1: a2 may not be null (was null)
G2: no violations
b (no nulls):
default: no violations
G1: no violations
G2: no violations
b (all nulls):
default: a2 may not be null (was null)
default: b2 may not be null (was null)
G1: a2 may not be null (was null)
G1: b2 may not be null (was null)
G2: no violations
c (not null):
no violations
c (null):
c1 may not be null (was null)

 

Environment

JDK 8 u121, MacOS X, HIbernate Validator 5.1.4.Final

Assignee

Unassigned

Reporter

Tim Bartley

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Suitable for new contributors

None

Pull Request

None

backportDecision

None

backportReEvaluate

None

Components

Affects versions

Priority

Major
Configure