gmunozfe commented on code in PR #3696:
URL:
https://github.com/apache/incubator-kie-kogito-runtimes/pull/3696#discussion_r1792279659
##########
kogito-serverless-workflow/kogito-jsonpath-expression/src/main/java/org/kie/kogito/expr/jsonpath/JsonPathExpression.java:
##########
@@ -61,24 +63,28 @@ private Configuration getConfiguration(KogitoProcessContext
context) {
.build();
}
+ private static boolean isContextAware(JsonNode context, Map<String,
JsonNode> additionalVars) {
+ return !additionalVars.isEmpty() && context instanceof ObjectNode;
+ }
+
private <T> T eval(JsonNode context, Class<T> returnClass,
KogitoProcessContext processInfo) {
- try (JsonNodeContext jsonNode = JsonNodeContext.from(context,
processInfo)) {
- Configuration jsonPathConfig = getConfiguration(processInfo);
- DocumentContext parsedContext =
JsonPath.using(jsonPathConfig).parse(jsonNode.getNode());
- if (String.class.isAssignableFrom(returnClass)) {
- StringBuilder sb = new StringBuilder();
- // valid json path is $. or $[
- for (String part : expr.split("((?=\\$\\.|\\$\\[))")) {
- JsonNode partResult = parsedContext.read(part,
JsonNode.class);
- sb.append(partResult.isTextual() ? partResult.asText() :
partResult.toPrettyString());
- }
- return (T) sb.toString();
- } else {
- Object result = parsedContext.read(expr);
- return Boolean.class.isAssignableFrom(returnClass) && result
instanceof ArrayNode ? (T) Boolean.valueOf(!((ArrayNode) result).isEmpty())
- :
JsonObjectUtils.convertValue(jsonPathConfig.mappingProvider().map(result,
returnClass, jsonPathConfig), returnClass);
+
+ Configuration jsonPathConfig = getConfiguration(processInfo);
+ DocumentContext parsedContext =
JsonPath.using(jsonPathConfig).parse(context);
+ if (String.class.isAssignableFrom(returnClass)) {
+ StringBuilder sb = new StringBuilder();
+ // valid json path is $. or $[
Review Comment:
In a `Foreach`, do we have to consider that the `inputCollection` may have
different nested levels with eligible variables or just one?
Because if it's possible to have more than one nested level, there are some
edge cases like:
- recursive descent `$..city`, where `city` is at different levels of the
json
- wildcards with recursive descent: `$.orders[*].shippingAddress.city` used
for explicit notation
that won't work with that split regex.
Let me put an example, suppose this data structure:
```json
{
"orders": [
{
"orderNumber": "1234",
"completed": true,
"shippingAddress": {
"street": "Pez 12",
"city": "Madrid",
"postalCode": "28001"
}
},
{
"orderNumber": "5678",
"completed": true,
"shippingAddress": {
"street": "Gato 13",
"city": "Barcelona",
"postalCode": "08005"
}
},
{
"orderNumber": "9910",
"completed": false,
"shippingAddress": {
"street": "Guadalquivir 101",
"city": "Sevilla",
"postalCode": "41007"
}
}
]
}
```
In that example, we could extract into the `outputCollection` those orders
where `completed` is `true` (same as it does in the
[specification](https://github.com/serverlessworkflow/specification/blob/0.8.x/specification.md#foreach-state))
but including the `city`, defined at the inner level.
```json
[
{
"completedorder": {
"orderNumber": "1234",
"completed": true,
"city": "Madrid"
}
},
{
"completedorder": {
"orderNumber": "5678",
"completed": true,
"city": "Barcelona"
}
}
]
```
In the `forEach` to access different nested elements the **_recursive
descent_** operator could be used:
```
$.orders..city
```
or the explicit path for city
`$.orders[*].shippingAddress.city`
I have checked the regex and I think it won't work in any of these edge
cases (recursive neither explicit). In the explicit case, because it's not
designed to split by a simple dot (.) without a preceding $ after encountering
[*], so it won't be able to handle the rest of the path
(`.shippingAddress.city`).
If this has to be supported, perhaps it's worthy to write a complex unit
test, and check if the following regex works correctly for both (recursive
descent and explicit):
`expr.split("((?=\\$\\.|\\$\\[|\\[\\*\\]\\.|\\.\\.|\\.))")`
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]