Skip to content

Avoid infinite recursion in KotlinDetector.hasSerializableAnnotation - #37128

Open
aleksei-averchenko-wise wants to merge 1 commit into
spring-projects:mainfrom
aleksei-averchenko-wise:codex/fix-kotlin-detector-generic-cycle
Open

Avoid infinite recursion in KotlinDetector.hasSerializableAnnotation#37128
aleksei-averchenko-wise wants to merge 1 commit into
spring-projects:mainfrom
aleksei-averchenko-wise:codex/fix-kotlin-detector-generic-cycle

Conversation

@aleksei-averchenko-wise

@aleksei-averchenko-wise aleksei-averchenko-wise commented Aug 11, 2026

Copy link
Copy Markdown

Overview

KotlinDetector.hasSerializableAnnotation recursively inspects generic types but did not account for self-referential generic bounds. Types such as Guava's ImmutableEnumSet, whose type parameter ultimately refers back to itself, therefore caused a StackOverflowError instead of returning whether Kotlin's @Serializable annotation is present.

This change tracks the underlying Java types by identity along the active traversal path. Repeated types short-circuit the current branch without invoking potentially recursive ResolvableType.equals() or hashCode() implementations, while removing types as the traversal unwinds preserves detection in separate and nested generic branches.

AI use disclosure: Assisted by Codex (5.6 Sol Extra High), edited and verified by hand.

Closes #37127.

Changes

  • Add identity-based cycle detection to KotlinDetector.hasSerializableAnnotation.
  • Add a focused regression test using a minimal self-referential generic type without an external test dependency.

Testing

  • ./gradlew spring-core:test --tests org.springframework.core.KotlinDetectorTests
  • ./gradlew spring-core:checkstyleMain spring-core:checkstyleTest

Track generic types by identity while traversing nested generics to
short-circuit self-referential bounds without relying on recursive
ResolvableType equality or hash codes.

Closes spring-projectsgh-37127

Signed-off-by: Aleksei Averchenko <aleksei.averchenko@wise.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 11, 2026
@aleksei-averchenko-wise
aleksei-averchenko-wise marked this pull request as ready for review August 11, 2026 12:19
@aleksei-averchenko-wise aleksei-averchenko-wise changed the title Avoid recursion in Kotlin serializable detection Avoid infinite recursion in Kotlin serializable detection Aug 11, 2026
@aleksei-averchenko-wise aleksei-averchenko-wise changed the title Avoid infinite recursion in Kotlin serializable detection Avoid infinite recursion in KotlinDetector.hasSerializableAnnotation Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Infinite recursion in KotlinDetector.hasSerializableAnnotation

2 participants