Skip to content

Bad parsing of surrounding quotes and line breaks #12

@pventurino

Description

@pventurino

CSVFileUtil.parseCSV(blob,boolean) doesn't correctly remove surrounding commas and fails to correctly parse line breaks in cells.

I wrote a custom test class for this utility (you can add it to your project)

@IsTest
public class CSVFileUtilTest {

    @IsTest
    static void testParseCsv() {
        String csv = 
              'One,Two,Three\n'
            + 'Tree,"Dog",""House""\n'
            + '"""Quotes""","Commas,commas","Line\nbreak"\n';
        
        List<String[]> actual = CSVFileUtil.parseCSV(Blob.valueOf(csv), false);
        
        System.assertEquals(
            new List<String[]>{
                new String[]{'One','Two','Three'},
                new String[]{'Tree','Dog','"House"'},
                new String[]{'"Quotes"','Commas,commas','Line\nbreak'}
            },
            actual);
    }
}

The result I get is:

System.AssertException: Assertion Failed: Expected: ((One, Two, Three), (Tree, Dog, "House"), ("Quotes", Commas,commas, Line
break)), Actual: ((One, Two, Three), (Tree, "Dog", "House"), (null,""Quotes"", "Commas,commas"), (null,break"))

Notice how "Dog" and "Commas,commas" should not be surrounded by quotes, and how "Line\nbreak" breaks the result (the first part is missing, and a couple null fields are added).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions