davsclaus commented on code in PR #28042:
URL: https://github.com/apache/camel/pull/28042#discussion_r4306157869
##########
components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java:
##########
@@ -681,11 +684,31 @@ private static Object lookupResult(
throw new IndexOutOfBoundsException(
"Key: " + key + " not found in bean: " + bean + " of type:
"
+
ObjectHelper.classCanonicalName(bean) + " using OGNL path [" + ognlPath
- + "]");
+ + "]" + keyHint(result != null
? result : bean, key));
} else {
// null safe so we can return null
return null;
}
}
+ /**
+ * What to do when a key is read from a value that has no keys
(CAMEL-25746): the message body as a stream or bytes
+ * (a request body before it is unmarshalled, or a variable set from it),
JSON text, or a list.
+ */
+ static String keyHint(Object value, String key) {
+ if (value instanceof StreamCache || value instanceof InputStream ||
value instanceof byte[]) {
+ return " (the value is the message body as a stream, not a Map:
unmarshal it first, for example with json,"
+ + " then read the key; a variable keeps the value it had
when it was set)";
+ }
+ if (value instanceof CharSequence text) {
+ String trimmed = text.toString().strip();
+ if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
+ return " (the value is JSON text, not a Map: unmarshal it
first, for example with json, then read the key)";
+ }
+ }
+ if (value instanceof Collection || value instanceof Object[]) {
+ return " (a List has no keys: pick an element first, such as [0]["
+ key + "], or split the list)";
Review Comment:
Agreed. The hint now names the type in b962b8f18b81: "a List", "an array"
(the suggestion would print "a array") or "a Collection", and it ends with "or
split it". `[0]` works for all three, because the lookup converts the value to
a List first. There is a new test for an array, which also checks that
`[0][qty]` reads the value.
_Claude Code on behalf of davsclaus_
--
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]