Defect #44603
openField wrapper is wrongly detected as a single quote when importing a CSV file that contains apostrophes
Description
The CSV import detects the field wrapper automatically (#39511). However, when a CSV file has no wrapped fields and some values contain apostrophes, the wrapper is wrongly detected as a single quote, and the file cannot be parsed with that setting.
To reproduce the problem, save the following content as a CSV file and upload it on the user import page. "Single quote" is selected as the field wrapper, although "Double quote" is expected.
login,firstname,lastname,mail jobrien,John,O'Brien,jobrien@example.net dwilliams,De'Andre,Williams,dwilliams@example.net soconnor,Sean,O'Connor,soconnor@example.net
This happens because Import#set_default_settings chooses the wrapper by comparing how many times " and ' appear at the beginning of the file. The file above contains no double quotes and three apostrophes.
wrapper = ['"', "'"].max_by {|quote_char| content.count(quote_char)}
The attached patch fixes this by counting only the quote characters that actually wrap a field, that is, those placed at both ends of a field. When neither character wraps any field, the double quote is still used, as before.
Files
Related issues