Fix CSV columnizer crash when "first line contains field names" is unchecked (#707) - #717
Merged
Merged
Conversation
CsvHelper's ReadHeader() throws when HasHeaderRecord is false, so Selected() failed and every line fell back to a single unsplit column. Read fields from the parser record instead, and name generated columns "Column 1".."Column N" (was string-concatenated to "Column 01", "Column 11").
…#707) Selected() let CsvHelper's BadDataException escape when the first line doesn't parse with the configured settings, e.g. a quoted ';' file read with a ',' delimiter loaded from csvcolumnizer.json. Catch it and use the same single-column fallback SplitCsvLine already uses.
The columnizer instance the user selects or configures is often a clone that loaded csvcolumnizer.json and never pre-processed the file, so it kept the saved delimiter (e.g. ',' from an earlier comma file) instead of the file's own. Detect the delimiter from the file's first line in Selected() and before showing the config dialog; after OK, rebuild the columns without re-detecting so a delimiter typed by the user sticks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #707
Problem
ReadHeader()throwsReaderException: Configuration.HasHeaderRecord is false., and the columnizer called it for every line, including when selecting the columnizer (Selected()).Column 01,Column 11, …csvcolumnizer.jsonand never ranPreProcessLine()on the file. It kept the saved delimiter (e.g.,from an earlier comma file), not the file's own. A quoted;file parsed with,is bad CSV data, soSelected()threwBadDataException. This happened with or without a header.Changes (
CsvColumnizer)ReadFields()helper reads each line's fields fromcsv.Parser.Recordinstead of the header API.Selected()andSplitCsvLine()both use it. Behavior for files with a header is unchanged (HeaderRecordwas justParser.Record).Column 1…Column N.Selected()falls back to a singleTextcolumn, asSplitCsvLine()already does. There's no exception dialog anymore.Selected()detects the delimiter from the file's first line, asPreProcessLine()does when a file loads.Configure()detects before opening the dialog, so the dialog shows the file's delimiter.Tests
New tests in
CSVColumnizerTest:Selected()gives 7 columns namedColumn 1–Column 7,SplitLine()returns the 7 field values, and the first line stays visible.,delimiter on a;file: fields are split correctly without a header, and column names come from the header with one.Textin both header modes.Known limitations (not changed)
PreProcessLine()behavior).Configure()still saves the detected delimiter tocsvcolumnizer.json.