shashank created CAMEL-26085:
--------------------------------
Summary: camel-csv - marshalling a List<Map> without a header puts
the values of a row in the wrong columns when its keys are in another order or
one is missing
Key: CAMEL-26085
URL: https://issues.apache.org/jira/browse/CAMEL-26085
Project: Camel
Issue Type: Bug
Components: camel-csv
Reporter: shashank
Assignee: shashank
When no {{header}} is set (or {{headerDisabled=true}}, which drops it),
{{CsvMarshaller.DynamicColumnsMarshaller}} writes each map of a {{List<Map>}}
body with {{map.values()}}, so in the iteration order of that map (main
6a2f66ddec3e). Within one body the column of a key depends on each row:
{{[{id=1, name=Alice, city=Oslo}, {name=Bob, city=Rome, id=2}]}} gives
{{1,Alice,Oslo}} and {{Bob,Rome,2}}; {{[{id=1, name=Alice, city=Oslo}, {id=2,
city=Rome}]}} gives {{2,Rome}}, so {{Rome}} is under {{name}}. No error and no
header line to notice it by. It happens when JSON objects or documents are
marshalled to CSV ({{unmarshal().json()}} then {{marshal().csv()}}, MongoDB
documents, Jackson {{NON_NULL}}): the system that imports the file reads values
in the wrong columns. Camel 2.15 kept a set of columns in the marshaller;
commit bbc905eb6f (2.16) removed it because it was shared by all exchanges, and
the alignment inside one body went with it. camel-univocity-parsers aligns by
key for the same body.
*Fix*: {{CsvMarshaller.marshal}} asks a new protected
{{createMapRecordValues()}} once per body (default {{getMapRecordValues}}, so
custom marshallers work as before). {{DynamicColumnsMarshaller}} keeps the
columns of the body: the keys of the first map, then the keys of later maps
that the first one does not have, in the order they first appear; a missing key
is a null value (an empty field, or {{nullString}}). Nothing is kept across
bodies. The data format page says how the columns are chosen; upgrade guide
4.24 entry.
*Tests*: new {{CsvMarshalMapRowsColumnOrderTest}}, 5 tests; 4 fail on main, for
example {{array contents differ at index [1], expected: <2,Bob,Rome> but was:
<Bob,Rome,2>}} and {{expected: <2,,Rome> but was: <2,Rome>}}; the control that
each body has its own columns passes on main. Unit tests of camel-csv with the
fix: 98 run, 0 failures.
*How it was found*: a reference differential of the CSV data formats against an
RFC 4180 model and univocity.
*Affected*: main, camel-4.23.x, camel-4.22.x, camel-4.18.x and camel-4.14.x
(same marshaller). Read, not run.
_Filed with Claude Code on behalf of allthingssecurity._
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)