Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
from localstack.services.events.utils import to_json_str

_PATTERN_SINGLETON_ARRAY_ACCESS_OUTPUT: Final[str] = r"\[\d+\]$"
_PATTERN_SLICE_OR_WILDCARD_ACCESS = r"\$(?:\.[^[]+\[(?:\*|\d*:\d*)\]|\[\*\])(?:\.[^[]+)*$"


def _is_singleton_array_access(path: str) -> bool:
# Returns true if the json path terminates with a literal singleton array access.
return bool(re.search(_PATTERN_SINGLETON_ARRAY_ACCESS_OUTPUT, path))


def _contains_slice_or_wildcard_array(path: str) -> bool:
# Returns true if the json path contains a slice or wildcard in the array.
# Slices at the root are discarded, but wildcard at the root is allowed.
return bool(re.search(_PATTERN_SLICE_OR_WILDCARD_ACCESS, path))


class NoSuchJsonPathError(Exception):
json_path: Final[str]
data: Final[Any]
Expand Down Expand Up @@ -42,6 +49,8 @@ def extract_json(path: str, data: Any) -> Any:

matches = input_expr.find(data)
if not matches:
if _contains_slice_or_wildcard_array(path):
return []
raise NoSuchJsonPathError(json_path=path, data=data)

if len(matches) > 1 or isinstance(matches[0].path, Index):
Expand Down
42 changes: 42 additions & 0 deletions tests/aws/services/stepfunctions/v2/base/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,45 @@ def test_json_path_array_access(
definition,
exec_input,
)

# These json_path_strings are handled gracefully in AWS by returning an empty array,
# although there are some exceptions like "$[1:5]", "$[1:], "$[:1]
@markers.aws.validated
@pytest.mark.parametrize(
"json_path_string",
[
"$[*]",
"$.items[*]",
"$.items[1:]",
"$.items[:1]",
"$.item.items[*]",
"$.item.items[1:]",
"$.item.items[:1]",
"$.item.items[1:5]",
"$.items[*].itemValue",
"$.items[1:].itemValue",
"$.items[:1].itemValue",
"$.item.items[1:5].itemValue",
],
)
def test_json_path_array_wildcard_or_slice_with_no_input(
self,
aws_client,
create_state_machine_iam_role,
create_state_machine,
sfn_snapshot,
json_path_string,
):
template = BaseTemplate.load_sfn_template(BaseTemplate.JSON_PATH_ARRAY_ACCESS)
template["States"]["EntryState"]["Parameters"]["item.$"] = json_path_string
definition = json.dumps(template)

exec_input = json.dumps({})
create_and_record_execution(
aws_client,
create_state_machine_iam_role,
create_state_machine,
sfn_snapshot,
definition,
exec_input,
)
Loading