gnodet-bot commented on code in PR #28950:
URL: https://github.com/apache/camel/pull/28950#discussion_r4447875123
##########
components/camel-alibaba/camel-alibaba-mns/src/main/java/org/apache/camel/component/alibaba/mns/MNSUtils.java:
##########
@@ -121,6 +129,46 @@ public static String resolveMessageBody(Exchange exchange)
{
return exchange.getMessage().getBody(String.class);
}
+ /**
+ * Sets the body of the exchange as the body of the MNS message to send or
publish. Text is sent as it is. Bytes, a
+ * stream or a file are sent as their bytes, so that binary data is not
changed. A body that has no bytes, such as a
+ * number, a Map, a collection or a POJO, is sent as its text.
+ */
+ public static void setMessageBody(Exchange exchange, BaseMessage message) {
+ Object body = exchange.getMessage().getBody();
+ // not a collection or an array other than byte[], which the type
converter would turn into an array of bytes
+ // element by element (failing for a List of String) instead of
sending their text
+ if (body != null && !(body instanceof String) && !(body instanceof
Collection<?>)
+ && !(body.getClass().isArray() && !(body instanceof byte[]))) {
Review Comment:
⚠️ **`Map` is not a `Collection` — the guard is imprecise**
`java.util.Map` does **not** implement `java.util.Collection` (it implements
`java.util.Map`), so a `Map.of("key","value")` body passes all three exclusion
checks:
- `!(body instanceof String)` → true
- `!(body instanceof Collection<?>)` → **true** (Map is not a Collection)
- `!(body.getClass().isArray() ...)` → true
It then falls into `exchange.getMessage().getBody(byte[].class)`. Today this
returns `null` (no Map→byte[] converter in Camel), so it falls through to
`resolveMessageBody()` and is correctly sent as `toString()`. But this is
accidental — if any custom component registers a `Map→byte[]` converter (e.g. a
JSON marshaller), Maps would be silently sent as binary, contradicting the
Javadoc and the documented behaviour.
The `mapIsSentAsItsText` test passes because there is currently no such
converter, not because the guard prevents the wrong path.
Add `Map` to the exclusion guard alongside `Collection`:
```suggestion
if (body != null && !(body instanceof String) && !(body instanceof
Map<?, ?>)
&& !(body instanceof Collection<?>)
&& !(body.getClass().isArray() && !(body instanceof
byte[]))) {
```
You'll also need `import java.util.Map;` (alongside the existing `import
java.util.Collection;`). Add a `mapIsSentAsItsText` variant that installs a
dummy Map→byte[] converter to lock this in permanently.
--
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]