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

  1. Fix the constructor so the created object fully satisfies the declared constraints in all code paths (initialize all constrained fields).
  2. Remove the constructor return-value constraint if not all construction paths can guarantee it.
  3. Check violations' property paths to find the fields left in invalid state.
  4. 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

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


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/4cd00c94c9a32a7f. Report an issue: GitHub.