micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6.07k stars 1.07k forks source link

micronaut-validator validates Optional.empty() as null when using @NotNull #2723

Closed flexguse closed 4 years ago

flexguse commented 4 years ago

Thanks for reporting an issue for Micronaut, please review the task list below before submitting the issue. Your issue report will be closed if the issue is incomplete and the below tasks not completed.

NOTE: If you are unsure about something and the issue is more of a question a better place to ask questions is on Stack Overflow (http://stackoverflow.com/tags/micronaut) or Gitter (https://gitter.im/micronautfw/). DO NOT use the issue tracker to ask questions.

Task List

Steps to Reproduce

  1. Annotate a method attribute as @NotNull
  2. put Optional.empty() in it
  3. get a unexpected ConstraintViolationException

Just run the tests in the example project to reproduce.

Expected Behaviour

Optional.empty() is something different than null and should not cause a ConstraintViolationException exception.

Actual Behaviour

Optional.empty() causes a ConstraintViolationException: greet.name: the greeting name is required javax.validation.ConstraintViolationException: greet.name: the greeting name is required at io.micronaut.validation.ValidatingInterceptor.intercept(ValidatingInterceptor.java:143) at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:69) at de.flexguse.micronaut.demo.service.ExampleServiceImplTest.should greet #name(ExampleServiceImplTest.groovy:21)

Environment Information

Example Application

https://github.com/flexguse/micronaut-optional-validation-demo

jameskleeh commented 4 years ago

For optionals the constraints are applied to the value inside. This is by design. Duplicates https://github.com/micronaut-projects/micronaut-core/issues/2608