File tree 2 files changed +72
-6
lines changed
main/kotlin/wu/seal/jsontokotlin/utils
test/kotlin/wu/seal/jsontokotlin/regression 2 files changed +72
-6
lines changed Original file line number Diff line number Diff line change @@ -290,16 +290,16 @@ private fun List<KotlinClass>.distinctByPropertiesAndSimilarClassNameOneTime():
290
290
}
291
291
return replaceReferencedClasses(rule)
292
292
}
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
297
294
// obtain the class to replace other ref class, if the replace times is less then 2, then it means that no need to replace
298
295
val targetClass = groupBy { it.codeWithoutXAndNumberClassName() }.maxByOrNull { it.value.size }
299
296
?.takeIf { it.value.size > 1 }?.value?.first() ? : return this
300
297
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
+ }
303
303
it.replaceClassRefRecursive {
304
304
if (it.codeWithoutXAndNumberClassName() == targetClass.codeWithoutXAndNumberClassName()) {
305
305
targetClass
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments