Skip to content

Commit

Permalink
Merge pull request #433 from Kotlin/issue-425-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wuseal authored Jul 19, 2024
2 parents d6abe59 + d7ccd26 commit 1079dd4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/main/kotlin/wu/seal/jsontokotlin/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,16 @@ private fun List<KotlinClass>.distinctByPropertiesAndSimilarClassNameOneTime():
}
return replaceReferencedClasses(rule)
}
//If class name the same with XXXX append or only diff with number, then treat them as the same class to do distinct
val distinctClassesWhenClassNameSimilar = distinctBy {
it.codeWithoutXAndNumberClassName()
}
//If class name the same with XXXX append or only diff with number, then group them in the same entry
//obtain the class to replace other ref class, if the replace times is less then 2, then it means that no need to replace
val targetClass = groupBy { it.codeWithoutXAndNumberClassName() }.maxByOrNull { it.value.size }
?.takeIf { it.value.size > 1 }?.value?.first() ?: return this

return distinctClassesWhenClassNameSimilar.map {
if (targetClass == it) return@map it
return mapNotNull {
if (targetClass == it) return@mapNotNull it
if (it.codeWithoutXAndNumberClassName() == targetClass.codeWithoutXAndNumberClassName()) {
return@mapNotNull null // removing the classes that we are replacing
}
it.replaceClassRefRecursive {
if (it.codeWithoutXAndNumberClassName() == targetClass.codeWithoutXAndNumberClassName()) {
targetClass
Expand Down
66 changes: 66 additions & 0 deletions src/test/kotlin/wu/seal/jsontokotlin/regression/Issue425Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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

class Issue425Test : BaseTest() {
@Test
fun testIssue425() {
val json = """
{
"firstTeam": {
"hometown": {
"name": "Town 1"
},
"stats": {
"rating": 10
}
},
"secondTeam": {
"hometown": {
"name": "Town 2"
},
"stats": {
"rating": 20
}
}
}
""".trimIndent()
val expected = """
data class Match(
val firstTeam: FirstTeam,
val secondTeam: SecondTeam
)
data class FirstTeam(
val hometown: Hometown,
val stats: Stats
)
data class SecondTeam(
val hometown: Hometown,
val stats: Stats
)
data class Hometown(
val name: String
)
data class Stats(
val rating: Int
)
""".trimIndent()
TestConfig.apply {
isCommentOff = true
targetJsonConvertLib = TargetJsonConverter.None
defaultValueStrategy = DefaultValueStrategy.None
isNestedClassModel = false
}
json.generateKotlinClassCode("Match").should.equal(expected)
}
}

0 comments on commit 1079dd4

Please sign in to comment.