From 03dfbe6931395ea10e0a67aa9ca0a4aa455f8e67 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Wed, 22 Feb 2023 10:16:54 -0100 Subject: [PATCH] DynamoDB: scan() should respect the Projection-attribute of GSI/LSI's (#5960) --- moto/dynamodb/models/table.py | 6 ++++++ tests/test_dynamodb/test_dynamodb.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/moto/dynamodb/models/table.py b/moto/dynamodb/models/table.py index b520e24f4..1be961410 100644 --- a/moto/dynamodb/models/table.py +++ b/moto/dynamodb/models/table.py @@ -843,6 +843,12 @@ class Table(CloudFormationModel): if passes_all_conditions: results.append(item) + results = copy.deepcopy(results) + if index_name: + index = self.get_index(index_name) + for result in results: + index.project(result) + results, last_evaluated_key = self._trim_results( results, limit, exclusive_start_key, scanned_index=index_name ) diff --git a/tests/test_dynamodb/test_dynamodb.py b/tests/test_dynamodb/test_dynamodb.py index 8f3459ef9..2146184dc 100644 --- a/tests/test_dynamodb/test_dynamodb.py +++ b/tests/test_dynamodb/test_dynamodb.py @@ -4708,6 +4708,17 @@ def test_gsi_projection_type_include(): } ) + # Same when scanning the table + items = table.scan(IndexName="GSI-INC")["Items"] + items[0].should.equal( + { + "gsiK1PartitionKey": "gsi-pk", + "gsiK1SortKey": "gsi-sk", + "partitionKey": "pk-1", + "projectedAttribute": "lore ipsum", + } + ) + @mock_dynamodb def test_lsi_projection_type_keys_only(): @@ -4756,6 +4767,12 @@ def test_lsi_projection_type_keys_only(): {"partitionKey": "pk-1", "sortKey": "sk-1", "lsiK1SortKey": "lsi-sk"} ) + # Same when scanning the table + items = table.scan(IndexName="LSI")["Items"] + items[0].should.equal( + {"lsiK1SortKey": "lsi-sk", "partitionKey": "pk-1", "sortKey": "sk-1"} + ) + @mock_dynamodb @pytest.mark.parametrize(