From 759a60390ddd7657e27c77ef98b73197a6879461 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 20 Oct 2021 13:35:40 +0000 Subject: [PATCH 1/2] Adding json type to task variables --- camunda/variables/tests/test_variables.py | 8 ++++++++ camunda/variables/variables.py | 19 ++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/camunda/variables/tests/test_variables.py b/camunda/variables/tests/test_variables.py index 8b08577..af23fcd 100644 --- a/camunda/variables/tests/test_variables.py +++ b/camunda/variables/tests/test_variables.py @@ -51,3 +51,11 @@ def test_to_dict_returns_variables_as_dict(self): "var2": {"value": True}, "var3": {"value": "string"}}) self.assertDictEqual({"var1": 1, "var2": True, "var3": "string"}, variables.to_dict()) + + def test_json_returns_variables_as_json(self): + variables = {"list": ["a", "b", "c"], "obj": [{"z":2, "h":3}, {"t":5, "s":9}], "zobj": {"az": [0, 1, 2], "zot": [True, False, False]}} + formatted_vars = Variables.format(variables) + self.assertDictEqual({"list": {"type": "json", "value": "[\"a\", \"b\", \"c\"]"}, + "obj": {"type": "json", "value": "[{\"z\": 2, \"h\": 3}, {\"t\": 5, \"s\": 9}]"}, + "zobj": {"type": "json", "value": "{\"az\": [0, 1, 2], \"zot\": [true, false, false]}"}}, formatted_vars) + diff --git a/camunda/variables/variables.py b/camunda/variables/variables.py index 59e200b..81d96fd 100644 --- a/camunda/variables/variables.py +++ b/camunda/variables/variables.py @@ -25,10 +25,13 @@ def format(cls, variables): """ formatted_vars = {} if variables: - formatted_vars = { - k: v if isinstance(v, dict) else {"value": v} - for k, v in variables.items() - } + for i in variables.keys(): + if type(variables[i]) in [bool, int, float, str]: + formatted_vars[i] = {"value": variables[i]} + elif type(variables[i]) == dict and "value" in variables[i] and type(variables[i]['value']) in [bool, int, float, str]: + formatted_vars[i] = variables[i] + else: + formatted_vars[i] = {"value": json.dumps(variables[i]), "type": "json"} return formatted_vars def to_dict(self): @@ -39,7 +42,13 @@ def to_dict(self): -> {"var1": 1, "var2": True} """ + print("#######################################") + print(self.variables) + print("---------------------------------------") result = {} for k, v in self.variables.items(): - result[k] = v["value"] + if 'type' in v and v['type'] == "Json": + result[k] = json.loads(v["value"]) + else: + result[k] = v["value"] return result From 66117829f8c3b27f12eb480dd65e203d601b9c41 Mon Sep 17 00:00:00 2001 From: Francois Liot Date: Wed, 20 Oct 2021 16:06:50 +0200 Subject: [PATCH 2/2] Removing debug --- camunda/variables/variables.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/camunda/variables/variables.py b/camunda/variables/variables.py index 81d96fd..46ea4d6 100644 --- a/camunda/variables/variables.py +++ b/camunda/variables/variables.py @@ -42,9 +42,6 @@ def to_dict(self): -> {"var1": 1, "var2": True} """ - print("#######################################") - print(self.variables) - print("---------------------------------------") result = {} for k, v in self.variables.items(): if 'type' in v and v['type'] == "Json":