Skip to content

Commit

Permalink
fix deserialize kotlin data class error, for issue #2231
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Feb 8, 2024
1 parent 2838c3a commit f71cd4b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1647,7 +1647,7 @@ boolean record = BeanUtils.isRecord(objectClass);
}

Class mixIn = provider.getMixIn(objectClass);
BeanUtils.setters(objectClass, mixIn, method -> {
BeanUtils.setters(objectClass, beanInfo, mixIn, method -> {
fieldInfo.init();
fieldInfo.features |= beanFeatures;
fieldInfo.format = beanFormat;
Expand Down
11 changes: 10 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,14 @@ public static Constructor getDefaultConstructor(Class objectClass, boolean inclu
}

public static void setters(Class objectClass, Consumer<Method> methodConsumer) {
setters(objectClass, null, methodConsumer);
setters(objectClass, null, null, methodConsumer);
}

public static void setters(Class objectClass, Class mixin, Consumer<Method> methodConsumer) {
setters(objectClass, null, mixin, methodConsumer);
}

public static void setters(Class objectClass, BeanInfo beanInfo, Class mixin, Consumer<Method> methodConsumer) {
if (ignore(objectClass)) {
return;
}
Expand Down Expand Up @@ -453,6 +457,11 @@ public static void setters(Class objectClass, Class mixin, Consumer<Method> meth
case "toString":
methodSkip = true;
break;
case "copy":
if (beanInfo != null && beanInfo.kotlin) {
methodSkip = true;
}
break;
default:
break;
}
Expand Down
33 changes: 33 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues_2200/Issue2231.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.alibaba.fastjson2.issues_2200

import com.alibaba.fastjson2.JSON
import com.alibaba.fastjson2.JSONB
import com.alibaba.fastjson2.JSONReader
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class Issue2231 {
@Test
fun test() {
var data = JSON.parseObject("""{}""", Hello::class.java)
assertEquals("hello", data.value)

var jsonb = JSONB.toBytes(data)

assertEquals(
"""{
"value":"hello"
}""",
JSONB.toJSONString(jsonb)
)
val data1 = JSONB.parseObject(jsonb, Hello::class.java)
assertEquals(data.value, data1.value)

val data2 = JSONB.parseObject(jsonb, Hello::class.java, JSONReader.Feature.FieldBased)
assertEquals(data.value, data2.value)
}
}

data class Hello(
val value: String = "hello"
)

0 comments on commit f71cd4b

Please sign in to comment.