Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnexpectedTypeException: Cannot find a constraint validator for constraint #312

Closed
mancze opened this issue Feb 26, 2024 · 3 comments
Closed

Comments

@mancze
Copy link

mancze commented Feb 26, 2024

Expected Behavior

Validator should not throw the exception and behave uniformly on subsequent calls.

Actual Behaviour

Exception is thrown when validator.validate(validBean) is being validated:

jakarta.validation.UnexpectedTypeException: Cannot find a constraint validator for constraint:

Steps To Reproduce

Included in the example application including naive workaround. Run tests to identify bug.

  1. Use Micronaut validation + Jakarta validation annotations
  2. Declare a bean with validated properties
  3. Inject Validator bean into your tests
  4. Call validator.validate() with your valid bean
  5. Exception is thrown
  6. Catch it and re-run validator.validate()
  7. No exception is thrown

Steps 5 and 7 produce inconsistent validation behaviour.

I think that possible cause is this line:

A ConstraintValidator.VALID is put to the validator's cache but Optional.empty() is returned. Subsequent calls of the method return the cached ConstraintValidator.VALID which is the reason of the inconsistent behaviour.

Environment Information

No response

Example Application

https://github.com/mancze/micronaut-sandbox/tree/repro/constraint-validation-not-found

Version

4.3.2

@dstepanov
Copy link
Contributor

The error is correct. You don't have a validator defined. Remove @Constraint if you want just to apply other constraints.

@dstepanov dstepanov closed this as not planned Won't fix, can't repro, duplicate, stale Feb 28, 2024
@mancze
Copy link
Author

mancze commented Mar 18, 2024

Thanks. Removal of @Constraint(validatedBy = { }) seems to have helped.

We were using it because it was documented so in docs for older version and it seems it changed in v4.1.0:
3cee60e#diff-70e97b012bcbcfdcc57ed2c42af87338142c473a8200234add37f3920f2557ca

I didn't notice this change during migration from Microanut 3 to a recent version (4.3.2) of Micronaut 4. Does it mean that @Constraint shouldn't be applied if validator of the bean comes from the factory method?

@mancze
Copy link
Author

mancze commented Mar 21, 2024

Also, I observe some inconsistency related to the validation cache. With @Constraint(validatedBy = { }) annotation preserved and the test case being modified to:

try {
	assertThat(validator.validate(bean)).isEmpty();
} catch (Exception e) {
	// retry
	assertThat(validator.validate(bean)).isEmpty();
}

(mancze/micronaut-sandbox@e311721)

then only the first validation fails and the second one in the catch block not. The behaviour is not consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants