blackmo18 / kotlin-grass

Kotlin Csv to Data Class Parser
Apache License 2.0
38 stars 8 forks source link

Parsing error when a nullable field has a custom key name #12

Closed winterhate closed 3 years ago

winterhate commented 3 years ago

Describe the bug When a field key name is mapped using customKeyMap, the parser fails with java.lang.NumberFormatException: For input string: ""

For bug details see below. I attempted to create a fix as well, it fixes the test case I discovered and does not break any other, but... ⚠️ !!

Stack trace:

java.lang.NumberFormatException: For input string: ""
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
    at java.base/java.lang.Integer.parseInt(Integer.java:662)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at com.vhl.blackmo.grass.vein.PrimitiveType$Companion$int$1.invoke(PrimitiveType.kt:21)
    at com.vhl.blackmo.grass.vein.PrimitiveType$Companion$int$1.invoke(PrimitiveType.kt:19)
    at com.vhl.blackmo.grass.pot.Root.createObject(Root.kt:66)
    at com.vhl.blackmo.grass.pot.Stem.harvestData(Stem.kt:41)
    at com.vhl.blackmo.grass.pot.Plant.harvest(Plant.kt:29)

To Reproduce Test case:

            "parse null values with custom names" {
                val expected0 = NullableDataTypesCustomNames(null, null, null, null, null, null, null)
                val contents = readTestFile("/primitive-empty.csv")
                val parser = grass<NullableDataTypesCustomNames>() {
                    customKeyMap = mapOf(
                        "short" to "shortCustom", "int" to "intCustom", "long" to "longCustom",
                        "float" to "floatCustom", "double" to "doubleCustom", "boolean" to "booleanCustom",
                        "string" to "stringCustom"
                    )
                }
                val parsed = parser.harvest(contents)

                assertTrue { expected0 == parsed.first() }
            }

where NullableDataTypesCustomNames is

data class NullableDataTypesCustomNames(
        val shortCustom: Short?,
        val intCustom: Int?,
        val longCustom: Long?,
        val floatCustom: Float?,
        val doubleCustom: Double?,
        val booleanCustom: Boolean?,
        val stringCustom: String?
)

Expected behavior Parser should not fail. Parser should detect nullable type in the data class and use empty string as an null value indicator.

Environment Git revision 329f7f00a099dcd8695289354dd38bbc7b19ca9e

Screenshots Not needed...

Fix_parsing_error_when_a_nullable_field_has_a_custom_key_name.patch.gz

blackmo18 commented 3 years ago

Hi @winterhate,

thanks for reporting such issue. You may also create a pull request to the fix you made.

winterhate commented 3 years ago

Thanks for such a quick fix!