Skip to content

Kotlin extension function on Generics type + @Validated proxy class causes an ArrayIndexOutOfBoundsException #25593

@FrancescoJo

Description

@FrancescoJo

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<String, String> 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

Metadata

Metadata

Assignees

Labels

for: external-projectNeeds a fix in external projectin: coreIssues in core modules (aop, beans, core, context, expression)in: webIssues in web modules (web, webmvc, webflux, websocket)theme: kotlinAn issue related to Kotlin support

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions