Skip to content

Commit 42d565f

Browse files
authored
Merge pull request #274 from wuseal/dev_3.6.0/bugfix/#269
dev_3.6.0/bugfix/#269
2 parents 900cde9 + e8faf89 commit 42d565f

File tree

5 files changed

+1614
-11
lines changed

5 files changed

+1614
-11
lines changed

src/main/kotlin/wu/seal/jsontokotlin/model/classscodestruct/GenericListClass.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ data class GenericListClass(override val generic: KotlinClass) : UnModifiableGen
2222
LogUtil.i("Can't find replacement for ${generic.name}")
2323
this
2424
} else {
25-
copy(generic = replaceRule.values.toList()[0])
25+
copy(generic = replacement)
2626
}
2727
}
2828
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ fun JsonArray.allElementAreSamePrimitiveType(): Boolean {
169169
return allElementAreSamePrimitiveType
170170
}
171171

172-
private fun theSamePrimitiveType(first: JsonPrimitive, second: JsonPrimitive): Boolean {
172+
fun theSamePrimitiveType(first: JsonPrimitive, second: JsonPrimitive): Boolean {
173173

174174
val sameBoolean = first.isBoolean && second.isBoolean
175175

src/main/kotlin/wu/seal/jsontokotlin/utils/classgenerator/GenericListClassGeneratorByJSONArray.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class GenericListClassGeneratorByJSONArray(private val jsonKey: String, jsonArra
4141
return GenericListClass(generic = dataClassFromJsonObj)
4242
}
4343
jsonArray.allItemAreArrayElement() -> {
44-
val fatJsonArray = getFatJsonArray(jsonArray)
44+
val fatJsonArray = getFatJsonArray(jsonArray.map { it.asJsonArray })
4545
val genericListClassFromFatJsonArray = GenericListClassGeneratorByJSONArray(jsonKey, fatJsonArray.toString()).generate()
4646
LogUtil.i("$tag jsonArray allItemAreArrayElement, return GenericListClass with generic type ${genericListClassFromFatJsonArray.name}")
4747
return GenericListClass(generic = genericListClassFromFatJsonArray)
@@ -57,13 +57,10 @@ class GenericListClassGeneratorByJSONArray(private val jsonKey: String, jsonArra
5757
return adjustPropertyNameForGettingArrayChildType(jsonKey)
5858
}
5959

60-
private fun getFatJsonArray(jsonArray: JsonArray): JsonArray {
61-
if (jsonArray.size() == 0 || !jsonArray.allItemAreArrayElement()) {
62-
throw IllegalStateException("input arg jsonArray must not be empty and all element should be json array! ")
63-
}
60+
private fun getFatJsonArray(jsonArrayList: List<JsonArray>): JsonArray {
6461
val fatJsonArray = JsonArray()
65-
jsonArray.forEach {
66-
fatJsonArray.addAll(it.asJsonArray)
62+
jsonArrayList.forEach {
63+
fatJsonArray.addAll(it)
6764
}
6865
return fatJsonArray
6966
}
@@ -87,11 +84,19 @@ class GenericListClassGeneratorByJSONArray(private val jsonKey: String, jsonArra
8784
// and will not be generated in final code
8885
fatJsonObject.add(key + BACKSTAGE_NULLABLE_POSTFIX, value)
8986
} else if (fatJsonObject.has(key)) {
90-
if (fatJsonObject[key].isJsonObject && value.isJsonObject) {
87+
if (fatJsonObject[key].isJsonObject && value.isJsonObject) {//try not miss any fields of the key's related json object
9188
val newValue = getFatJsonObject(JsonArray().apply { add(fatJsonObject[key]);add(value) })
9289
fatJsonObject.add(key, newValue)
90+
} else if (fatJsonObject[key].isJsonArray && value.isJsonArray) {////try not miss any elements of the key's related json array
91+
val newValue = getFatJsonArray(listOf(fatJsonObject[key].asJsonArray, value.asJsonArray))
92+
fatJsonObject.add(key, newValue)
93+
} else if (fatJsonObject[key].isJsonPrimitive && value.isJsonPrimitive && theSamePrimitiveType(fatJsonObject[key].asJsonPrimitive, value.asJsonPrimitive)) {
94+
//if the value and exist value are the same primitive type then ignore it
95+
} else if (value.isJsonNull) {
96+
//if the value is null, we ignore this value
9397
} else {
94-
fatJsonObject.add(key, value)
98+
//others the two values of the key are not the same type, then give it a null value indicate that it's should be an Any Type in Kotlin
99+
fatJsonObject.add(key, JsonNull.INSTANCE)
95100
}
96101
} else {
97102
fatJsonObject.add(key, value)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package wu.seal.jsontokotlin.model.classscodestruct
2+
3+
import com.winterbe.expekt.should
4+
import org.junit.Test
5+
6+
import org.junit.Assert.*
7+
import org.junit.Before
8+
import wu.seal.jsontokotlin.test.TestConfig
9+
10+
class GenericListClassTest {
11+
12+
@Before
13+
fun setUp() {
14+
TestConfig.setToTestInitState()
15+
}
16+
17+
@Test
18+
fun replaceReferencedClasses() {
19+
val genericListClass = GenericListClass(generic = KotlinClass.ANY)
20+
val newGenericListClass = genericListClass.replaceReferencedClasses(mapOf(KotlinClass.BOOLEAN to KotlinClass.DOUBLE, KotlinClass.ANY to KotlinClass.BOOLEAN))
21+
newGenericListClass.generic.should.be.equal(KotlinClass.BOOLEAN)
22+
}
23+
}

0 commit comments

Comments
 (0)