Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(ddb): parity tests with aws (#1258)

Nahuel Nuceraebe99fa0e9df
diff --git a/ministack/services/dynamodb.py b/ministack/services/dynamodb.py
index acd44895..b5186443 100644
--- a/ministack/services/dynamodb.py
+++ b/ministack/services/dynamodb.py
@@ -6194,8 +6194,10 @@ def _project_item(item, proj_expr, attr_names):
result[root_name] = _merge_projection(result[root_name], sub)
else:
result[root_name] = sub
- # Compact sparse lists: remove None placeholders and collapse to dense lists.
- return _compact_projection(result)
+ # Compact sparse lists per attribute: `result` maps attribute names to
+ # AttributeValues, so compaction runs on each value (never on the outer
+ # map, whose keys are attribute names, not type tags).
+ return {name: _compact_projection(av) for name, av in result.items()}
def _compact_projection(value):
diff --git a/tests/test_dynamodb.py b/tests/test_dynamodb.py
index a49d1d5e..3275e775 100644
--- a/tests/test_dynamodb.py
+++ b/tests/test_dynamodb.py
@@ -4490,6 +4490,30 @@ def test_dynamodb_projection_multiple_sibling_paths(ddb):
ddb.delete_table(TableName=name)
+def test_dynamodb_projection_list_index_with_sibling_attribute(ddb):
+ """A list-index projection alongside another attribute: the sparse-list
+ placeholders must still be compacted when the result has >1 attribute
+ (regression — compaction previously only ran, incorrectly, on a
+ single-key result and silently left None placeholders)."""
+ name = "pe-list-sib"
+ _proj_table(ddb, name)
+ try:
+ ddb.put_item(TableName=name, Item={
+ "pk": {"S": "a"},
+ "items": {"L": [{"S": "first"}, {"S": "second"}, {"S": "third"}]},
+ })
+ r = ddb.get_item(
+ TableName=name, Key={"pk": {"S": "a"}},
+ ProjectionExpression="pk, items[2]",
+ )
+ assert r["Item"] == {
+ "pk": {"S": "a"},
+ "items": {"L": [{"S": "third"}]},
+ }
+ finally:
+ ddb.delete_table(TableName=name)
+
+
# ---------------------------------------------------------------------------
# ReturnValues + ReturnItemCollectionMetrics enum + shape validation.
# ---------------------------------------------------------------------------