wuseal / JsonToKotlinClass

🚀 Plugin for Android Studio And IntelliJ Idea to generate Kotlin data class code from JSON text ( Json to Kotlin )
GNU General Public License v3.0
3.1k stars 176 forks source link

Similar classes get incorrectly deduplicated #425

Closed ark-1 closed 4 months ago

ark-1 commented 8 months ago

Steps to reproduce: Input the following JSON:

{
  "firstTeam": {
    "hometown": {
      "name": "Town 1"
    },
    "stats": {
      "rating": 10
    }
  },
  "secondTeam": {
    "hometown": {
      "name": "Town 2"
    },
    "stats": {
      "rating": 20
    }
  }
}

Expected output:

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
)

Actual output:

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: StatsX
)

data class Hometown(
    val name: String
)

data class Stats(
    val rating: Int
)

StatsX was not generated, but is referenced from SecondTeam.

wuseal commented 6 months ago

As i thought the expected result should be:

data class Match(
    val firstTeam: FirstTeam,
    val secondTeam: FirstTeam
)

data class FirstTeam(
    val hometown: Hometown,
    val stats: Stats
)

data class Hometown(
    val name: String
)

data class Stats(
    val rating: Int
)

as 'FirstTeam' same with 'SecondTeam' Maybe you need update your UT check

ark-1 commented 6 months ago

Currently, only classes with the same name (not considering XX suffixes) are deduplicated. There's no code for deduplicating classes with different names. And the code for classes with same names is broken. I suggest fixing that (by merging #426), and opening another issue if you want to extend this behavior to classes with different names (which I don't expect to be trivial).