Open marko-bekhta opened 1 month ago
/cc @radcortez (config)
You need to annotate the mapping with @StaticInitSafe
because ConstraintValidator
s are initialized during STATIC_INIT:
https://quarkus.io/guides/config-mappings#static-init
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.
@StaticInitSafe
doesn't fit the case when the user has to pass the application configuration at runtime, let's say, depending on where it is deployed.
Not sure what would be the best solution, but here are a few suggestions we can consider:
runtimeInitialize(...)
that can be triggered by Quarkus.There is already a way to use runtime configuration, by injecting a Instance
, which is initialized at runtime:
@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 ?
come up with a workaround that would allow the user to use the runtime property and add an example of that to the docs ...
By the way @radcortez , that's out of scope for this issue, but maybe we should create another one about clarifying the error message...
Caused by: java.util.NoSuchElementException: SRCFG00027: Could not find a mapping for io.quarkus.it.hibernate.validator.injection.ValidationConfigProperties
at io.smallrye.config.SmallRyeConfig.getConfigMapping(SmallRyeConfig.java:592)
at io.quarkus.arc.runtime.ConfigMappingCreator.create(ConfigMappingCreator.java:30)
at io.quarkus.it.hibernate.validator.injection.ValidationConfigProperties_oli60TV5cswBFUOsKjucMk-Wsw8_Synthetic_Bean.createSynthetic(Unknown Source)
... 48 more
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: https://github.com/quarkusio/quarkus/pull/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 https://github.com/marko-bekhta/quarkus/commit/f0fffb29172bf0d46d13765e047df1c430de6cb7
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.