-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Injecting @ConfigMapping into ConstraintValidator results in an exception #43450
Comments
/cc @radcortez (config) |
You need to annotate the mapping with |
Ohh, sorry, @radcortez, I missed your comment and just noticed that you've closed the ticket. Yeah, indeed, constraint validators are currently initialized during the static init. I opened this ticket since we are currently working towards a Hibernate Validator 9 release, and we have a chance to make some changes there (if we need to) to address some of the Quarkus use cases.
Not sure what would be the best solution, but here are a few suggestions we can consider:
|
There is already a way to use runtime configuration, by injecting a @ApplicationScoped
public class MyConstraintConstraintValidator implements ConstraintValidator<MyConstraint, String> {
@Inject
Instance<ValidationConfigProperties> config;
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}
return config.get().pattern().matcher(value).matches();
}
} I think we shouldn't add specific APIs for this case, considering that other components have the same semantics (REST Providers for instance). This is not only for configuration but for any injectable bean. |
Thanks @radcortez! I think that's something we discussed with Marko last time we talked about this. I wonder if you tried @marko-bekhta? If it works in the case of Validator, it looks like this would be the solution @marko-bekhta ?
|
By the way @radcortez , that's out of scope for this issue, but maybe we should create another one about clarifying the error message...
Shouldn't that mention something like "you're trying to use runtime config at static init" or even simply "no values found for property x or y"... ? So that the user understand what they are doing wrong. Unless that information is in a cause you removed from the stack trace @marko-bekhta? |
It is just a generic error message. The mapping is not available in the config, so it cannot be found. We can certainly provide a more helpful message. We did something similar for values here: #36281 It should be possible to validate if the mapping injection can be done safely, and if not, warn the user. |
Describe the bug
Assume having some app properties as:
and then trying to use these properties in some custom constraint validator implementation as:
Expected behavior
ConstraintValidatorFactory
correctly instantiates theMyConstraintConstraintValidator
instance.Actual behavior
An exception is thrown:
How to Reproduce?
I've created a simple test in this commit marko-bekhta@f0fffb2
Output of
uname -a
orver
Linux fedora 6.10.9-100.fc39.x86_64 #1 SMP PREEMPT_DYNAMIC Mon Sep 9 02:28:01 UTC 2024 x86_64 GNU/Linux
Output of
java -version
Java version: 21.0.4, vendor: Eclipse Adoptium
Quarkus version or git rev
3.14.3
Build tool (ie. output of
mvnw --version
orgradlew --version
)Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)
Additional information
Injecting the same config properties elsewhere works fine.
The text was updated successfully, but these errors were encountered: