0

Scenario : Quarkus version 2.16.5Final , RestEasy Rest End point which uses Hibernate validation for validation.

Incoming Request POJO

@Valid class Employee{ @NotEmpty String empId; String deptId; }

Upon receiving such a request I copy this over to a model class within my application code. The model class looks like this :

@Valid class EmployeeModel{ @NotEmpty String empId;  String deptId; }

Now if I happen to have a bug in my application code due to which I end up not copying the field 'empId' from the request to the model class what I see is that Quarkus is enforcing the validation on the model class and as a result rejecting the request with Status Code 400 Bad Request.

This seems wrong. Shouldn't Hibernate validation in Quarkus applications only be applied on the incoming requests ? And even if they are applied inside application code a rejection arising out of this should not be rejected with 400 Status code, right ?

Kiran K
  • 703
  • 4
  • 11

1 Answers1

1

Shouldn't Hibernate validation in Quarkus applications only be applied on the incoming requests?

No. Bean Validation and Restful Web Services are totally independent standards. Bean Validation should applied to all annotated POJOs, fields, etc. Just imagine you have a service which does not have REST endpoint, perhaps it is listening on an AMQP channel. That incoming message should be validated also.

And even if they are applied inside application code a rejection arising out of this should not be rejected with 400 Status code, right?

It happens when you didn't handle the ConstraintViolationExceptions inside application code. RestEasy (Classic) and Resteasy Reactive extensions provide an ExceptionMapper to transform unhandled ConstraintViolationExceptions to a Response object. So you should handle theese exceptions if you have to need different HTTP status or different Response.

zforgo
  • 2,508
  • 2
  • 14
  • 22