-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Bean validation fails with ArrayIndexOutOfBoundsException
for Kotlin suspending function
#29793
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
Comments
i have dig into the exception, and found out the problem class KtReflectionParameterNameDiscoverer: KotlinReflectionParameterNameDiscoverer() {
override fun getParameterNames(method: Method): Array<String>? {
return super.getParameterNames(method)?.run {
if (KotlinDetector.isSuspendingFunction(method)) this.plus("continuation") else this
}
}
} i have found that configuration at ValidationAutoConfiguration @Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@ConditionalOnMissingBean(Validator.class)
public static LocalValidatorFactoryBean defaultValidator(ApplicationContext applicationContext,
ObjectProvider<ValidationConfigurationCustomizer> customizers) {
LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
factoryBean.setConfigurationInitializer((configuration) -> customizers.orderedStream()
.forEach((customizer) -> customizer.customize(configuration)));
MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory(applicationContext);
factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
return factoryBean;
} i'm try to custom a ValidationConfigurationCustomizer to special the KtReflectionParameterNameDiscoverer, but i found i can't special the parameterNameDiscoverer property of LocalValidatorFactoryBean, i have to configuration the LocalValidatorFactoryBean instance @Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
fun defaultValidator(applicationContext: ApplicationContext): LocalValidatorFactoryBean {
val factoryBean = LocalValidatorFactoryBean()
factoryBean.setParameterNameDiscoverer(KtReflectionParameterNameDiscoverer())
val interpolatorFactory = MessageInterpolatorFactory(applicationContext)
factoryBean.messageInterpolator = interpolatorFactory.getObject()
return factoryBean
} when i done that things, it works. |
@sbrannen could you fix that problem? |
the KotlinReflectionParameterNameDiscoverer class may used by other place, you can provide a new one, and override the getParameterNames function and replace it in LocalValidatorFactoryBean public LocalValidatorFactoryBean() {
if (KotlinDetector.isKotlinReflectPresent()) {
this.parameterNameDiscoverer = new KotlinReflectionParameterNameDiscoverer();
}
} |
ArrayIndexOutOfBoundsException
for Kotlin suspending function
Duplicate of #23499. |
@sdeleuze the issure exist for many years, and dosn't solved |
@cjdxhjj it's not solved yet. This issue is just a duplicate so it's been closed in favor of the other one. |
you may add one switch at KotlinReflectionParameterNameDiscoverer to perform one result for hibernate validate and one for others |
The exception only occurs in KotlinCoroutines which will append an extra If you are using pure Kotlin, I do not think there is a problem. So here, you have to make sure it is a Kotlin coroutine case. |
@hantsy yes, the simple way is to extend that class or add one switch for other case |
Uh oh!
There was an error while loading. Please reload this page.
Affects: Spring Framework 6.0.3
I put the
@Validated
on the controller, when i call any action on that controller, it fail with flowing exceptionThe text was updated successfully, but these errors were encountered: