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

Catch defensively exception thrown by validation contraints AOT processing of Kotlin extensions #30037

Closed
benweet opened this issue Feb 24, 2023 · 4 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) theme: kotlin An issue related to Kotlin support type: regression A bug that is also a regression
Milestone

Comments

@benweet
Copy link

benweet commented Feb 24, 2023

Maven goal spring-boot-maven-plugin:process-aot does not work anymore on our side. It worked with v3.0.2. With latest v3.0.3 we have the following error :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getTypeParametersCascadingMetaDataForParameterizedType(AnnotationMetaDataProvider.java:679)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getTypeParametersCascadingMetadata(AnnotationMetaDataProvider.java:661)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findCascadingMetaData(AnnotationMetaDataProvider.java:628)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getParameterMetaData(AnnotationMetaDataProvider.java:432)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findExecutableMetaData(AnnotationMetaDataProvider.java:308)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getMetaData(AnnotationMetaDataProvider.java:292)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getMethodMetaData(AnnotationMetaDataProvider.java:279)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.retrieveBeanConfiguration(AnnotationMetaDataProvider.java:131)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getBeanConfiguration(AnnotationMetaDataProvider.java:121)
	at org.hibernate.validator.internal.metadata.BeanMetaDataManagerImpl.getBeanConfigurationForHierarchy(BeanMetaDataManagerImpl.java:234)
	at org.hibernate.validator.internal.metadata.BeanMetaDataManagerImpl.createBeanMetaData(BeanMetaDataManagerImpl.java:201)
	at org.hibernate.validator.internal.metadata.BeanMetaDataManagerImpl.getBeanMetaData(BeanMetaDataManagerImpl.java:165)
	at org.hibernate.validator.internal.engine.ValidatorImpl.getConstraintsForClass(ValidatorImpl.java:316)
	at org.springframework.validation.beanvalidation.BeanValidationBeanRegistrationAotProcessor$BeanValidationDelegate.processAheadOfTime(BeanValidationBeanRegistrationAotProcessor.java:70)
	at org.springframework.validation.beanvalidation.BeanValidationBeanRegistrationAotProcessor.processAheadOfTime(BeanValidationBeanRegistrationAotProcessor.java:59)
	at org.springframework.beans.factory.aot.BeanDefinitionMethodGeneratorFactory.getAotContributions(BeanDefinitionMethodGeneratorFactory.java:151)
	at org.springframework.beans.factory.aot.BeanDefinitionMethodGeneratorFactory.getBeanDefinitionMethodGenerator(BeanDefinitionMethodGeneratorFactory.java:99)
	at org.springframework.beans.factory.aot.BeanDefinitionMethodGeneratorFactory.getBeanDefinitionMethodGenerator(BeanDefinitionMethodGeneratorFactory.java:115)
	at org.springframework.beans.factory.aot.BeanRegistrationsAotProcessor.processAheadOfTime(BeanRegistrationsAotProcessor.java:48)
	at org.springframework.beans.factory.aot.BeanRegistrationsAotProcessor.processAheadOfTime(BeanRegistrationsAotProcessor.java:36)
	at org.springframework.context.aot.BeanFactoryInitializationAotContributions.getContributions(BeanFactoryInitializationAotContributions.java:67)
	at org.springframework.context.aot.BeanFactoryInitializationAotContributions.<init>(BeanFactoryInitializationAotContributions.java:49)
	at org.springframework.context.aot.BeanFactoryInitializationAotContributions.<init>(BeanFactoryInitializationAotContributions.java:44)
	at org.springframework.context.aot.ApplicationContextAotGenerator.lambda$processAheadOfTime$0(ApplicationContextAotGenerator.java:58)
	at org.springframework.context.aot.ApplicationContextAotGenerator.withCglibClassHandler(ApplicationContextAotGenerator.java:67)
	at org.springframework.context.aot.ApplicationContextAotGenerator.processAheadOfTime(ApplicationContextAotGenerator.java:53)
	at org.springframework.context.aot.ContextAotProcessor.performAotProcessing(ContextAotProcessor.java:106)
	at org.springframework.context.aot.ContextAotProcessor.doProcess(ContextAotProcessor.java:84)
	at org.springframework.context.aot.ContextAotProcessor.doProcess(ContextAotProcessor.java:49)
	at org.springframework.context.aot.AbstractAotProcessor.process(AbstractAotProcessor.java:82)
	at org.springframework.boot.SpringApplicationAotProcessor.main(SpringApplicationAotProcessor.java:80)

