{"record":{"id":"e1d44c3645b28350","repo":"quarkusio/quarkus","slug":"dynamic-return-value-constraint-violations-summa","errorCode":null,"errorMessage":"(dynamic: return value constraint violations summary for the invoked method)","messagePattern":"\\(dynamic: return value constraint violations summary for the invoked method\\)","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":78,"sourceCode":"     *         return value validation.\n     */\n    protected Object validateMethodInvocation(InvocationContext ctx) throws Exception {\n\n        ExecutableValidator executableValidator = validatorInstance.get().forExecutables();\n        Set<ConstraintViolation<Object>> violations = executableValidator.validateParameters(ctx.getTarget(),\n                ctx.getMethod(), ctx.getParameters());\n\n        if (!violations.isEmpty()) {\n            throw new ConstraintViolationException(getMessage(ctx.getMethod(), ctx.getParameters(), violations),\n                    violations);\n        }\n\n        Object result = ctx.proceed();\n\n        violations = executableValidator.validateReturnValue(ctx.getTarget(), ctx.getMethod(), result);\n\n        if (!violations.isEmpty()) {\n            throw new ConstraintViolationException(getMessage(ctx.getMethod(), ctx.getParameters(), violations),\n                    violations);\n        }\n\n        return result;\n    }\n\n    /**\n     * Validates the Bean Validation constraints specified at the parameters and/or\n     * return value of the intercepted constructor.\n     *\n     * @param ctx The context of the intercepted constructor invocation.\n     *\n     * @throws Exception Any exception caused by the intercepted constructor\n     *         invocation. A {@link ConstraintViolationException} in case\n     *         at least one constraint violation occurred either during\n     *         parameter or return value validation.\n     */\n    protected void validateConstructorInvocation(InvocationContext ctx) throws Exception {","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/interceptor/AbstractMethodValidationInterceptor.java#L60-L96","documentation":"This ConstraintViolationException is thrown by the method-validation interceptor AFTER the method body has already executed and returned, when ExecutableValidator.validateReturnValue finds the returned value violates constraints declared on the method (e.g. @NotNull on the return type or on elements of the returned collection). The method's side effects already happened even though its result is rejected.","triggerScenarios":"A validated bean method returns a value violating its return-value constraints (e.g. returns null where @NotNull return type is declared, or a list containing null elements under @NotNull List<@NotNull T>); thrown at AbstractMethodValidationInterceptor.java:77-80 after ctx.proceed().","commonSituations":"A repository method annotated with @NotNull return returns null when a row is missing; a service returning an optional result as null; adding return-type constraints to legacy methods whose implementations sometimes return null.","solutions":["Fix the bean implementation so the returned value satisfies the declared constraints (return Optional instead of null, filter invalid elements).","Relax or remove the return-value constraint annotation if returning null/invalid values is legitimate.","Check the violation set for the exact property path; if cascading (@Valid) is involved, fix the nested object's fields.","For query-style methods, use Optional<T> return types instead of @NotNull + null."],"exampleFix":"// before\n@NotNull\npublic User find(String id) { return repo.find(id); } // may return null\n\n// after\npublic Optional<User> find(String id) { return Optional.ofNullable(repo.find(id)); }","handlingStrategy":"validation","validationCode":"Set<ConstraintViolation<Object>> v = validator.forExecutables().validateReturnValue(target, method, result);\nif (!v.isEmpty()) { /* result violates declared constraints */ }","typeGuard":"boolean isValidResult = result != null; // for a @NotNull-returning method call site","tryCatchPattern":"try { User u = service.find(id); } catch (ConstraintViolationException e) { // side effects may have occurred; inspect e.getConstraintViolations() }","preventionTips":["Avoid @NotNull on methods that can legitimately find nothing; prefer Optional","Unit-test validated methods' return paths including null/empty cases","Remember the method body already ran — design methods so a rejected return value is not harmful","Cascade @Valid only where nested constraint reports are useful"],"tags":["bean-validation","constraint-violation","return-value","method-validation"],"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-12T22:17:10.623Z"}