Skip to content

Commit 76689ff

Browse files
committed
Fix #431: Properties named "list" result in empty names of classes
1 parent d6abe59 commit 76689ff

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

src/main/kotlin/wu/seal/jsontokotlin/utils/TypeHelper.kt

+9-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ fun isExpectedJsonObjArrayType(jsonElementArray: JsonArray): Boolean {
111111
return jsonElementArray.firstOrNull()?.isJsonObject ?: false
112112
}
113113

114+
internal val random = Random(0)
115+
116+
fun resetJsonToKotlinRandom() {
117+
random.setSeed(0)
118+
}
119+
114120
/**
115121
* when get the child type in an array
116122
* ,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.
@@ -128,14 +134,16 @@ fun adjustPropertyNameForGettingArrayChildType(property: String): String {
128134
val firstLatterAfterListIndex = innerProperty.lastIndexOf("List") + 4
129135
if (innerProperty.length > firstLatterAfterListIndex && innerProperty[firstLatterAfterListIndex] in 'A'..'Z') {
130136
innerProperty = innerProperty.replaceFirst("List", "", false)
137+
} else if (innerProperty == "List") {
138+
innerProperty = "Item${random.nextInt(10)}"
131139
} else if (innerProperty.length == firstLatterAfterListIndex) {
132140
innerProperty = innerProperty.substring(0, innerProperty.lastIndexOf("List"))
133141
}
134142
}
135143

136144
innerProperty.contains("list") -> {
137145
if (innerProperty == "list") {
138-
innerProperty = "Item${Date().time.toString().last()}"
146+
innerProperty = "Item${random.nextInt()}"
139147
} else if (innerProperty.indexOf("list") == 0 && innerProperty.length >= 5) {
140148
val end = innerProperty.substring(5)
141149
val pre = (innerProperty[4] + "").toLowerCase()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package wu.seal.jsontokotlin.regression
2+
3+
import com.winterbe.expekt.should
4+
import org.junit.Test
5+
import wu.seal.jsontokotlin.generateKotlinClassCode
6+
import wu.seal.jsontokotlin.model.DefaultValueStrategy
7+
import wu.seal.jsontokotlin.model.TargetJsonConverter
8+
import wu.seal.jsontokotlin.test.TestConfig
9+
import wu.seal.jsontokotlin.utils.BaseTest
10+
import wu.seal.jsontokotlin.utils.resetJsonToKotlinRandom
11+
12+
class Issue431Test : BaseTest() {
13+
@Test
14+
fun testIssue431() {
15+
TestConfig.apply {
16+
isCommentOff = true
17+
targetJsonConvertLib = TargetJsonConverter.None
18+
defaultValueStrategy = DefaultValueStrategy.None
19+
isNestedClassModel = false
20+
}
21+
val json = """{"list":[{}]}"""
22+
val expected = """
23+
data class Test(
24+
val list: List<Item0>
25+
)
26+
27+
class Item0
28+
""".trimIndent()
29+
resetJsonToKotlinRandom()
30+
json.generateKotlinClassCode("Test").should.equal(expected)
31+
}
32+
}

src/test/kotlin/wu/seal/jsontokotlin/utils/TypeHelperKtTest.kt

+12-7
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,18 @@ class TypeHelperKtTest {
7070

7171
@Test
7272
fun adjustPropertyNameForGettingArrayChildTypeTest() {
73-
adjustPropertyNameForGettingArrayChildType("").should.be.equal("X")
74-
adjustPropertyNameForGettingArrayChildType("List").should.be.equal("")
75-
adjustPropertyNameForGettingArrayChildType("arrayList").should.be.equal("Array")
76-
adjustPropertyNameForGettingArrayChildType("Apples").should.be.equal("Apple")
77-
adjustPropertyNameForGettingArrayChildType("Activities").should.be.equal("Activity")
78-
adjustPropertyNameForGettingArrayChildType("Book_List").should.be.equal("Book")
79-
adjustPropertyNameForGettingArrayChildType("show_list").should.be.equal("Show")
73+
fun testArrayPropertyName(propertyName: String, expected: String) {
74+
resetJsonToKotlinRandom()
75+
adjustPropertyNameForGettingArrayChildType(propertyName).should.be.equal(expected)
76+
}
77+
78+
testArrayPropertyName("", "X")
79+
testArrayPropertyName("List", "Item0")
80+
testArrayPropertyName("arrayList", "Array")
81+
testArrayPropertyName("Apples", "Apple")
82+
testArrayPropertyName("Activities", "Activity")
83+
testArrayPropertyName("Book_List", "Book")
84+
testArrayPropertyName("show_list", "Show")
8085
}
8186

8287
@Test

0 commit comments

Comments
 (0)