Thanks in advance for reading this issue. After thorough search in StackOverflow.com, I found nothing about this in there(for every combinations of keywords: Spring, Kotlin extension, @Validated ArrayIndexOutOfBoundsException). There is a workaround hopefully, but it would be great if this issue is solved as well!

While I was writing a Kotlin Mixin, I bumped into an ArrayIndexOutOfBoundsException caused by CGLIB proxied class. Spent around 30 minutes and found that this is because of a peculiar combination such as:

  1. declare an Kotlin extension function that receiver type is using Java/Kotlin generics(MultiValueMap is in this case)
interface FetchCriteriaRequestMixin {
     fun <SORT> MultiValueMap<String, String>.toFetchCriteria(
         sortByProvider: (sortBy: String?) -> SORT,
         defaultSortDirection: SortDirection,
         defaultPage: Int,
         defaultFetchSizeRange: IntRange
     ): ContentsFetchCriteria<SORT>
}
  1. let this method is exposed to org.springframework.validation.annotation.Validated annotated class which is to be a proxy target
@Validated
interface GetRepliesController

@RestController
internal class GetRepliesControllerImpl : GetRepliesController, FetchCriteriaRequestMixin 
  1. declare a spring-data-jpa dependency with Hibernate

Yet I couldn't found any solution for this, but there is a workaround which are 1. eliminate an extension function and make it as like plain Java utility method, 2. eliminate generics in receiver type of extension function.

I'm leaving my Github source code link for better bug reproduction.

Mixin type - an original cause of this issue @Validated @RestController that utilises this mixin

Tested under: * org.springframework.boot:spring-boot-starter:2.3.1.RELEASE (depends on Spring 5.2.7.RELEASE) * javax.validation:validation-api:2.0.1.Final * jakarta.servlet:jakarta.servlet-api:4.0.3 * org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72 * org.jetbrains.kotlin:kotlin-allopen:1.3.72

Comment From: BarracudaX

I had similar problem and exception when I tried to use spring data jpa repositories with validation on, where spring data jpa repositories called persistent listener that checked every method for validation metadata and threw the ArrayIndexOutOfBoundException when tried to find validation metadata for extensions on entity class that used List as a receiver.

Comment From: Areks

This problem also appear when we try to use coroutines in kotlin

// any rest method in controller
fun getById(val id: String): AnyObject {
   return runBlocking {
     someCode
   }
}

Also main question why we got broken bean in context and haven't finished application initialisation with fail? After spring call ObjenesisCglibAopProxy.createProxyClassAndInstance to apply validation we receive broken bean in line ((Factory) proxyInstance).setCallbacks(callbacks); I think spring need to check such problem and fail context initialisation

Comment From: sdeleuze

For the issue on extensions, it is likely due to https://hibernate.atlassian.net/browse/HV-1796 and/or https://youtrack.jetbrains.com/issue/KT-40857, so it should be fixed on Hibernate/Kotlin side. ArrayIndexOutOfBoundsException on validated suspending functions should have been fixed by #23499.