quarkusio/quarkus · error · ConstraintViolationException
(dynamic: constructor return value constraint violations sum
Error message
(dynamic: constructor return value constraint violations summary)
What it means
Thrown by the method-validation interceptor when the object created by a constructor violates return-value constraints (constraints placed on the constructor itself). validateConstructorReturnValue runs after the constructor body completes; the object exists and its constructor side effects are done, but the creation is reported as failed via ConstraintViolationException.
Source
Thrown at extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/interceptor/AbstractMethodValidationInterceptor.java:113
*/
protected void validateConstructorInvocation(InvocationContext ctx) throws Exception {
ExecutableValidator executableValidator = validatorInstance.get().forExecutables();
Set<? extends ConstraintViolation<?>> violations = executableValidator
.validateConstructorParameters(ctx.getConstructor(), ctx.getParameters());
if (!violations.isEmpty()) {
throw new ConstraintViolationException(getMessage(ctx.getConstructor(), ctx.getParameters(), violations),
violations);
}
ctx.proceed();
Object createdObject = ctx.getTarget();
violations = executableValidator.validateConstructorReturnValue(ctx.getConstructor(),
createdObject);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(getMessage(ctx.getConstructor(), ctx.getParameters(), violations),
violations);
}
}
private String getMessage(Member member, Object[] args, Set<? extends ConstraintViolation<?>> violations) {
StringBuilder message = new StringBuilder();
message.append(violations.size());
message.append(" constraint violation(s) occurred during method validation.");
message.append("\nConstructor or Method: ");
message.append(member);
message.append("\nArgument values: ");
message.append(Arrays.toString(args));
message.append("\nConstraint violations: ");
int i = 1;
for (ConstraintViolation<?> constraintViolation : violations) {
Path.Node leafNode = getLeafNode(constraintViolation);View on GitHub (pinned to e1c734241f)
Solutions
- Fix the constructor so the created object fully satisfies the declared constraints in all code paths (initialize all constrained fields).
- Remove the constructor return-value constraint if not all construction paths can guarantee it.
- Check violations' property paths to find the fields left in invalid state.
- Replace constructor-level constraints with explicit validation in a factory method that can return Optional or throw a domain-specific error.
Example fix
// before
@MyConstrained
public Order(String id) { if (id == null) { return; } this.id = id; } // may leave id null
// after
public Order(String id) { this.id = Objects.requireNonNull(id); } Defensive patterns
Strategy: validation
Validate before calling
Set<? extends ConstraintViolation<?>> v = validator.forExecutables().validateConstructorReturnValue(ctor, created);
if (!v.isEmpty()) { /* object state invalid; don't use it */ } Type guard
boolean objectUsable = created != null && created.getId() != null; // mirror constrained fields
Try / catch
try { Order o = factory.create(id); } catch (ConstraintViolationException e) { // constructor ran; discard instance, inspect violations } Prevention
- Ensure constructors initialize every constrained field on all paths
- Prefer fail-fast (Objects.requireNonNull) inside constructors over post-hoc constraint checks
- Test constructors with edge-case inputs
- Avoid constructor-level return constraints unless initialization is guaranteed
When it happens
Trigger: A constructor annotated with return-value constraints (e.g. a @Valid @NotNull constraint on the constructor) builds an object whose state violates those constraints; thrown at AbstractMethodValidationInterceptor.java:112-115 after ctx.proceed().
Common situations: Constructors that leave required fields null (e.g. a field not initialized when a condition branch skips it); refactoring that adds constructor-level constraints on classes whose initialization is incomplete in some paths; factory beans returning half-initialized objects.
Related errors
- (dynamic: constructor parameter constraint violations summar
- (dynamic: parameter constraint violations summary for the in
- (dynamic: return value constraint violations summary for the
- Unable to serialize ${param} as the wrong number of paramete
- Unable to determine the recordable constructor to use for ${
AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05).
Data as JSON: /api/errors/4cd00c94c9a32a7f.
Report an issue: GitHub.