Skip to content

Commit b8495d2

Browse files
committed
[Fix #786] Evaluate embedded properties
Signed-off-by: fjtirado <[email protected]>
1 parent c868d49 commit b8495d2

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

impl/core/src/main/java/io/serverlessworkflow/impl/expressions/ObjectExpressionFactory.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ protected Object toJavaObject(Object eval) {
5353
private Map<String, Object> buildExpressionMap(
5454
Map<String, Object> origMap, ExpressionFactory factory) {
5555
return new ProxyMap(
56-
origMap, o -> ExpressionUtils.isExpr(o) ? buildExpression(o.toString()) : o);
56+
origMap,
57+
o ->
58+
ExpressionUtils.isExpr(o)
59+
? buildExpression(o.toString())
60+
: buildExpressionObject(o, factory));
5761
}
5862

5963
private Object buildExpressionObject(Object obj, ExpressionFactory factory) {
@@ -64,9 +68,14 @@ private Map<String, Object> evaluateExpressionMap(
6468
Map<String, Object> origMap, WorkflowContext workflow, TaskContext task, WorkflowModel n) {
6569
return new ProxyMap(
6670
origMap,
67-
o ->
68-
o instanceof ObjectExpression
69-
? toJavaObject(((ObjectExpression) o).eval(workflow, task, n))
70-
: o);
71+
o -> {
72+
if (o instanceof ObjectExpression expr) {
73+
return toJavaObject(expr.eval(workflow, task, n));
74+
} else if (o instanceof Map map) {
75+
return evaluateExpressionMap(map, workflow, task, n);
76+
} else {
77+
return o;
78+
}
79+
});
7180
}
7281
}

impl/jackson/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,17 @@
2828
<groupId>net.thisptr</groupId>
2929
<artifactId>jackson-jq</artifactId>
3030
</dependency>
31+
<dependency>
32+
<groupId>org.junit.jupiter</groupId>
33+
<artifactId>junit-jupiter-engine</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.assertj</groupId>
37+
<artifactId>assertj-core</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.mockito</groupId>
41+
<artifactId>mockito-core</artifactId>
42+
</dependency>
3143
</dependencies>
3244
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.expressions.jq;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import io.serverlessworkflow.impl.WorkflowContext;
21+
import io.serverlessworkflow.impl.WorkflowValueResolver;
22+
import io.serverlessworkflow.impl.expressions.ExpressionDescriptor;
23+
import io.serverlessworkflow.impl.jackson.JsonUtils;
24+
import java.util.Map;
25+
import org.junit.jupiter.api.Test;
26+
import org.mockito.Mockito;
27+
28+
class JQExpressionFactoryTest {
29+
30+
@Test
31+
void testNestedMap() {
32+
WorkflowContext workflowContext = Mockito.mock(WorkflowContext.class);
33+
JQExpressionFactory factory = new JQExpressionFactory();
34+
WorkflowValueResolver<Map<String, Object>> expr =
35+
factory.resolveMap(
36+
ExpressionDescriptor.object(
37+
Map.of(
38+
"name",
39+
"${.name}",
40+
"surname",
41+
"Doe",
42+
"nested",
43+
Map.of("name", "${.name}", "surname", "Doe"))));
44+
Map<String, Object> result =
45+
expr.apply(
46+
workflowContext,
47+
null,
48+
new JacksonModel(JsonUtils.mapper().createObjectNode().put("name", "John")));
49+
assertThat(result.get("name")).isEqualTo("John");
50+
assertThat(result.get("surname")).isEqualTo("Doe");
51+
Map<String, Object> nested = (Map<String, Object>) result.get("nested");
52+
assertThat(result.get("name")).isEqualTo("John");
53+
assertThat(result.get("surname")).isEqualTo("Doe");
54+
}
55+
}

0 commit comments

Comments
 (0)