Skip to content

Commit

Permalink
Merge pull request #432 from Kotlin/issue-431-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wuseal authored Jul 19, 2024
2 parents 65c7a4c + f3dee71 commit abd3438
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/main/kotlin/wu/seal/jsontokotlin/utils/TypeHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ fun isExpectedJsonObjArrayType(jsonElementArray: JsonArray): Boolean {
return jsonElementArray.firstOrNull()?.isJsonObject ?: false
}

internal val random = Random(0)

fun resetJsonToKotlinRandom() {
random.setSeed(0)
}

/**
* when get the child type in an array
* ,we need to make the property name be legal and then modify the property name to make it's type name looks like a child type.
Expand All @@ -128,14 +134,16 @@ fun adjustPropertyNameForGettingArrayChildType(property: String): String {
val firstLatterAfterListIndex = innerProperty.lastIndexOf("List") + 4
if (innerProperty.length > firstLatterAfterListIndex && innerProperty[firstLatterAfterListIndex] in 'A'..'Z') {
innerProperty = innerProperty.replaceFirst("List", "", false)
} else if (innerProperty == "List") {
innerProperty = "Item${random.nextInt(10)}"
} else if (innerProperty.length == firstLatterAfterListIndex) {
innerProperty = innerProperty.substring(0, innerProperty.lastIndexOf("List"))
}
}

innerProperty.contains("list") -> {
if (innerProperty == "list") {
innerProperty = "Item${Date().time.toString().last()}"
innerProperty = "Item${random.nextInt()}"
} else if (innerProperty.indexOf("list") == 0 && innerProperty.length >= 5) {
val end = innerProperty.substring(5)
val pre = (innerProperty[4] + "").toLowerCase()
Expand Down
32 changes: 32 additions & 0 deletions src/test/kotlin/wu/seal/jsontokotlin/regression/Issue431Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package wu.seal.jsontokotlin.regression

import com.winterbe.expekt.should
import org.junit.Test
import wu.seal.jsontokotlin.generateKotlinClassCode
import wu.seal.jsontokotlin.model.DefaultValueStrategy
import wu.seal.jsontokotlin.model.TargetJsonConverter
import wu.seal.jsontokotlin.test.TestConfig
import wu.seal.jsontokotlin.utils.BaseTest
import wu.seal.jsontokotlin.utils.resetJsonToKotlinRandom

class Issue431Test : BaseTest() {
@Test
fun testIssue431() {
TestConfig.apply {
isCommentOff = true
targetJsonConvertLib = TargetJsonConverter.None
defaultValueStrategy = DefaultValueStrategy.None
isNestedClassModel = false
}
val json = """{"list":[{}]}"""
val expected = """
data class Test(
val list: List<Item0>
)
class Item0
""".trimIndent()
resetJsonToKotlinRandom()
json.generateKotlinClassCode("Test").should.equal(expected)
}
}
19 changes: 12 additions & 7 deletions src/test/kotlin/wu/seal/jsontokotlin/utils/TypeHelperKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,18 @@ class TypeHelperKtTest {

@Test
fun adjustPropertyNameForGettingArrayChildTypeTest() {
adjustPropertyNameForGettingArrayChildType("").should.be.equal("X")
adjustPropertyNameForGettingArrayChildType("List").should.be.equal("")
adjustPropertyNameForGettingArrayChildType("arrayList").should.be.equal("Array")
adjustPropertyNameForGettingArrayChildType("Apples").should.be.equal("Apple")
adjustPropertyNameForGettingArrayChildType("Activities").should.be.equal("Activity")
adjustPropertyNameForGettingArrayChildType("Book_List").should.be.equal("Book")
adjustPropertyNameForGettingArrayChildType("show_list").should.be.equal("Show")
fun testArrayPropertyName(propertyName: String, expected: String) {
resetJsonToKotlinRandom()
adjustPropertyNameForGettingArrayChildType(propertyName).should.be.equal(expected)
}

testArrayPropertyName("", "X")
testArrayPropertyName("List", "Item0")
testArrayPropertyName("arrayList", "Array")
testArrayPropertyName("Apples", "Apple")
testArrayPropertyName("Activities", "Activity")
testArrayPropertyName("Book_List", "Book")
testArrayPropertyName("show_list", "Show")
}

@Test
Expand Down

0 comments on commit abd3438

Please sign in to comment.