Skip to content

Commit 1079dd4

Browse files
authored
Merge pull request #433 from Kotlin/issue-425-fix
2 parents d6abe59 + d7ccd26 commit 1079dd4

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,16 +290,16 @@ private fun List<KotlinClass>.distinctByPropertiesAndSimilarClassNameOneTime():
290290
}
291291
return replaceReferencedClasses(rule)
292292
}
293-
//If class name the same with XXXX append or only diff with number, then treat them as the same class to do distinct
294-
val distinctClassesWhenClassNameSimilar = distinctBy {
295-
it.codeWithoutXAndNumberClassName()
296-
}
293+
//If class name the same with XXXX append or only diff with number, then group them in the same entry
297294
//obtain the class to replace other ref class, if the replace times is less then 2, then it means that no need to replace
298295
val targetClass = groupBy { it.codeWithoutXAndNumberClassName() }.maxByOrNull { it.value.size }
299296
?.takeIf { it.value.size > 1 }?.value?.first() ?: return this
300297

301-
return distinctClassesWhenClassNameSimilar.map {
302-
if (targetClass == it) return@map it
298+
return mapNotNull {
299+
if (targetClass == it) return@mapNotNull it
300+
if (it.codeWithoutXAndNumberClassName() == targetClass.codeWithoutXAndNumberClassName()) {
301+
return@mapNotNull null // removing the classes that we are replacing
302+
}
303303
it.replaceClassRefRecursive {
304304
if (it.codeWithoutXAndNumberClassName() == targetClass.codeWithoutXAndNumberClassName()) {
305305
targetClass
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
11+
class Issue425Test : BaseTest() {
12+
@Test
13+
fun testIssue425() {
14+
val json = """
15+
{
16+
"firstTeam": {
17+
"hometown": {
18+
"name": "Town 1"
19+
},
20+
"stats": {
21+
"rating": 10
22+
}
23+
},
24+
"secondTeam": {
25+
"hometown": {
26+
"name": "Town 2"
27+
},
28+
"stats": {
29+
"rating": 20
30+
}
31+
}
32+
}
33+
""".trimIndent()
34+
val expected = """
35+
data class Match(
36+
val firstTeam: FirstTeam,
37+
val secondTeam: SecondTeam
38+
)
39+
40+
data class FirstTeam(
41+
val hometown: Hometown,
42+
val stats: Stats
43+
)
44+
45+
data class SecondTeam(
46+
val hometown: Hometown,
47+
val stats: Stats
48+
)
49+
50+
data class Hometown(
51+
val name: String
52+
)
53+
54+
data class Stats(
55+
val rating: Int
56+
)
57+
""".trimIndent()
58+
TestConfig.apply {
59+
isCommentOff = true
60+
targetJsonConvertLib = TargetJsonConverter.None
61+
defaultValueStrategy = DefaultValueStrategy.None
62+
isNestedClassModel = false
63+
}
64+
json.generateKotlinClassCode("Match").should.equal(expected)
65+
}
66+
}

0 commit comments

Comments
 (0)