From 8b6d7e0da7f65f0e92e96bb9ec2316fa3852e1a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Thu, 4 Dec 2025 15:50:19 +0100 Subject: [PATCH] Refine KotlinDetector#hasSerializableAnnotation Refine KotlinDetector#hasSerializableAnnotation in order to detect deeply nested generic types. Closes gh-35960 --- .../main/java/org/springframework/core/KotlinDetector.java | 5 ++--- .../kotlin/org/springframework/core/KotlinDetectorTests.kt | 7 +++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/KotlinDetector.java b/spring-core/src/main/java/org/springframework/core/KotlinDetector.java index 269bb88c256..c7852e8e83b 100644 --- a/spring-core/src/main/java/org/springframework/core/KotlinDetector.java +++ b/spring-core/src/main/java/org/springframework/core/KotlinDetector.java @@ -148,9 +148,8 @@ public abstract class KotlinDetector { if (resolvedClass.isAnnotationPresent(KOTLIN_SERIALIZABLE)) { return true; } - @Nullable Class[] resolvedGenerics = type.resolveGenerics(); - for (Class resolvedGeneric : resolvedGenerics) { - if (resolvedGeneric != null && resolvedGeneric.isAnnotationPresent(KOTLIN_SERIALIZABLE)) { + for (ResolvableType genericType : type.getGenerics()) { + if (hasSerializableAnnotation(genericType)) { return true; } } diff --git a/spring-core/src/test/kotlin/org/springframework/core/KotlinDetectorTests.kt b/spring-core/src/test/kotlin/org/springframework/core/KotlinDetectorTests.kt index c7fdb28b7de..a9048b7c5b8 100644 --- a/spring-core/src/test/kotlin/org/springframework/core/KotlinDetectorTests.kt +++ b/spring-core/src/test/kotlin/org/springframework/core/KotlinDetectorTests.kt @@ -57,6 +57,13 @@ class KotlinDetectorTests { Assertions.assertThat(KotlinDetector.hasSerializableAnnotation(ResolvableType.forClassWithGenerics(Map::class.java, String::class.java, WithoutSerializable::class.java))).isFalse() Assertions.assertThat(KotlinDetector.hasSerializableAnnotation(ResolvableType.forClassWithGenerics(Map::class.java, String::class.java, WithSerializable::class.java))).isTrue() + Assertions.assertThat(KotlinDetector.hasSerializableAnnotation( + ResolvableType.forClassWithGenerics(Map::class.java, + ResolvableType.forClass(String::class.java), + ResolvableType.forClassWithGenerics(List::class.java, WithSerializable::class.java)))).isTrue() + Assertions.assertThat(KotlinDetector.hasSerializableAnnotation( + ResolvableType.forClassWithGenerics(List::class.java, + ResolvableType.forClassWithGenerics(List::class.java, WithSerializable::class.java)))).isTrue() Assertions.assertThat(KotlinDetector.hasSerializableAnnotation(ResolvableType.NONE)).isFalse() }