Claus Ibsen created CAMEL-25940:
-----------------------------------
Summary: camel-jsonpath - a NullPointerException when resultType
is java.util.Map and the path gives one value that is not a map
Key: CAMEL-25940
URL: https://issues.apache.org/jira/browse/CAMEL-25940
Project: Camel
Issue Type: Bug
Components: camel-jsonpath
Reporter: Claus Ibsen
Fix For: 4.24.0
A jsonpath expression whose path gives a list of values that are not maps, with
{{resultType}} set to {{java.util.Map}}, fails with a {{NullPointerException}}
instead of a conversion result or a clear error.
*Reproduce*
Body:
{code}
[{"sku":"X","qty":42},{"sku":"Y","qty":0}]
{code}
Expression {{$[?(@.sku == 'X')].qty}} with {{resultType: java.util.Map}}:
{code}
org.apache.camel.ExpressionEvaluationException: java.lang.NullPointerException
Caused by: java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:220)
at java.util.Optional.of(Optional.java:113)
at java.util.stream.FindOps$FindSink$OfRef.get(FindOps.java:194)
{code}
*Cause*
{{JsonPathExpression.evaluate}} (camel-jsonpath) converts each element of a
list result to the result type. Integer to Map has no converter, so the element
becomes null. The list of one element is then unwrapped with
{{col.stream().findFirst().get()}}, and {{findFirst()}} throws an NPE when the
element it picks is null:
{code:java}
} else if (result instanceof Collection<?> col) {
// convert each element in the list
result = col.stream()
.filter(Objects::nonNull) // skip null
.map(item ->
exchange.getContext().getTypeConverter().convertTo(resultType, exchange, item))
.collect(Collectors.toList());
}
if (result instanceof Collection<?> col && col.size() == 1) {
result = col.stream().findFirst().get();
}
{code}
The code came with CAMEL-20508, which aligned the conversion to resultType with
camel-jq.
*Expected*
The unwrap should handle a null element, for example with
{{col.iterator().next()}}, so the result is null, or the conversion that fails
should say which element could not be converted to the result type. Either way,
no NPE.
Found in the review of https://github.com/apache/camel/pull/28210: the AI tools
there point to {{resultType: java.util.Map}} for a filter.
https://github.com/apache/camel/pull/28253 stops giving that advice for a path
that goes on after the filter.
_Claude Code on behalf of davsclaus_
--
This message was sent by Atlassian Jira
(v8.20.10#820010)