Skip to content

Commit

Permalink
fix kotlin support, for issue #3288
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Feb 9, 2025
1 parent 5b34823 commit 3cb9068
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,10 @@ public static String getterName(Method method, boolean kotlin, String namingStra
}

String fieldName = getterName(methodName, namingStrategy);
int subIndex;
if (kotlin && (subIndex = fieldName.indexOf('-')) != -1) {
fieldName = fieldName.substring(0, subIndex);
}

if (fieldName.length() > 2
&& fieldName.charAt(0) >= 'A' && fieldName.charAt(0) <= 'Z'
Expand Down
27 changes: 27 additions & 0 deletions kotlin/src/test/kotlin/com/alibaba/fastjson2/issues/Issue3288.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.alibaba.fastjson2.issues

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

class Issue3288 {
@JvmInline
value class TestBean(val a: Int)

data class Test2(
val b: TestBean
)

@Test
fun test() {
val s1 = "{\"a\":1}"
assertEquals(s1, TestBean(1).toJSONString())
assertEquals(s1, JSON.parseObject(s1, TestBean::class.java).toJSONString())
assertEquals(s1, TestBean(1).toJSONString())
val s2 = "{\"b\":2}"
assertEquals(s2, JSON.parseObject(s2, Test2::class.java)
.toJSONString())
assertEquals(s2, JSON.parseObject(s2, Test2::class.java).toJSONString())
}
}

0 comments on commit 3cb9068

Please sign in to comment.