We get the error by running mvn native:compile -Pnative.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 24, 2023
@scottfrederick
Copy link
Contributor

Thanks for the report. It is difficult to tell what's going on with limited information. If you would like us to spend some time investigating, please provide a complete minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it and attaching it to this issue.

@scottfrederick scottfrederick added the status: waiting-for-feedback We need additional information before we can continue label Feb 24, 2023
@benweet
Copy link
Author

benweet commented Feb 26, 2023

Hi @scottfrederick

You will find a minimal sample here. You will reproduce the problem by running mvn native:compile -P native.

Things I've noticed during my investigation :

  • The project includes the spring-boot-starter-validation dependency and, during the spring-boot-maven-plugin:process-aot goal, Hibernate seems to validate all my beans methods, even the DemoService.parseUserDto() method which is private and is not used during JSR 303 validation. It does work when I remove the spring-boot-starter-validation dependency or when I exclude the jakarta dependency as follows :
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-validation</artifactId>
			<exclusions>
				<exclusion>
					<groupId>jakarta.validation</groupId>
					<artifactId>jakarta.validation-api</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
  • It does work when I remove the param: String parameter of the DemoService.parseUserDto() method.

  • It does work when I use spring-boot-starter-parent:3.0.2 as the project parent pom.

Cheers!

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Feb 26, 2023
@philwebb
Copy link
Member

It looks like the fix #29823 has now triggered another bug which I think could be https://hibernate.atlassian.net/browse/HV-1796.

I can replicate the same issue by adding the following test to your sample project:

class ValidationTests {

    @Test
    fun validate() {
        val validator: Validator = Validation.buildDefaultValidatorFactory().validator
        val clazz: Class<*> = DemoService::class.java
        validator.getConstraintsForClass(clazz)
    }

}

I don't think there is anything that we can do in Spring Boot to fix this, but perhaps Spring Framework can be more defensive when invoking getConstraintsForClass.

@bclozel Could you please transfer this issue?

/cc @sdeleuze

@bclozel bclozel transferred this issue from spring-projects/spring-boot Feb 26, 2023
@sdeleuze sdeleuze self-assigned this Feb 27, 2023
@sdeleuze sdeleuze added type: regression A bug that is also a regression in: core Issues in core modules (aop, beans, core, context, expression) theme: kotlin An issue related to Kotlin support and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Feb 27, 2023
@sdeleuze sdeleuze added this to the 6.0.6 milestone Feb 27, 2023
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Feb 27, 2023
An ArrayIndexOutOfBoundsException is thrown by
Validator.getConstraintsForClass when processing Kotlin beans
with extensions functions (Kotlin or Hibernate Validator bug).

This commit catches those exceptions and report them as warning
without the full stactrace, and report as well other
ones thrown as errors with the full stracktrace.

Closes spring-projectsgh-30037
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Feb 27, 2023
An ArrayIndexOutOfBoundsException is thrown by
Validator.getConstraintsForClass when processing Kotlin beans
with extensions functions (Kotlin or Hibernate Validator bug).

This commit catches those exceptions and report them as warning
without the full stactrace, and report as well other
ones thrown as errors with the full stracktrace.

Closes spring-projectsgh-30037
@sdeleuze
Copy link
Contributor

sdeleuze commented Feb 27, 2023

Not strictly a regression since this is due to a bug on Kotlin or Hibernate side, but indeed better to catch those exceptions defensively. I have bring the root issue to the attention of the Kotlin team to see if they can move forward in fixing KT-40857 mentioned as the root issue in HV-1796.

@sdeleuze sdeleuze added type: enhancement A general enhancement and removed type: enhancement A general enhancement labels Feb 27, 2023
@sdeleuze sdeleuze changed the title Maven goal spring-boot-maven-plugin:process-aot not working with latest 3.0.3 Catch defensively exception thrown by validation contraints AOT processing of Kotlin extensions Feb 27, 2023
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
An ArrayIndexOutOfBoundsException is thrown by
Validator.getConstraintsForClass when processing Kotlin beans
with extensions functions (Kotlin or Hibernate Validator bug).

This commit catches those exceptions and report them as warning
without the full stactrace, and report as well other
ones thrown as errors with the full stracktrace.

Closes spring-projectsgh-30037
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) theme: kotlin An issue related to Kotlin support type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

5 participants