Skip to content

Commit

Permalink
Merge pull request #132 from nanjingboy/fix/generate
Browse files Browse the repository at this point in the history
[Fix]  Kotlin Data Class don't generated as expected
  • Loading branch information
wuseal authored Apr 2, 2019
2 parents df12f03 + 43f8568 commit f5b98c4
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
!.gitignore
!.travis.yml

local.properties

build/
out/
2 changes: 1 addition & 1 deletion src/main/kotlin/wu/seal/jsontokotlin/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fun JsonArray.onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive(): Boolean
return false
}

if (allItemAreObjectElement()) {
if (get(0).isJsonArray && get(0).asJsonArray.allItemAreObjectElement()) {
return true
}

Expand Down
81 changes: 81 additions & 0 deletions src/test/kotlin/wu/seal/jsontokotlin/regression/Issue121Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package wu.seal.jsontokotlin.regression

import com.winterbe.expekt.should
import org.junit.Before
import org.junit.Test
import wu.seal.jsontokotlin.KotlinDataClassCodeMaker
import wu.seal.jsontokotlin.test.TestConfig

class Issue121Test {

private val json = """{"data": [
[
{
"id": 1,
"question": "This is test question?",
"answer": "Ok i understand.",
"created": "2019-03-28T15:37:06+05:30",
"created_by": 0,
"modified": "2019-03-28T15:37:06+05:30",
"modified_by": 0,
"active": 1,
"is_del": 0
},
{
"id": 2,
"question": "What is soulmate?",
"answer": "answer",
"created": "2019-03-28T15:41:52+05:30",
"created_by": 0,
"modified": "2019-03-28T15:41:52+05:30",
"modified_by": 0,
"active": 1,
"is_del": 0
}
]
]
}"""

private val expected = """data class Test(
@SerializedName("data")
val `data`: List<List<Data>> = listOf()
) {
data class Data(
@SerializedName("active")
val active: Int = 0, // 1
@SerializedName("answer")
val answer: String = "", // answer
@SerializedName("created")
val created: String = "", // 2019-03-28T15:41:52+05:30
@SerializedName("created_by")
val createdBy: Int = 0, // 0
@SerializedName("id")
val id: Int = 0, // 2
@SerializedName("is_del")
val isDel: Int = 0, // 0
@SerializedName("modified")
val modified: String = "", // 2019-03-28T15:41:52+05:30
@SerializedName("modified_by")
val modifiedBy: Int = 0, // 0
@SerializedName("question")
val question: String = "" // What is soulmate?
)
}"""

/**
* init test environment before test
*/
@Before
fun setUp() {
TestConfig.setToTestInitState()
}

/**
* test issue #121 of Github Project issue
*/
@Test
fun testIssue089() {
val result = KotlinDataClassCodeMaker("Test", json).makeKotlinDataClassCode()
result.trim().should.be.equal(expected)
}
}
33 changes: 33 additions & 0 deletions src/test/kotlin/wu/seal/jsontokotlin/utils/ExtensionsKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,38 @@ class ExtensionsKtTest {

jsonArray.onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive().should.be.`false`
}

val subArrayString = """[
[
{
"id": 1,
"question": "This is test question?",
"answer": "Ok i understand.",
"created": "2019-03-28T15:37:06+05:30",
"created_by": 0,
"modified": "2019-03-28T15:37:06+05:30",
"modified_by": 0,
"active": 1,
"is_del": 0
},
{
"id": 2,
"question": "What is soulmate?",
"answer": "answer",
"created": "2019-03-28T15:41:52+05:30",
"created_by": 0,
"modified": "2019-03-28T15:41:52+05:30",
"modified_by": 0,
"active": 1,
"is_del": 0
}
]
]
""".trimIndent()
gson.fromJson<JsonArray>(
subArrayString,
JsonArray::class.java
).onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive().should.be.`true`

}
}

0 comments on commit f5b98c4

Please sign in to comment.