[BUG] useImport imports an empty record when the csv file ends with a line break
Describe the bug
useImport imports an extra, empty record when the CSV file ends with a line break, which is how most editors, scripts and unix tools write CSV files.
papaparse returns the trailing newline as a last row [""]. importCSVMapper zips it with the headers into { id: "", title: undefined }, and useImport sends that object to create (with batchSize: 1) or includes it in the same createMany request as the real rows (the default batch size is Number.MAX_SAFE_INTEGER). Depending on the backend, this adds a blank record or makes the whole createMany batch fail validation. onProgress/onFinish also report one more row than the file contains.
Root cause: packages/core/src/hooks/import/index.tsx:200 calls papaparse.parse(file, { complete, ...paparseOptions }) without skipEmptyLines. The existing test CSV has no trailing newline, so the suite does not cover this case.
Steps To Reproduce
- Create
data.csvwith 2 data rows and a trailing newline:"id","title" "35ad97dd-9379-480a-b6ac-6fc9c13e9224","Viral Strategist Local" "9a428977-1b03-4c3e-8cdd-1e4e2813528a","Concrete Soap Neural" - Call
useImport({ batchSize: 1 })andhandleChange({ file })with that file. dataProvider.createis called 3 times; the third call has{ id: "", title: undefined }.
As a failing test (added to packages/core/src/hooks/import/index.spec.tsx), on main @ 2352eb5b:
× useImport hook > should not import the empty line at the end of a csv file
→ expected "spy" to be called 2 times, but got 3 times
Tests 1 failed | 12 passed (13)Expected behavior
Only the 2 data rows are imported; create is called twice (or createMany receives 2 rows).
Packages
- @refinedev/core 5.2.0 (current
main, 2352eb5b)
Additional Context
A fix with a regression test is ready: pass skipEmptyLines: true to papaparse before ...paparseOptions, so callers can still override it (3 lines, plus a @refinedev/core patch changeset). With the fix, the import tests pass 13/13. I'd like to open the PR against this issue.
useImport has parsed without skipEmptyLines since 2021 (#1073).
Found and fixed by an AI agent (breken-ai), run against refine's own test suite.
Source: refinedev/refine