Skip to content

Leave out CSV fields past the last header instead of throwing KeyNotFoundException - #1072

Merged
michelebastione merged 1 commit into
mini-software:masterfrom
vtxe02023274:fix/csv-extra-fields-with-header
Nov 25, 2026
Merged

michelebastione merged 1 commit into
mini-software:masterfrom
vtxe02023274:fix/csv-extra-fields-with-header

Conversation

@vtxe02023274

@vtxe02023274 vtxe02023274 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1071.

With hasHeaderRow: true, CsvReader.QueryAsync looked up headRows[i] for every field of a row, so a row with more fields than the header (a trailing separator is enough) threw KeyNotFoundException. The header branch now stops at the header count, so fields without a column name are left out, as the OpenXml reader does for cells without a header. Rows with fewer fields still throw ColumnNotFoundException or are filled by FillMissingColumns as before, and reading without a header row is unchanged.

Test: GithubIssuesTests.ExtraFieldsWithHeaderRow reads A,B\n1,2,\n3,4,5,6\n with hasHeaderRow: true through Query, QueryAsync and GetDataReader. It fails on master (KeyNotFoundException: The given key '2' was not present in the dictionary.) and passes with this change.

Ran (Windows): dotnet test tests/MiniExcel.Csv.Tests on net8.0, net10.0 and net11.0, 92 passed each. dotnet test tests/MiniExcel.OpenXml.Tests -f net10.0, 564 passed. net9.0 not run locally (no runtime here).

Summary by CodeRabbit

  • Bug Fixes
    • CSV imports with a header row now ignore extra fields that have no corresponding header, rather than failing when processing them. This behavior applies to both synchronous and asynchronous queries. Fields with matching headers continue to be available by name, and existing validation for rows with fewer fields than headers remains unchanged.

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Header-row parsing now skips CSV fields beyond the available headers. Tests cover synchronous and asynchronous queries and confirm that the data reader exposes only the named fields.

Changes: CSV header mapping

Layer / File(s) Summary
Bounded header assignment
src/MiniExcel.Csv/CsvReader.cs, tests/MiniExcel.Csv.Tests/Issues/GithubIssuesTests.cs
Header-row parsing limits assignments to the number of available headers. Tests verify that synchronous and asynchronous queries and the data reader omit extra fields, including an empty trailing field.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: michelebastione, shps951023

Merge Risk: Minimal · up to 6ee7a

CSV rows with more fields than headers no longer throw KeyNotFoundException when a header row is used; the extra fields are omitted. The change is small and localized, and I found no merge-blocking risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main behavior change: CSV fields past the last header are left out instead of throwing KeyNotFoundException. The raw summary says header-row parsing now stops at the smaller of the…
Linked Issues check Passed [#1071] The requirement: with hasHeaderRow: true, fields past the last header must be omitted instead of throwing KeyNotFoundException. The reviewed head changes CsvReader.QueryAsync (src/MiniExcel.Cs…
Out of Scope Changes check Passed The whole-PR diff against the merge base 27caf1e touches two files: src/MiniExcel.Csv/CsvReader.cs (a 4-line change plus one comment, limited to the header-row field loops) and tests/MiniExcel.Csv.Tes…
Full details: Title check

Explanation

The title names the main behavior change: CSV fields past the last header are left out instead of throwing KeyNotFoundException. The raw summary says header-row parsing now stops at the smaller of the field count and the header count, and the added test covers trailing fields with headers A,B. The title matches the changeset and the linked issue objective.

Full details: Linked Issues check

Explanation

[#1071] The requirement: with hasHeaderRow: true, fields past the last header must be omitted instead of throwing KeyNotFoundException. The reviewed head changes CsvReader.QueryAsync (src/MiniExcel.Csv/CsvReader.cs). The header branch now computes fieldCount = Math.Min(read.Length, headRows.Count) and iterates only to fieldCount in both the ReadEmptyFieldsAsDefault and the non-default branch. For 'A,B' with row '1,2,', the row yields A = "1" and B = "2" and the extra field is dropped, as the issue expects. The synchronous Query is generated from QueryAsync via [CreateSyncVersion]. GetDataReader is reported in the issue as failing the same way; the diff touches the single shared header loop, and the test ExtraFieldsWithHeaderRow covers Query, QueryAsync and GetDataReader. Rows with fewer fields still reach the unchanged ColumnNotFoundException check or FillMissingColumns handling. The no-header path (body branch) is unchanged, as the issue requires. Test results were reported by the author and were not re-run here. Test source was not inspected in this assessment.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base 27caf1e touches two files: src/MiniExcel.Csv/CsvReader.cs (a 4-line change plus one comment, limited to the header-row field loops) and tests/MiniExcel.Csv.Tests/Issues/GithubIssuesTests.cs (new ExtraFieldsWithHeaderRow test covering the issue scenario). Both changes connect directly to #1071. No unrelated refactoring or behavior change was found in the reviewed diff. The reviewed diff for CsvReader.cs was read in full; the test file was assessed from the summary only.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 98a16560-d5d6-4867-a235-a496305c2f35
Commits

Reviewing files that changed from the base of the PR and between 27caf1e and 6ee7aa7.

Files selected for processing: 2

  • src/MiniExcel.Csv/CsvReader.cs
  • tests/MiniExcel.Csv.Tests/Issues/GithubIssuesTests.cs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@michelebastione
michelebastione merged commit 1de1748 into mini-software:master Nov 25, 2026
8 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] - CSV rows with more fields than the header row throw KeyNotFoundException

2 participants