From 759b459facb049834949c84f15ad81238e52b86c Mon Sep 17 00:00:00 2001 From: Daniel Grittner Date: Tue, 8 Oct 2024 10:03:09 +0200 Subject: [PATCH] fix: handling of linebreaks, quotes, and empty string in workflow editor args input --- admyral/editor/json_with_references_serde.py | 16 ++++- .../editor/test_json_with_references_serde.py | 59 ++++++++++++++++++- 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/admyral/editor/json_with_references_serde.py b/admyral/editor/json_with_references_serde.py index 7ca40a5d..65f26ec8 100644 --- a/admyral/editor/json_with_references_serde.py +++ b/admyral/editor/json_with_references_serde.py @@ -27,6 +27,11 @@ - Note if it is a string within a string (e.g. `"... {{ a['b'] }} ..."`), it will not wrap the referenced value in quotes. Example: `"... {{ a['b'] }} ..."` with `a['b'] = "abc"` will be resolved to `"... abc ..."`. + +Note: +- we handle \n and \\n exactly the same way in the UI +- empty UI field equals None + """ @@ -77,10 +82,15 @@ def serialize_json_with_reference(value: JsonValue) -> str: return json.dumps(value) if isinstance(value, str): + if value == "": + return '""' # handle string escaping for ints, floats, bools, dicts, and lists + # if _is_string_escaped_json_value(value): + # return f'"{_escape_string(value)}"' + # return _escape_string(value) if _is_string_escaped_json_value(value): - return f'"{_escape_string(value)}"' - return _escape_string(value) + return f'"{value}"' + return value if isinstance(value, list): content = ", ".join(_handle_value_inside_container(item) for item in value) @@ -97,7 +107,7 @@ def serialize_json_with_reference(value: JsonValue) -> str: def deserialize_json_with_reference(value: str) -> JsonValue: - value = value.strip().strip("\n") + value = value.replace("\\n", "\n") value = _unescape_string(value) if value == "": diff --git a/tests/editor/test_json_with_references_serde.py b/tests/editor/test_json_with_references_serde.py index a287c463..6abbcf38 100644 --- a/tests/editor/test_json_with_references_serde.py +++ b/tests/editor/test_json_with_references_serde.py @@ -296,7 +296,7 @@ def test_invalid_list(): json_str = serialize_json_with_reference(json_obj) assert json_obj == '[\n"]' - assert json_str == '[\\n"]' + assert json_str == '[\n"]' ######################################################################################################### @@ -314,7 +314,7 @@ def test_invalid_list_in_string(): json_str = serialize_json_with_reference(json_obj) assert json_obj == '[\n"]' - assert json_str == '[\\n"]' + assert json_str == '[\n"]' ######################################################################################################### @@ -333,3 +333,58 @@ def test_empty_reference(): assert json_obj == "{{}}" assert json_str == "{{}}" + + +######################################################################################################### + + +def test_linebreak(): + """ + Textfield Input in UI: + ``` + + + ``` + """ + input_value = "\n" + json_obj = deserialize_json_with_reference(input_value) + json_str = serialize_json_with_reference(json_obj) + + assert json_obj == "\n" + assert json_str == "\n" + + +######################################################################################################### + + +def test_escaped_quotes(): + """ + Textfield Input in UI: + ``` + + + ``` + """ + input_value = '""' + json_obj = deserialize_json_with_reference(input_value) + json_str = serialize_json_with_reference(json_obj) + + assert json_obj == "" + assert json_str == '""' + + +######################################################################################################### + + +def test_empty(): + """ + Textfield Input in UI: + ``` + ``` + """ + input_value = "" + json_obj = deserialize_json_with_reference(input_value) + json_str = serialize_json_with_reference(json_obj) + + assert json_obj is None + assert json_str == "null"