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

@AssertTrue is not working on ConfigMapping when it returns true #46471

Closed
gbourant opened this issue Feb 25, 2025 · 5 comments · Fixed by #46489
Closed

@AssertTrue is not working on ConfigMapping when it returns true #46471

gbourant opened this issue Feb 25, 2025 · 5 comments · Fixed by #46489
Milestone

Comments

@gbourant
Copy link

I have the following class

@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
@ConfigMapping(prefix = "project")
public interface ProjectConfig {
     @AssertTrue(message = "ProjectConfig values are invalid")
    default boolean isValid() {
        return true;
    }
}

When isValid method returns true, I get the following exception:

ERROR [io.qua.run.Application] (Quarkus Main Thread) Failed to start application: java.lang.RuntimeException: Failed to start quarkus
	at io.quarkus.runner.ApplicationImpl.doStart(Unknown Source)
	at io.quarkus.runtime.Application.start(Application.java:101)
	at io.quarkus.runtime.ApplicationLifecycleManager.run(ApplicationLifecycleManager.java:121)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:77)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:48)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:137)
	at io.quarkus.runner.GeneratedMain.main(Unknown Source)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at io.quarkus.runner.bootstrap.StartupActionImpl$1.run(StartupActionImpl.java:116)
	at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: jakarta.validation.ValidationException: HV000041: Call to TraversableResolver.isReachable() threw an exception.
	at org.hibernate.validator.internal.engine.ValidatorImpl.isReachable(ValidatorImpl.java:1316)
	at org.hibernate.validator.internal.engine.ValidatorImpl.isValidationRequired(ValidatorImpl.java:1291)
	at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:549)
	at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:518)
	at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:488)
	at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:450)
	at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:400)
	at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:172)
	at io.smallrye.config.validator.BeanValidationConfigValidator.validateMappingClass(BeanValidationConfigValidator.java:239)
	at io.smallrye.config.validator.BeanValidationConfigValidator.validateMappingInterface(BeanValidationConfigValidator.java:61)
	at io.smallrye.config.validator.BeanValidationConfigValidator.validateMapping(BeanValidationConfigValidator.java:40)
	at io.smallrye.config.ConfigMappingContext.constructMapping(ConfigMappingContext.java:76)
	at io.smallrye.config.ConfigMappingContext.<init>(ConfigMappingContext.java:58)
	at io.smallrye.config.SmallRyeConfig$1.get(SmallRyeConfig.java:129)
	at io.smallrye.config.SmallRyeConfig$1.get(SmallRyeConfig.java:126)
	at io.smallrye.config.SecretKeys.doUnlocked(SecretKeys.java:28)
	at io.smallrye.config.SmallRyeConfig.buildMappings(SmallRyeConfig.java:126)
	at io.smallrye.config.SmallRyeConfig.<init>(SmallRyeConfig.java:93)
	at io.smallrye.config.SmallRyeConfigBuilder.build(SmallRyeConfigBuilder.java:773)
	at io.quarkus.runtime.generated.Config.readConfig(Unknown Source)
	at io.quarkus.runtime.generated.Config.createRunTimeConfig(Unknown Source)
	at io.quarkus.deployment.steps.RuntimeConfigSetup.deploy(Unknown Source)
	... 11 more
Caused by: java.lang.IllegalStateException: Persistence providers are not available during the static init phase.
	at io.quarkus.hibernate.orm.runtime.StaticInitHibernatePersistenceProviderResolver.getPersistenceProviders(StaticInitHibernatePersistenceProviderResolver.java:15)
	at jakarta.persistence.Persistence$PersistenceUtilImpl.isLoaded(Persistence.java:142)
	at org.hibernate.validator.internal.engine.resolver.JPATraversableResolver.isReachable(JPATraversableResolver.java:52)
	at org.hibernate.validator.internal.engine.resolver.CachingJPATraversableResolverForSingleValidation.lambda$isReachable$0(CachingJPATraversableResolverForSingleValidation.java:43)
	at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1228)
	at org.hibernate.validator.internal.engine.resolver.CachingJPATraversableResolverForSingleValidation.isReachable(CachingJPATraversableResolverForSingleValidation.java:43)
	at org.hibernate.validator.internal.engine.ValidatorImpl.isReachable(ValidatorImpl.java:1307)
	... 32 more

When isValid method returns false, I get the following exception:

ERROR [io.qua.run.Quarkus] (Quarkus Main Thread) Error running Quarkus: java.lang.ExceptionInInitializerError
	at io.quarkus.runner.ApplicationImpl.<clinit>(Unknown Source)
	at java.base/jdk.internal.misc.Unsafe.allocateInstance(Native Method)
	at java.base/java.lang.invoke.DirectMethodHandle.allocateInstance(DirectMethodHandle.java:501)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:76)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:48)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:137)
	at io.quarkus.runner.GeneratedMain.main(Unknown Source)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at io.quarkus.runner.bootstrap.StartupActionImpl$1.run(StartupActionImpl.java:116)
	at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: io.smallrye.config.ConfigValidationException: Configuration validation failed:
	project valid ProjectConfig.onboarding config values are invalid
	at io.smallrye.config.SmallRyeConfig.buildMappings(SmallRyeConfig.java:139)
	at io.smallrye.config.SmallRyeConfig.<init>(SmallRyeConfig.java:93)
	at io.smallrye.config.SmallRyeConfigBuilder.build(SmallRyeConfigBuilder.java:773)
	at io.quarkus.runtime.generated.Config.<clinit>(Unknown Source)
	... 11 more

What I want to achieve is cross validation. If there is a better way to do it, please let me know. I want to validate the config before the application starts (or at least before running any user code, like DB migrations via Flyway, etc)

Copy link

quarkus-bot bot commented Feb 25, 2025

/cc @radcortez (config)

@geoand
Copy link
Contributor

geoand commented Feb 25, 2025

Are you building a Quarkus extension?

If not, @ConfigRoot should not be used.

@gbourant
Copy link
Author

Well spotted @geoand. I build/use an extension but the ProjectConfig exists outside of the extension. I want the extension to have access to that config. I had asked about that https://quarkusio.zulipchat.com/#narrow/channel/187030-users/topic/.E2.9C.94.20Configuration.20.40WithDefault.20value.20is.20not.20present.20in.20exte.2E.2E.2E in the past and solved it by adding the @ConfigRoot and the relative META-INF/quarkus-config-roots.list file.

@geoand
Copy link
Contributor

geoand commented Feb 25, 2025

Thanks.

Also cc @gsmet as this includes HV handling in Quarkus

@gsmet
Copy link
Member

gsmet commented Feb 25, 2025

My wild guess is that #46489 should fix the issue.

@gsmet gsmet closed this as completed in a1021fd Feb 25, 2025
@quarkus-bot quarkus-bot bot added this to the 3.21 - main milestone Feb 25, 2025
@gsmet gsmet modified the milestones: 3.21 - main, 3.19.1 Feb 25, 2025
gsmet added a commit to gsmet/quarkus that referenced this issue Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants