Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #431: Properties named "list" result in empty names of classes #432

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading