Fix #398: Add portable ConstraintViolationExceptionMapper for MVC validation on OpenLiberty#399
Fix #398: Add portable ConstraintViolationExceptionMapper for MVC validation on OpenLiberty#399ivargrimstad wants to merge 3 commits intoeclipse-ee4j:mainfrom
Conversation
…ndling (eclipse-ee4j#398) On JAX-RS runtimes without a Krazo-specific mechanism to suppress built-in bean validation for MVC controllers (such as OpenLiberty/CXF), the runtime throws ConstraintViolationException before Krazo's CDI ValidationInterceptor can run. This mapper catches the exception, populates BindingResult, and invokes the controller via CDI so it can handle validation errors. Changes: - New ConstraintViolationExceptionMapper in core module - BindingResultImpl: add validationPerformedExternally flag - ValidationInterceptor: skip re-validation when flag is set - DefaultConfigProvider: register the new ExceptionMapper Agent-Logs-Url: https://github.com/ivargrimstad/krazo/sessions/4879b273-1607-488f-be1e-c18610e7c87c Co-authored-by: ivargrimstad <149188+ivargrimstad@users.noreply.github.com>
Agent-Logs-Url: https://github.com/ivargrimstad/krazo/sessions/4879b273-1607-488f-be1e-c18610e7c87c Co-authored-by: ivargrimstad <149188+ivargrimstad@users.noreply.github.com>
|
@ivargrimstad Would you like me to try and build this branch locally to see if it works? Yes, that would be awesome! I must admit I let GitHub CoPilot do all the work and I just haven't had time to test it yet since I am a little busy at OCX this week. |
|
Tried building locally but ran into this error:
I see Jenkins failed with the same error. @ivargrimstad, does that make any bell ring, maybe? Update: as a work-around, I set |
|
I've tried it on the attached reproducer project. The Here is the full stack trace: |
Summary
Fixes #398
On OpenLiberty (and other JAX-RS runtimes that don't have a Krazo-specific mechanism to suppress built-in bean validation), the JAX-RS runtime validates controller method parameters and throws
ConstraintViolationExceptionbefore Krazo's CDIValidationInterceptorcan run. This results in a 400 Bad Request response instead of populatingBindingResultand invoking the controller.Jersey and RESTEasy have runtime-specific hooks (
KrazoValidationInterceptorandKrazoGeneralValidator) that suppress this premature validation, but there was no equivalent for OpenLiberty/CXF.Solution
This PR adds a portable
ExceptionMapper<ConstraintViolationException>in the core module that:ResourceInfoand@ControllerannotationBindingResultwith@MvcBinding-annotated constraint violations (reusing the same logic asValidationInterceptor)BindingResult.isFailed()and return the appropriate viewValidationInterceptorvia avalidationPerformedExternallyflag onBindingResultImplViewableentity for view rendering, or handles redirectsFor non-MVC requests, the mapper returns a standard 400 Bad Request response (same as the default JAX-RS behavior).
Files Changed
ConstraintViolationExceptionMapper.java(new) — The core ExceptionMapperBindingResultImpl.java— AddedvalidationPerformedExternallyflagValidationInterceptor.java— Skips validation when flag is setDefaultConfigProvider.java— Registers the new ExceptionMapperLimitations
BindingResult.isFailed()before using parameter values, which is the expected MVC pattern.