jsoizo/kotlin-csv

Wrong quotes and missing escape characters on writing CSV with WriteQuoteMode.ALL

Floern opened this issue · 2 comments

When we use WriteQuoteMode.ALL, it does not use the quote character we specified in the CsvWriteQuoteContext but just uses " instead, and it does not escape any special character at all, even producing invalid CSV output.

Looking at the source code, it's pretty obvious why:
https://github.com/doyaaaaaken/kotlin-csv/blob/8dbaa0742579f354cd973f230cf95dbad39f4263/src/jvmMain/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvFileWriter.kt#L130

@Floern
That's right. Thank you very much for finding the bug and send me PR!
I wrote the below test code for checking it and it failed.
(And this bug was due to this line you pointed. https://github.com/doyaaaaaken/kotlin-csv/blob/8dbaa0742579f354cd973f230cf95dbad39f4263/src/jvmMain/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvFileWriter.kt#L130 )

"write csv with custom quote character on WriteQuoteMode.ALL mode" {
    val rows = listOf(listOf("a1", "b1"), listOf("a2", "b2"))
    val expected = "_a1_,_b1_\r\n_a2_,_b2_\r\n"
    csvWriter{
        quote {
            mode = WriteQuoteMode.ALL
            char = '_'
        }
    }.writeAll(rows, testFileName)
    val actual = readTestFile()
    actual shouldBe expected
}

released at v0.11.1