Claus Ibsen created CAMEL-25923:
-----------------------------------

             Summary: camel-core - unmarshal with allowNullBody drops the 
message headers when the body is null
                 Key: CAMEL-25923
                 URL: https://issues.apache.org/jira/browse/CAMEL-25923
             Project: Camel
          Issue Type: Bug
          Components: camel-core
    Affects Versions: 4.18.0, 4.22.0, 4.23.0
            Reporter: Claus Ibsen
             Fix For: 4.24.0


With {{allowNullBody=true}}, unmarshal skips the data format for a null body, 
as CAMEL-17720 added. It also drops the headers of the message:

{code:yaml}
- route:
    from:
      uri: timer:tick
      parameters:
        repeatCount: 1
      steps:
        - setHeader:
            name: orderId
            constant: "42"
        - unmarshal:
            allowNullBody: true
            json: {}
        - log: "orderId=${header.orderId}"
{code}

After the unmarshal, {{orderId}} is empty. The same steps with {{marshal}} 
instead keep it (marshal has {{allowNullBody}} since CAMEL-25284). Checked on 
4.24.0-SNAPSHOT.

*Cause*: for a null body, {{UnmarshalProcessor}} takes the out message without 
copying the in message into it (camel-support, 
{{UnmarshalProcessor.java:84-86}} on main):

{code:java}
if (allowNullBody && body == null) {
    // The body is null, and it is an allowed value so let's skip the 
unmarshalling
    out = exchange.getOut();
} else {
    // lets set up the out message before we invoke the dataFormat so that it 
can mutate it if necessary
    out = exchange.getOut();
    out.copyFrom(in);
{code}

{{getOut()}} creates an empty message, which then takes the place of the in 
message, so its headers are gone. {{MarshalProcessor}} returns early for a null 
body and leaves the message as it is. 
{{UnmarshalProcessorTest.testAllowNullBody}} checks only the body and the 
exception.

*Proposed change*: for a null body, leave the message as it is, as marshal does 
(with {{variableReceive}}, set that variable to null), and check the headers in 
the test.

Since Camel 3.16 (CAMEL-17720); in 4.18.0, 4.22.0 and 4.23.0.

_Claude Code on behalf of davsclaus_



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

Reply via email to