Skip to content

Commit

Permalink
Add additional safeguards to list and dictionary parameter types (#2127)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiersten-stokes authored and lresende committed Sep 14, 2021
1 parent 49ec33c commit 08b2cf6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
14 changes: 13 additions & 1 deletion elyra/pipeline/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,14 @@ def _process_dictionary_value(self, value: str) -> Union[Dict, str]:
JSON should be converted to the appropriate Dict format, if possible. If a Dict cannot be formed,
log and return stripped string value.
"""
converted_dict = None
if not value:
return {}

value = value.strip()
if value == "None":
return {}

converted_dict = None
if value.startswith('{') and value.endswith('}'):
try:
converted_dict = ast.literal_eval(value)
Expand All @@ -437,7 +443,13 @@ def _process_list_value(self, value: str) -> Union[List, str]:
should be converted to the appropriate List format, if possible. If a List cannot be formed,
log and return stripped string value.
"""
if not value:
return []

value = value.strip()
if value == "None":
return []

converted_list = None
if value.startswith('[') and value.endswith(']'):
try:
Expand Down
8 changes: 6 additions & 2 deletions elyra/pipeline/tests/test_processor_airflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,10 @@ def test_unique_operation_name_non_existent(processor):

def test_process_list_value_function(processor):
# Test values that will be successfully converted to list
assert processor._process_list_value("") == []
assert processor._process_list_value(None) == []
assert processor._process_list_value("[]") == []
assert processor._process_list_value("None") == []
assert processor._process_list_value("['elem1']") == ["elem1"]
assert processor._process_list_value("['elem1', 'elem2', 'elem3']") == ["elem1", "elem2", "elem3"]
assert processor._process_list_value(" ['elem1', 'elem2' , 'elem3'] ") == ["elem1", "elem2", "elem3"]
Expand All @@ -387,7 +390,6 @@ def test_process_list_value_function(processor):

# Test values that will not be successfully converted to list
# Surrounding quotes are added to string values for correct DAG render
assert processor._process_list_value("") == "\"\""
assert processor._process_list_value("[[]") == "\"[[]\""
assert processor._process_list_value("[elem1, elem2]") == "\"[elem1, elem2]\""
assert processor._process_list_value("elem1, elem2") == "\"elem1, elem2\""
Expand All @@ -397,7 +399,10 @@ def test_process_list_value_function(processor):

def test_process_dictionary_value_function(processor):
# Test values that will be successfully converted to dictionary
assert processor._process_dictionary_value("") == {}
assert processor._process_dictionary_value(None) == {}
assert processor._process_dictionary_value("{}") == {}
assert processor._process_dictionary_value("None") == {}
assert processor._process_dictionary_value("{'key': 'value'}") == {"key": "value"}

dict_as_str = "{'key1': 'value', 'key2': 'value'}"
Expand Down Expand Up @@ -432,7 +437,6 @@ def test_process_dictionary_value_function(processor):

# Test values that will not be successfully converted to dictionary
# Surrounding quotes are added to string values for correct DAG render
assert processor._process_dictionary_value("") == "\"\""
assert processor._process_dictionary_value("{{}") == "\"{{}\""
assert processor._process_dictionary_value("{key1: value, key2: value}") == "\"{key1: value, key2: value}\""
assert processor._process_dictionary_value(" { key1: value, key2: value } ") == "\"{ key1: value, key2: value }\""
Expand Down
8 changes: 6 additions & 2 deletions elyra/pipeline/tests/test_processor_kfp.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ def test_collect_envs(processor):

def test_process_list_value_function(processor):
# Test values that will be successfully converted to list
assert processor._process_list_value("") == []
assert processor._process_list_value(None) == []
assert processor._process_list_value("[]") == []
assert processor._process_list_value("None") == []
assert processor._process_list_value("['elem1']") == ["elem1"]
assert processor._process_list_value("['elem1', 'elem2', 'elem3']") == ["elem1", "elem2", "elem3"]
assert processor._process_list_value(" ['elem1', 'elem2' , 'elem3'] ") == ["elem1", "elem2", "elem3"]
Expand All @@ -204,7 +207,6 @@ def test_process_list_value_function(processor):
assert processor._process_list_value("[{'obj': 'val', 'obj2': 'val2'}, {}]") == [{'obj': 'val', 'obj2': 'val2'}, {}]

# Test values that will not be successfully converted to list
assert processor._process_list_value("") == ""
assert processor._process_list_value("[[]") == "[[]"
assert processor._process_list_value("[elem1, elem2]") == "[elem1, elem2]"
assert processor._process_list_value("elem1, elem2") == "elem1, elem2"
Expand All @@ -214,7 +216,10 @@ def test_process_list_value_function(processor):

def test_process_dictionary_value_function(processor):
# Test values that will be successfully converted to dictionary
assert processor._process_dictionary_value("") == {}
assert processor._process_dictionary_value(None) == {}
assert processor._process_dictionary_value("{}") == {}
assert processor._process_dictionary_value("None") == {}
assert processor._process_dictionary_value("{'key': 'value'}") == {"key": "value"}

dict_as_str = "{'key1': 'value', 'key2': 'value'}"
Expand Down Expand Up @@ -248,7 +253,6 @@ def test_process_dictionary_value_function(processor):
assert processor._process_dictionary_value(dict_as_str) == expected_value

# Test values that will not be successfully converted to dictionary
assert processor._process_dictionary_value("") == ""
assert processor._process_dictionary_value("{{}") == "{{}"
assert processor._process_dictionary_value("{key1: value, key2: value}") == "{key1: value, key2: value}"
assert processor._process_dictionary_value(" { key1: value, key2: value } ") == "{ key1: value, key2: value }"
Expand Down

0 comments on commit 08b2cf6

Please sign in to comment.