Skip to content

Commit de787cd

Browse files
author
Tihomir Surdilovic
authored
Merge pull request #52 from tsurdilo/functionrefparams
update functionref params to jsonnode
2 parents b6ae245 + 6af4522 commit de787cd

File tree

8 files changed

+150
-7
lines changed

8 files changed

+150
-7
lines changed

api/src/main/java/io/serverlessworkflow/api/deserializers/FunctionRefDeserializer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
import java.io.IOException;
3030
import java.util.HashMap;
31-
import java.util.Map;
3231

3332
public class FunctionRefDeserializer extends StdDeserializer<FunctionRef> {
3433

@@ -61,11 +60,12 @@ public FunctionRef deserialize(JsonParser jp,
6160

6261
if (!node.isObject()) {
6362
functionRef.setRefName(node.asText());
64-
functionRef.setParameters(new HashMap<>());
63+
ObjectMapper objectMapper = new ObjectMapper();
64+
functionRef.setParameters(null);
6565
return functionRef;
6666
} else {
6767
if(node.get("parameters") != null) {
68-
functionRef.setParameters(mapper.treeToValue(node.get("parameters"), Map.class));
68+
functionRef.setParameters(mapper.treeToValue(node.get("parameters"), JsonNode.class));
6969
}
7070

7171
if(node.get("refName") != null) {

api/src/main/java/io/serverlessworkflow/api/serializers/FunctionRefSerializer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public void serialize(FunctionRef functionRef,
5454
gen.writeObjectField("parameters", functionRef.getParameters());
5555
}
5656

57-
5857
gen.writeEndObject();
5958
}
6059
}

api/src/main/resources/schema/functions/functionref.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"parameters": {
1111
"type": "object",
1212
"description": "Function parameters",
13-
"existingJavaType": "java.util.Map<String, String>"
13+
"existingJavaType": "com.fasterxml.jackson.databind.JsonNode"
1414
}
1515
},
1616
"required": [

api/src/test/java/io/serverlessworkflow/api/test/MarkupToWorkflowTest.java

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package io.serverlessworkflow.api.test;
1818

19+
import com.fasterxml.jackson.databind.JsonNode;
1920
import io.serverlessworkflow.api.Workflow;
2021
import io.serverlessworkflow.api.actions.Action;
2122
import io.serverlessworkflow.api.defaultdef.DefaultDefinition;
@@ -33,6 +34,7 @@
3334
import org.junit.jupiter.params.provider.ValueSource;
3435

3536
import java.util.List;
37+
import java.util.Map;
3638

3739
import static org.junit.jupiter.api.Assertions.*;
3840

@@ -230,15 +232,15 @@ public void testFunctionRefs(String workflowLocation) {
230232
assertNotNull(action1.getFunctionRef());
231233
FunctionRef functionRef1 = action1.getFunctionRef();
232234
assertEquals("creditCheckFunction", functionRef1.getRefName());
233-
assertEquals(0, functionRef1.getParameters().size());
235+
assertNull(functionRef1.getParameters());
234236

235237
Action action2 = operationState.getActions().get(1);
236238
assertNotNull(action2);
237239
assertNotNull(action2.getFunctionRef());
238240
FunctionRef functionRef2 = action2.getFunctionRef();
239241
assertEquals("sendRejectionEmailFunction", functionRef2.getRefName());
240242
assertEquals(1, functionRef2.getParameters().size());
241-
assertEquals("{{ $.customer }}", functionRef2.getParameters().get("applicant"));
243+
assertEquals("{{ $.customer }}", functionRef2.getParameters().get("applicant").asText());
242244
}
243245

244246
@ParameterizedTest
@@ -283,4 +285,64 @@ public void testSubflowStateRepeat(String workflowLocation) {
283285
assertEquals(1, subflowState.getRepeat().getStopOnEvents().size());
284286
assertEquals("CarTurnedOffEvent", subflowState.getRepeat().getStopOnEvents().get(0));
285287
}
288+
289+
@ParameterizedTest
290+
@ValueSource(strings = {"/features/functionrefjsonparams.json", "/features/functionrefjsonparams.yml"})
291+
public void testFunctionRefJsonParams(String workflowLocation) {
292+
Workflow workflow = Workflow.fromSource(WorkflowTestUtils.readWorkflowFile(workflowLocation));
293+
294+
assertNotNull(workflow);
295+
assertNotNull(workflow.getId());
296+
assertNotNull(workflow.getName());
297+
assertNotNull(workflow.getStates());
298+
299+
assertNotNull(workflow.getStates());
300+
assertTrue(workflow.getStates().size() == 1);
301+
assertTrue(workflow.getStates().get(0) instanceof OperationState);
302+
303+
OperationState operationState = (OperationState) workflow.getStates().get(0);
304+
assertNotNull(operationState.getActions());
305+
assertEquals(1, operationState.getActions().size());
306+
List<Action> actions = operationState.getActions();
307+
assertNotNull(actions.get(0).getFunctionRef());
308+
assertEquals("addPet", actions.get(0).getFunctionRef().getRefName());
309+
310+
JsonNode params = actions.get(0).getFunctionRef().getParameters();
311+
assertNotNull(params);
312+
assertEquals(4, params.size());
313+
assertEquals(123, params.get("id").intValue());
314+
assertEquals("My Address, 123 MyCity, MyCountry", params.get("address").asText());
315+
assertEquals("${ .owner.name }", params.get("owner").asText());
316+
assertEquals("Pluto", params.get("body").get("name").asText());
317+
assertEquals("${ .pet.tagnumber }", params.get("body").get("tag").asText());
318+
}
319+
320+
@ParameterizedTest
321+
@ValueSource(strings = {"/features/functionrefnoparams.json", "/features/functionrefnoparams.yml"})
322+
public void testFunctionRefNoParams(String workflowLocation) {
323+
Workflow workflow = Workflow.fromSource(WorkflowTestUtils.readWorkflowFile(workflowLocation));
324+
325+
assertNotNull(workflow);
326+
assertNotNull(workflow.getId());
327+
assertNotNull(workflow.getName());
328+
assertNotNull(workflow.getStates());
329+
330+
assertNotNull(workflow.getStates());
331+
assertTrue(workflow.getStates().size() == 1);
332+
assertTrue(workflow.getStates().get(0) instanceof OperationState);
333+
334+
OperationState operationState = (OperationState) workflow.getStates().get(0);
335+
assertNotNull(operationState.getActions());
336+
assertEquals(2, operationState.getActions().size());
337+
List<Action> actions = operationState.getActions();
338+
assertNotNull(actions.get(0).getFunctionRef());
339+
assertNotNull(actions.get(1).getFunctionRef());
340+
assertEquals("addPet", actions.get(0).getFunctionRef().getRefName());
341+
assertEquals("addPet", actions.get(1).getFunctionRef().getRefName());
342+
343+
JsonNode params = actions.get(0).getFunctionRef().getParameters();
344+
assertNull(params);
345+
JsonNode params2 = actions.get(1).getFunctionRef().getParameters();
346+
assertNull(params);
347+
}
286348
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"id": "functionrefparams",
3+
"version": "1.0",
4+
"name": "Function Ref Params Test",
5+
"states": [
6+
{
7+
"name": "AddPluto",
8+
"type": "operation",
9+
"start": true,
10+
"actions": [
11+
{
12+
"functionRef": {
13+
"refName": "addPet",
14+
"parameters": {
15+
"body": {
16+
"name": "Pluto",
17+
"tag": "${ .pet.tagnumber }"
18+
},
19+
"id": 123,
20+
"address": "My Address, 123 MyCity, MyCountry",
21+
"owner": "${ .owner.name }"
22+
}
23+
}
24+
}
25+
],
26+
"end": true
27+
}
28+
]
29+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
id: functionrefparams
2+
version: '1.0'
3+
name: Function Ref Params Test
4+
states:
5+
- name: AddPluto
6+
type: operation
7+
start: true
8+
actions:
9+
- functionRef:
10+
refName: addPet
11+
parameters:
12+
body:
13+
name: Pluto
14+
tag: "${ .pet.tagnumber }"
15+
id: 123
16+
address: My Address, 123 MyCity, MyCountry
17+
owner: "${ .owner.name }"
18+
end: true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"id": "functionrefparams",
3+
"version": "1.0",
4+
"name": "Function Ref Params Test",
5+
"states": [
6+
{
7+
"name": "AddPluto",
8+
"type": "operation",
9+
"start": true,
10+
"actions": [
11+
{
12+
"functionRef": {
13+
"refName": "addPet"
14+
}
15+
},
16+
{
17+
"functionRef": "addPet"
18+
}
19+
],
20+
"end": true
21+
}
22+
]
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
id: functionrefparams
2+
version: '1.0'
3+
name: Function Ref Params Test
4+
states:
5+
- name: AddPluto
6+
type: operation
7+
start: true
8+
actions:
9+
- functionRef:
10+
refName: addPet
11+
- functionRef: addPet
12+
end: true

0 commit comments

Comments
 (0)