Claus Ibsen created CAMEL-25325:
-----------------------------------

             Summary: camel-datasonnet - XML elements that appear once vs 
several times, and leaked ~/$ keys
                 Key: CAMEL-25325
                 URL: https://issues.apache.org/jira/browse/CAMEL-25325
             Project: Camel
          Issue Type: Improvement
            Reporter: Claus Ibsen


Reading XML in DataSonnet:

* A repeated element comes through as an array when there are several and as an 
object when there is one. {{<Items><Item/></Items>}} gives {{Item: \{...\}}} 
but two Items give {{Item: \[...\]}}. A mapping such as {{std.map(function(i) 
i\['@sku'\], body.Order.Items.Item)}} works on a test message with two items 
and fails in production with one: "Wrong parameter type: expected Array, got 
object". This is a very common mistake for people and AI models alike, and the 
docs give no guidance.
* XML conversion adds keys for element order ({{~}}) and text ({{$}}, {{$1}}, 
... for mixed content). When part of an XML document is mapped straight to 
JSON, those keys end up in the output, e.g. {{\{"@sku":"x","$":"2","~":1\}}}.

Proposal:
* Add helpers to camel.libsonnet, e.g. {{c.asArray(x)}} (null gives [], an 
object gives [object], an array stays as is) and something to strip the 
{{~}}/{{$}} keys or turn {{$}} into a value.
* Add a section to the datasonnet-language doc on reading XML: attributes 
{{\['@id'\]}}, text {{\['$'\]}}, one vs many, and the leaked keys, with 
examples.
* Use {{c.asArray}} when translating DataWeave {{.*}} (see the related 
DataWeave issue).

_Claude Code on behalf of davsclaus_



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to