From 76689ff476a08d5e89597f3069085c1550cd3d42 Mon Sep 17 00:00:00 2001 From: "Arkady.Bazhanov" Date: Sun, 14 Jul 2024 20:05:06 +0200 Subject: [PATCH] Fix #431: Properties named "list" result in empty names of classes --- .../wu/seal/jsontokotlin/utils/TypeHelper.kt | 10 +++++- .../jsontokotlin/regression/Issue431Test.kt | 32 +++++++++++++++++++ .../jsontokotlin/utils/TypeHelperKtTest.kt | 19 +++++++---- 3 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 src/test/kotlin/wu/seal/jsontokotlin/regression/Issue431Test.kt diff --git a/src/main/kotlin/wu/seal/jsontokotlin/utils/TypeHelper.kt b/src/main/kotlin/wu/seal/jsontokotlin/utils/TypeHelper.kt index 002bd54b..3dab3b68 100755 --- a/src/main/kotlin/wu/seal/jsontokotlin/utils/TypeHelper.kt +++ b/src/main/kotlin/wu/seal/jsontokotlin/utils/TypeHelper.kt @@ -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. @@ -128,6 +134,8 @@ 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")) } @@ -135,7 +143,7 @@ fun adjustPropertyNameForGettingArrayChildType(property: String): String { 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() diff --git a/src/test/kotlin/wu/seal/jsontokotlin/regression/Issue431Test.kt b/src/test/kotlin/wu/seal/jsontokotlin/regression/Issue431Test.kt new file mode 100644 index 00000000..16d83980 --- /dev/null +++ b/src/test/kotlin/wu/seal/jsontokotlin/regression/Issue431Test.kt @@ -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 + ) + + class Item0 + """.trimIndent() + resetJsonToKotlinRandom() + json.generateKotlinClassCode("Test").should.equal(expected) + } +} diff --git a/src/test/kotlin/wu/seal/jsontokotlin/utils/TypeHelperKtTest.kt b/src/test/kotlin/wu/seal/jsontokotlin/utils/TypeHelperKtTest.kt index f0ae0fa8..76d5b15e 100755 --- a/src/test/kotlin/wu/seal/jsontokotlin/utils/TypeHelperKtTest.kt +++ b/src/test/kotlin/wu/seal/jsontokotlin/utils/TypeHelperKtTest.kt @@ -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