Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
Fix Step Functions wildcard path projection (#1217)
Felix Plajereb5a43df16c8
diff --git a/ministack/services/stepfunctions.py b/ministack/services/stepfunctions.pyindex 57736de2..cd55ffbc 100644--- a/ministack/services/stepfunctions.py+++ b/ministack/services/stepfunctions.py@@ -3500,33 +3500,126 @@ def _iter_top_level_chars(text):yield idx, ch+def _parse_jsonpath_tokens(path):+ """Parse the JSONPath subset used by ASL paths."""+ tokens = []+ pos = 1 # Skip the initial "$".++ while pos < len(path):+ if path[pos] == ".":+ pos += 1++ # Ending on "." is invalid.+ if pos >= len(path):+ return None++ # ".*"+ if path[pos] == "*":+ tokens.append(("wildcard", None))+ pos += 1+ continue++ # Bracket notation after "." - next pass will find the bracket.+ if path[pos] == "[":+ continue++ # ".field"+ start = pos+ while pos < len(path) and path[pos] not in ".[":+ pos += 1+ if start == pos:+ return None+ tokens.append(("field", path[start:pos]))+ continue++ # No ".", so must have opening bracket.+ if path[pos] != "[":+ return None+ pos += 1++ # Ending on "[" is invalid.+ if pos >= len(path):+ return None++ # "[*]"+ if path[pos] == "*":+ tokens.append(("wildcard", None))+ pos += 1++ # Array index, eg "[0]"+ elif path[pos].isdigit():+ start = pos+ while pos < len(path) and path[pos].isdigit():+ pos += 1+ tokens.append(("index", int(path[start:pos])))++ # Quoted field name, eg "['field']"+ elif path[pos] in ("'", '"'):+ start = pos+ quote = path[pos]+ pos += 1+ escaped = False+ while pos < len(path):+ if escaped:+ escaped = False+ elif path[pos] == "\\":+ escaped = True+ elif path[pos] == quote:+ break+ pos += 1+ if pos >= len(path):+ return None+ try:+ field = ast.literal_eval(path[start:pos + 1])+ except (SyntaxError, ValueError):+ return None+ if not isinstance(field, str):+ return None+ tokens.append(("field", field))+ pos += 1+ else:+ return None++ # Ensure there's a closing bracket.+ if pos >= len(path) or path[pos] != "]":+ return None+ pos += 1++ return tokens++def _resolve_path(path, data):if path == "$" or not path:return dataif not path.startswith("$"):return data- parts = path[2:].split(".") if path.startswith("$.") else []- cur = data- for part in parts:- if not part:- continue- m = re.match(r"(\w+)\[(\d+)]", part)- if m:- field, idx = m.group(1), int(m.group(2))- if isinstance(cur, dict) and field in cur:- cur = cur[field]- if isinstance(cur, list) and idx < len(cur):- cur = cur[idx]- else:- return None- else:- return None- elif isinstance(cur, dict) and part in cur:- cur = cur[part]- else:- return None- return cur+ tokens = _parse_jsonpath_tokens(path)+ if tokens is None:+ return None++ projected = any(kind == "wildcard" for kind, _ in tokens)+ values = [data]+ for kind, value in tokens:+ resolved = []+ for current in values:+ if kind == "field":+ if isinstance(current, dict) and value in current:+ resolved.append(current[value])+ elif kind == "index":+ if isinstance(current, list) and value < len(current):+ resolved.append(current[value])+ elif isinstance(current, list):+ resolved.append(None)+ elif isinstance(current, list):+ resolved.extend(current)+ elif isinstance(current, dict):+ resolved.extend(current.values())+ values = resolved++ if projected:+ return values+ return values[0] if values else Nonedef _parse_intrinsic_args(s, pos):diff --git a/tests/test_stepfunctions.py b/tests/test_stepfunctions.pyindex 1afa8750..63bbea75 100644--- a/tests/test_stepfunctions.py+++ b/tests/test_stepfunctions.py@@ -4827,6 +4827,120 @@ def test_sfn_intrinsic_functions_batch_2(sfn, sfn_sync):sfn_sync.delete_state_machine(stateMachineArn=sm_arn)+ ("path", "data", "expected"),+ [+ ("$.matrix[0][1]", {"matrix": [[1, 2]]}, 2),+ ("$.matrix[*][*]", {"matrix": [[1, 2], [3]]}, [1, 2, 3]),+ (+ "$.abc.['def ghi'][*]",+ {"abc": {"def ghi": ["first", "second"]}},+ ["first", "second"],+ ),+ ("$.values.*", {"values": {"only": 1}}, [1]),+ (+ "$.items[*].value",+ {"items": [{"value": None}, {}]},+ [None],+ ),+ ("$.matrix[2]", {"matrix": [[1, 2]]}, None),+ ("$.rows[*][0]", {"rows": [[1], [], [2]]}, [1, None, 2]),+ (+ "$.items[*].name",+ {"items": [{"name": "first"}, {}, {"name": "third"}]},+ ["first", "third"],+ ),+ ("$.items[*].missing", {"items": [{"name": "first"}]}, []),+ ],+)+def test_sfn_jsonpath_resolution(path, data, expected):+ from ministack.services.stepfunctions import _resolve_path++ assert _resolve_path(path, data) == expected+++ "path",+ [+ "$.",+ "$.matrix[",+ "$.matrix[*",+ ],+)+def test_sfn_jsonpath_rejects_unsupported_syntax(path):+ from ministack.services.stepfunctions import _parse_jsonpath_tokens++ assert _parse_jsonpath_tokens(path) is None+++def test_sfn_jsonpath_wildcard_projection(sfn_sync):+ """JSONPath wildcards project nested values for parameters and intrinsics."""+ sm_name = f"wildcard-projection-{_uuid_mod.uuid4().hex[:8]}"+ definition = json.dumps({+ "StartAt": "Project",+ "States": {+ "Project": {+ "Type": "Pass",+ "Parameters": {+ "requiresAction.$": (+ "States.ArrayContains("+ "$.results[*].previews[0].requiresAction, true)"+ ),+ "changes.$": "$.results[*].previews[0].changes[*]",+ },+ "End": True,+ },+ },+ })+ sm_arn = sfn_sync.create_state_machine(+ name=sm_name,+ definition=definition,+ roleArn="arn:aws:iam::000000000000:role/sfn-role",+ )["stateMachineArn"]++ result = sfn_sync.start_sync_execution(+ stateMachineArn=sm_arn,+ input=json.dumps({+ "results": [+ {+ "previews": [{+ "requiresAction": False,+ "changes": [{"id": "first"}],+ }],+ },+ {+ "previews": [{+ "requiresAction": True,+ "changes": [{"id": "second"}, {"id": "third"}],+ }],+ },+ ],+ }),+ )+ assert result["status"] == "SUCCEEDED", (+ f"Execution failed: {result.get('error')} — {result.get('cause')}"+ )+ assert json.loads(result["output"]) == {+ "requiresAction": True,+ "changes": [{"id": "first"}, {"id": "second"}, {"id": "third"}],+ }++ empty_result = sfn_sync.start_sync_execution(+ stateMachineArn=sm_arn,+ input=json.dumps({"results": []}),+ )+ assert empty_result["status"] == "SUCCEEDED", (+ f"Execution failed: {empty_result.get('error')} — "+ f"{empty_result.get('cause')}"+ )+ assert json.loads(empty_result["output"]) == {+ "requiresAction": False,+ "changes": [],+ }++ sfn_sync.delete_state_machine(stateMachineArn=sm_arn)++def test_sfn_aws_sdk_error_prefix_catch(sfn, sm):"""aws-sdk errors are prefixed with the service name so Catch blocks match.