question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Missing columns from header line (compare to `CsvSchema`) not detected when reordering columns (add `CsvParser.Feature.FAIL_ON_MISSING_HEADER_COLUMNS`)

See original GitHub issue

When reading given CSV with jackson-dataformat-csv 2.11.4

name
Roger
Chris

using following snippet

CsvMapper csvMapper = new CsvMapper();
csvMapper.configure(CsvParser.Feature.FAIL_ON_MISSING_COLUMNS, true);
CsvSchema csvSchema = CsvSchema.builder().setUseHeader(true).setReorderColumns(true)
        .addColumn("name").addColumn("age").build();
List<Person> persons = csvMapper
        .readerFor(Person.class)
        .with(csvSchema)
        .<Person> readValues(csv)
        .readAll();
...
class Person {
    public String name;
    public int age;
}

doesn’t throw a CsvMappingException although age column is missing in CSV.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Aug 21, 2022

Ok: changed the behavior to optionally throw an exception if column headers are missing (compared to registered CsvSchema), controller by new

CsvParser.Feature.FAIL_ON_MISSING_HEADER_COLUMNS

which defaults to true (that is, by default this problem is reported).

Will be in 2.14.0 release

0reactions
norrisjeremycommented, Nov 10, 2022

I.e., should there be something like this?

diff --git a/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java b/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java
index b7b050e4..c3621ae9 100644
--- a/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java
+++ b/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java
@@ -862,14 +862,16 @@ public class CsvParser
             }
         }
         // [dataformats-text#285]: Are we missing something?
-        int diff = schemaColumnCount - newColumnCount;
-        if (diff > 0) {
-            Set<String> oldColumnNames = new LinkedHashSet<>();
-            _schema.getColumnNames(oldColumnNames);
-            oldColumnNames.removeAll(newSchema.getColumnNames());
-            _reportCsvMappingError(String.format("Missing %d header column%s: [\"%s\"]",
-                    diff, (diff == 1) ? "" : "s",
-                            String.join("\",\"", oldColumnNames)));
+        if (CsvParser.Feature.FAIL_ON_MISSING_HEADER_COLUMNS.enabledIn(_formatFeatures)) {
+            int diff = schemaColumnCount - newColumnCount;
+            if (diff > 0) {
+                Set<String> oldColumnNames = new LinkedHashSet<>();
+                _schema.getColumnNames(oldColumnNames);
+                oldColumnNames.removeAll(newSchema.getColumnNames());
+                _reportCsvMappingError(String.format("Missing %d header column%s: [\"%s\"]",
+                        diff, (diff == 1) ? "" : "s",
+                                String.join("\",\"", oldColumnNames)));
+            }
         }
 
         // otherwise we will use what we got
Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson CSV missing columns - java - Stack Overflow
I know this is an old thread, but as I run into the same question myself, let me share the solution : csvMapper.configure(CsvParser.Feature....
Read more >
CsvParser.Feature (Jackson-dataformat-CSV 2.12.3 API)
Feature that allows ignoring of unmappable "extra" columns; that is, values for columns that appear after columns for which types are defined.
Read more >
How to Read or Parse CSV files with Header in Java ... - Java67
How to parse a CSV file with column header in Java using Jackson ... CSV to Java objects while CsvSchema defines Schema as...
Read more >
Reading CSV with Jackson - cowtowncoder
It may or may not contain logical names for columns (more on this bit later). Since Jackson API was designed to work with...
Read more >
com.fasterxml.jackson.dataformat.csv.CsvSchema.builder java ...
Accessor for creating a "default" CSV schema instance, with following * settings: *<ul> * <li>Does NOT use header line * </li> * <li>Uses...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found