gnodet-bot commented on code in PR #22358:
URL: https://github.com/apache/camel/pull/22358#discussion_r4683814039
##########
components/camel-xslt/src/main/java/org/apache/camel/component/xslt/XsltBuilder.java:
##########
@@ -489,7 +490,11 @@ protected void configureTransformer(Transformer
transformer, Exchange exchange)
addParameters(transformer, getParameters());
transformer.setParameter("exchange", exchange);
transformer.setParameter("in", exchange.getIn());
- transformer.setParameter("out", exchange.getOut());
+ // only set "out" param if a response exists; avoids creating an empty
OUT as side effect
+ Message response = ExchangeHelper.getResponse(exchange);
+ if (response != null) {
+ transformer.setParameter("out", response);
+ }
Review Comment:
💡 **Subtle behavioral regression — `$out` XSLT parameter**
Old code: `transformer.setParameter("out", exchange.getOut())` — `getOut()`
lazily created an empty OUT message, so `$out` was **always** defined in the
stylesheet (even if empty).
New code: `$out` is only set when `ExchangeHelper.getResponse(exchange) !=
null`. In any InOnly route (or InOut route before the response is created),
`$out` is now **undefined** in the stylesheet. XSLT processors that encounter
an undefined parameter usually return an empty string rather than throwing —
but stylesheets that declare `<xsl:param name="out"/>` and use it will silently
evaluate to empty instead of getting the message object.
The simplest fix that avoids the lazy-creation side effect and preserves
backward compatibility:
```suggestion
transformer.setParameter("out",
ExchangeHelper.getResponse(exchange));
```
Passing `null` for an XSLT parameter is equivalent to defining the param as
absent — no lazy OUT is created, and stylesheets that don't use `$out` are
unaffected. Stylesheets that do use it were already relying on an empty message
object, so `null` is semantically equivalent (and cleaner).
##########
core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java:
##########
@@ -1303,4 +1301,72 @@ public static <T> T getBodyAndResetStreamCache(Exchange
exchange, Class<T> type)
return exchange.getMessage().getBody(type);
}
+ /**
+ * Returns true if a response message has been explicitly set on this
exchange.
+ * <p>
+ * This is a non-deprecated equivalent of {@link Exchange#hasOut()},
provided as a utility until a proper
+ * {@code hasResponse()} method is added to the {@link Exchange} API.
+ */
+ public static boolean hasResponse(Exchange exchange) {
+ return exchange.getIn() != exchange.getMessage();
+ }
+
+ /**
+ * Returns the response message if one has been explicitly set, null
otherwise. Unlike the deprecated
+ * {@link Exchange#getOut()}, this does NOT lazily create an empty message.
+ * <p>
+ * This is provided as a utility until a proper {@code getResponse()}
method is added to the {@link Exchange} API.
+ */
+ public static Message getResponse(Exchange exchange) {
+ return hasResponse(exchange) ? exchange.getMessage() : null;
+ }
+
+ /**
+ * Sets the response message on this exchange. Unlike {@link
Exchange#getOut()} which lazily creates an empty
+ * message on read, this makes response creation explicit.
Review Comment:
📝 **Missing `@param` on `setResponse` Javadoc — null-is-clear semantics
undocumented**
`setResponse(exchange, null)` is used in many places to clear the response
(RedeliveryErrorHandler, PollEnricher, MarshalProcessor, WireTapProcessor). The
Javadoc only describes the "set" case. Add a `@param` clause documenting the
null-clears-response contract:
```suggestion
/**
* Sets the response message on this exchange. Unlike {@link
Exchange#getOut()} which lazily creates an empty
* message on read, this makes response creation explicit.
* <p>
* This is a non-deprecated equivalent of {@link
Exchange#setOut(Message)}, provided as a utility until a proper
* {@code setResponse(Message)} method is added to the {@link Exchange}
API.
*
* @param exchange the exchange
* @param response the response message, or {@code null} to clear any
existing response
*/
@SuppressWarnings("deprecation")
public static void setResponse(Exchange exchange, Message response) {
exchange.setOut(response);
}
```
--
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]