davsclaus opened a new pull request, #30588:
URL: https://github.com/apache/camel/pull/30588

   JIRA: [CAMEL-27371](https://issues.apache.org/jira/browse/CAMEL-27371)
   
   ## What was wrong
   
   1. The CSV marshaller writes a header line only when `header` gives the 
column names. For a List of Maps (such as
      JSON rows turned into CSV) it could not use the keys of the first Map. 
The new `csv-serialize-action` Kamelet builds
      that header line itself.
   2. `header` was split on commas without trimming, so `id, name` gave the 
column `" name"`. Marshalling a Map with the
      key `name` wrote an empty field, and `useMaps=true` gave the key `" 
name"`.
   3. Unmarshalling a null body failed with `... read the data first - setBody 
with constant: resource:file:data.json
      ...`, which puts a JSON hint in a CSV error. The message comes from core: 
`UnmarshalProcessor.nullBodyException`
      uses `BodyFacts.hint(exchange, Need.A_BODY)` (CAMEL-25285 / CAMEL-25749), 
and that hint has a fixed `data.json`.
   
   ## The fix
   
   1. **New option `headerFromMapKeys`** (default `false`) on the CSV data 
format, in the model
      (`core/camel-core-model`) and in the component. When no header is 
configured and the first row is a Map with
      entries, `MapKeysHeaderMarshaller` writes the keys of that Map as the 
header line (the printer writes the format's
      header), and those keys are the columns of all the rows, as if they were 
given with `header`. Rows without a key
      get an empty field, and a key that only later rows have is not written. 
Without the option, a key that first shows
      up in a later row gets a new column (CAMEL-26085), but that cannot match 
a header line that was already written.
      The option works with a single Map, a List and an Iterator: the first row 
is peeked and put back. Keys that are not
      Strings also work. A body whose first row is a List gets no header line, 
`skipHeaderRecord=true` leaves the header
      line out, and a configured `header` wins.
   2. **Header names are trimmed** in `CsvDataFormat.createAndConfigureFormat`, 
for both the comma and the single-name
      case. Upgrade guide entry added.
   3. **Null-body hint**: minimal core change. `UnmarshalProcessor` passes the 
data format name as the `target` of
      `BodyFacts.hint(..., Need.A_BODY, name)`, and `BodyFacts.dataFile(name)` 
picks `data.csv`, `data.tsv`, `data.xml`,
      `data.yaml`, or `data.json` as before for JSON and unknown data formats. 
So a CSV unmarshal now says
      `resource:file:data.csv`. The other callers of the hint (jq, the `DATA` 
hint) are unchanged.
   
   Docs: `csv-dataformat.adoc` has a new section "Writing a header line from 
the keys of the first Map" (Java, XML and
   YAML) and a note that the header names are trimmed. The catalog copy is 
updated too.
   
   Generated files: model json, `CsvDataFormatConfigurer`, component json, 
xml-io `ModelParser`/`ModelWriter`, yaml-io
   `YamlModelWriter`, java-io `JavaDslModelWriter`, YAML DSL 
`ModelDeserializers` and the three schemas, and in the catalog
   the models and dataformats `csv.json`, `camel-xml-io.xsd`, 
`camel-spring.xsd`, `camelYamlDsl-model.json` and the docs.
   I built them in order (camel-support, camel-core-model, camel-core-reifier, 
xml-io, yaml-io, java-io, camel-csv,
   camel-spring-xml, yaml-dsl) into a scratch local repo, not `~/.m2`.
   
   ## Tests
   
   - `CsvMarshalHeaderFromMapKeysTest` (11 tests, new): List / single Map / 
Iterator of Maps, the first Map's keys as
     columns, non-String keys, List rows, a configured header wins, 
skipHeaderRecord, no option, the model builder
     (`dataFormat().csv().headerFromMapKeys(true)`, so the reifier is covered), 
and the `dataformat:` URI.
   - `CsvHeaderNamesTrimTest` (4 tests, new): marshal and unmarshal (`useMaps`) 
with `id, name`, on the URI too, and a
     single padded name.
   - `CsvUnmarshalNullBodyTest` (new): the message for a null body names 
`data.csv` and does not mention json.
   - `BodyFactsTest#testABodyHintNamesAFileOfTheDataFormat` (new, camel-core).
   - `mvn -o test` in camel-csv: 122 tests, 0 failures.
   - camel-core `BodyFactsTest`, `UnmarshalProcessorTest`, 
`MarshalProcessorTest`: 23 tests, 0 failures. The
     `MyDataFormat` message in `UnmarshalProcessorTest` still says `data.json`.
   
   _Claude Code on behalf of davsclaus_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to