{"record":{"id":"4cd00c94c9a32a7f","repo":"quarkusio/quarkus","slug":"dynamic-constructor-return-value-constraint-viol","errorCode":null,"errorMessage":"(dynamic: constructor return value constraint violations summary)","messagePattern":"\\(dynamic: constructor return value constraint violations summary\\)","errorType":"validation","errorClass":"ConstraintViolationException","httpStatus":null,"severity":"error","filePath":"extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/interceptor/AbstractMethodValidationInterceptor.java","lineNumber":113,"sourceCode":"     */\n    protected void validateConstructorInvocation(InvocationContext ctx) throws Exception {\n        ExecutableValidator executableValidator = validatorInstance.get().forExecutables();\n        Set<? extends ConstraintViolation<?>> violations = executableValidator\n                .validateConstructorParameters(ctx.getConstructor(), ctx.getParameters());\n\n        if (!violations.isEmpty()) {\n            throw new ConstraintViolationException(getMessage(ctx.getConstructor(), ctx.getParameters(), violations),\n                    violations);\n        }\n\n        ctx.proceed();\n        Object createdObject = ctx.getTarget();\n\n        violations = executableValidator.validateConstructorReturnValue(ctx.getConstructor(),\n                createdObject);\n\n        if (!violations.isEmpty()) {\n            throw new ConstraintViolationException(getMessage(ctx.getConstructor(), ctx.getParameters(), violations),\n                    violations);\n        }\n    }\n\n    private String getMessage(Member member, Object[] args, Set<? extends ConstraintViolation<?>> violations) {\n\n        StringBuilder message = new StringBuilder();\n        message.append(violations.size());\n        message.append(\" constraint violation(s) occurred during method validation.\");\n        message.append(\"\\nConstructor or Method: \");\n        message.append(member);\n        message.append(\"\\nArgument values: \");\n        message.append(Arrays.toString(args));\n        message.append(\"\\nConstraint violations: \");\n\n        int i = 1;\n        for (ConstraintViolation<?> constraintViolation : violations) {\n            Path.Node leafNode = getLeafNode(constraintViolation);","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/interceptor/AbstractMethodValidationInterceptor.java#L95-L131","documentation":"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.","triggerScenarios":"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().","commonSituations":"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.","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."],"exampleFix":"// before\n@MyConstrained\npublic Order(String id) { if (id == null) { return; } this.id = id; } // may leave id null\n\n// after\npublic Order(String id) { this.id = Objects.requireNonNull(id); }","handlingStrategy":"validation","validationCode":"Set<? extends ConstraintViolation<?>> v = validator.forExecutables().validateConstructorReturnValue(ctor, created);\nif (!v.isEmpty()) { /* object state invalid; don't use it */ }","typeGuard":"boolean objectUsable = created != null && created.getId() != null; // mirror constrained fields","tryCatchPattern":"try { Order o = factory.create(id); } catch (ConstraintViolationException e) { // constructor ran; discard instance, inspect violations }","preventionTips":["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"],"tags":["bean-validation","constraint-violation","constructor"],"backgroundTag":"constraint-violation-exception","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}