Skip to content

Commit

Permalink
fix: handling of linebreaks, quotes, and empty string in workflow edi…
Browse files Browse the repository at this point in the history
…tor args input
  • Loading branch information
danielgrittner committed Oct 8, 2024
1 parent 6437140 commit 759b459
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
16 changes: 13 additions & 3 deletions admyral/editor/json_with_references_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""


Expand Down Expand Up @@ -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)
Expand All @@ -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 == "":
Expand Down
59 changes: 57 additions & 2 deletions tests/editor/test_json_with_references_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]'


#########################################################################################################
Expand All @@ -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"]'


#########################################################################################################
Expand All @@ -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"

0 comments on commit 759b459

Please sign in to comment.