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

Remove Introspected from MyValidatorBean example #336

Merged
merged 5 commits into from
May 30, 2024

Conversation

jeremyg484
Copy link
Contributor

@jeremyg484 jeremyg484 commented Mar 27, 2024

The docs are updated to no longer show the use of the @Introspected annotation on the MyValidatorBean example, as it cannot successfully be used on a validator that is a managed bean with dependencies.

DefaultInternalConstraintValidatorFactory is updated to include the exception cause in the stack trace when a validator cannot be initialized.

The CustomMessageConstraint2Validator test class for CustomConstraintsSpec is updated to include a dependency that must successfully be injected in order to verify that this works as long as the validator is not also Introspected.

Resolves #258

Note For Reviewers:

An alternative approach that I considered is to change this lookup algorithm in DefaultInternalConstraintValidatorFactory to instead look for a bean first before looking for the introspection. Something such as:

entry = Optional.ofNullable(instantiateConstraintValidatorEntryOfBeanRegistration(type))
                .or(() -> beanIntrospector.findIntrospection(type)
                .map(this::instantiateConstraintValidatorEntry)).orElseThrow();

This allows the existing example with both @Singleton and @Introspected to still work when an injected dependency is required in the constructor...but I am of the opinion that it is incorrect in the first place to have @Introspected on a managed bean. The upside to doing that would be that users' 3.x apps that followed the example won't break when upgrading to 4.x, but this seems acceptable as a breaking change in the major version.

The docs are updated to no longer show the use of the Introspected
annotation on the MyValidatorBean example, as it cannot successfully
be used on a validator that is a managed bean with dependencies.

DefaultInternalConstraintValidatorFactory is updated to include the
exception cause in the stack trace when a validator cannot be
initialized.

The CustomMessageConstraint2Validator test class for
CustomConstraintsSpec is updated to include a dependency that must
successfully be injected in order to verify that this works as long as
the validator is not also Introspected.
jeremyg484 and others added 2 commits March 27, 2024 15:33
Co-authored-by: Tim Yates <tim.yates@gmail.com>
Copy link

sonarcloud bot commented Mar 28, 2024

@sdelamo sdelamo self-requested a review April 16, 2024 15:02
@graemerocher graemerocher merged commit 169e0da into 5.0.x May 30, 2024
11 checks passed
@graemerocher graemerocher deleted the constraint-validator-bean-with-dependency branch May 30, 2024 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Micronaut 4 Validation - Cannot use dependency injection in custom validator classes
3 participants