From ee938bbaca4449d2f4512c75ccbb0c95a025044a Mon Sep 17 00:00:00 2001 From: "leonid.stashevsky" Date: Thu, 9 Apr 2026 14:32:45 +0200 Subject: [PATCH] fix: warn when Kotlin class loses field values due to missing kotlin-reflect When a Kotlin data class has constructor parameters but kotlin-reflect is not on the classpath, parameter names cannot be resolved. This causes deserialization to silently produce objects with default/null field values. Add a diagnostic warning to stderr when this condition is detected, so users get a clear pointer to the root cause instead of debugging silent data loss. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../main/java/com/alibaba/fastjson2/util/KotlinUtils.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/src/main/java/com/alibaba/fastjson2/util/KotlinUtils.java b/core/src/main/java/com/alibaba/fastjson2/util/KotlinUtils.java index 755973137..54ac6976d 100644 --- a/core/src/main/java/com/alibaba/fastjson2/util/KotlinUtils.java +++ b/core/src/main/java/com/alibaba/fastjson2/util/KotlinUtils.java @@ -102,6 +102,13 @@ public class KotlinUtils { } catch (Throwable e) { // Ignore this exception } + } else if (creatorParams != 0 && beanInfo.createParameterNames == null) { + System.err.println("fastjson2 warning: class " + clazz.getName() + + " is a Kotlin class with constructor parameters, " + + "but kotlin-reflect is not on the classpath. " + + "Constructor parameter names cannot be resolved, " + + "which will cause deserialization to silently lose field values. " + + "Add org.jetbrains.kotlin:kotlin-reflect to your dependencies."); } beanInfo.creatorConstructor = creatorConstructor; -- Gitee