jackson-dataformats-text icon indicating copy to clipboard operation
jackson-dataformats-text copied to clipboard

CSV missing columns for optional fields

Open simonhir opened this issue 8 months ago • 2 comments

Fields which are optional (@JsonProperty(required=false)) are still required as columns.

Expected behavior

Only columns for properties with @JsonProperty(required=false) are required.

Reproduce

@JsonIgnoreProperties(ignoreUnknown = true)
@Data
class CsvEntry {
    @JsonProperty(required = true)
    private String value1;
    @JsonProperty(required = true)
    private String value2;
    @JsonProperty
    private String value3;
    @JsonProperty
    private String value4;
}
final CsvMapper csvMapper = new CsvMapper();
final CsvSchema schema = csvMapper.typedSchemaFor(CsvEntry.class)
                .withHeader()
                .withColumnReordering(true);
csvMapper.readerFor(CsvEntry.class)
                .with(schema).readValues(inputStream);
type1,type2
asd,asd
asd,asd

Leads to following error:

Caused by: com.fasterxml.jackson.dataformat.csv.CsvReadException: Missing 1 header columns: ["type1","type2","type3","type4"]
 at [Source: (BufferedReader); line: 1, column: 48]
	at com.fasterxml.jackson.dataformat.csv.CsvReadException.from(CsvReadException.java:25)
	at com.fasterxml.jackson.dataformat.csv.CsvParser._reportCsvMappingError(CsvParser.java:1421)
	at com.fasterxml.jackson.dataformat.csv.CsvParser._readHeaderLine(CsvParser.java:936)
	at com.fasterxml.jackson.dataformat.csv.CsvParser._handleStartDoc(CsvParser.java:955)
	at com.fasterxml.jackson.dataformat.csv.CsvParser.nextToken(CsvParser.java:748)
	at com.fasterxml.jackson.databind.ObjectReader.readValues(ObjectReader.java:1949)

Workaround

By setting .without(CsvParser.Feature.FAIL_ON_MISSING_HEADER_COLUMNS) the error disappears but then even required columns are no longer required.

simonhir avatar Mar 13 '25 15:03 simonhir

Quick note: required-ness works different for columnar formats, since there must always be at least some kind of placeholder to keep column indexes matching.

Not sure if reported issue is wrt that but thought I'll add this thought.

cowtowncoder avatar Mar 13 '25 15:03 cowtowncoder

@cowtowncoder As you described, it could definitely be that this is not a real bug and that this is just the general way Jackson handles column formats. I just had the requirement, and by looking at the required property, I expected it to be also applied to the columns/header. If that's "by design", that would be a pity but fine with me.

simonhir avatar Mar 13 '25 16:03 simonhir