0

we upgraded from HV 4.x to HV 5.4.2 and now when we have interface like following

@NotNull
List<AccountInfo> getMultiClientAccountBalances(@NotNull ClientContext clientContext, @NotNull Optional<AccountFilter> accountFilter);

I'm getting error:

javax.validation.UnexpectedTypeException: HV000186: The constraint of type 'javax.validation.constraints.NotNull' defined on 'getMultiClientAccountBalances.arg1' has multiple matching constraint validators which is due to an additional value handler of type 'org.hibernate.validator.internal.engine.valuehandling.OptionalValueUnwrapper'. It is unclear which value needs validating. Clarify configuration via @UnwrapValidatedValue.

I know it can be fixed by adding @UnwrapValidatedValue to the field, but this must be added to every method what is a lot of work for me. Is there any simpler solution (besides upgrade to HV6.x)

bilak
  • 4,526
  • 3
  • 35
  • 75
  • *Unrelated to the concrete question*, you should not be using Optionals as a method parameters, see https://stackoverflow.com/questions/31922866/why-should-java-8s-optional-not-be-used-in-arguments – jpangamarca Aug 12 '19 at 14:44

1 Answers1

1

Unfortunately I don't see how we could change this behavior in 5.4 without breaking other use cases.

And there is no easy way to disable the optional value handler as it's added unconditionally.

So I would say you have two solutions:

  1. a search and replace of all @Constraint Optional to add the UnwrapValidatedValue option
  2. or move to HV 6, where we totally reworked this feature and where, I think, it should work as you expect it. I know you didn't want this answer but it is what it is...

The issue with 1. is that we removed this annotation from HV (it was experimental) in favor of a standard feature included in Bean Validation so you will have to remove it when moving to 6.

I don't know your exact environment but HV 6 is highly compatible with the previous versions so it might work very well. Just be careful about the dependencies as we changed the groupId of the artifact from org.hibernate to org.hibernate.validator. Also be aware that you need to update the validation-api from 1.1 to 2.0.

6 is already very stable and if you have any issues with it, we will fix them right away.

Guillaume Smet
  • 9,921
  • 22
  • 29
  • sorry I coppied bad method. Yes `@NotNull` is on Optional parameter also. I will edit the question. We are also using Spring. Do you need still some sample application to test it on? – bilak Nov 22 '17 at 15:02
  • I've created simple project. Just run [this test](https://github.com/bilak-poc/hibernate-validator-unwrapvalidatedvalue/blob/master/src/test/java/com/example/demo/DemoServiceTest.java) – bilak Nov 22 '17 at 15:19
  • Updated my answer following your question update. HTH. – Guillaume Smet Nov 22 '17 at 15:33
  • what is difference between [org.hibernate](https://mvnrepository.com/artifact/org.hibernate/hibernate-validator/6.0.5.Final) and [org.hibernate.validator](https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator/6.0.5.Final)? – bilak Nov 23 '17 at 09:32
  • 1
    org.hibernate is just a relocation artifact to ease the upgrade. You should use org.hibernate.validator for HV 6. The idea is to separate each major Hibernate project instead of having them all in the same groupId. – Guillaume Smet Nov 23 '17 at 11:29