Skip to content

Fix #425: Similar classes get incorrectly deduplicated #433

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

Merged
merged 1 commit 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
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)
}
}
Loading