fix(dynamodb): apply the ProjectionExpression of a TransactGetItems Get (#4061)

The projection was validated and then ignored, so every Get returned the whole
item. A projection selecting no attribute leaves the entry without an Item,
where GetItem and BatchGetItem both keep an empty one.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-09-21 08:43:35 -04:00
1 parent c62db2de1e
commit 138f3c2650
2 files changed
+76 -1

No files matched your search

@@ -1825,7 +1825,7 @@ public class DynamoDbService implements ResourceProvider {
String tableName = get.path("TableName").asText();
JsonNode key = get.get("Key");
try {
results.add(getItem(tableName, key, region));
results.add(projectTransactGet(getItem(tableName, key, region), get));
cancelReasons.add(new TransactionCanceledException.CancellationReason("", null));
} catch (AwsException e) {
if ("ValidationException".equals(e.getErrorCode())) {
@@ -1849,6 +1849,20 @@ public class DynamoDbService implements ResourceProvider {
return results;
}
/**
* GetItem and BatchGetItem keep an empty Item when the projection matches nothing.
* TransactGetItems omits it.
*/
private JsonNode projectTransactGet(JsonNode item, JsonNode get) {
String projectionExpression = get.path("ProjectionExpression").textValue();
if (item == null || projectionExpression == null) {
return item;
}
ObjectNode projected = ProjectionEvaluator.project(item, projectionExpression,
get.has("ExpressionAttributeNames") ? get.get("ExpressionAttributeNames") : null);
return projected.isEmpty() ? null : projected;
}
// --- UpdateTable ---
public TableDefinition updateTable(String tableName, Long readCapacity, Long writeCapacity, String region) {
@@ -1818,6 +1818,67 @@ class DynamoDbJsonHandlerTest {
ex.getMessage());
}
@Test
void transactGetItemsKeepsOnlyTheProjectedAttributes() throws Exception {
seedTransactGetItem();
ObjectNode get = mapper.createObjectNode();
get.put("TableName", "Users");
get.set("Key", item("userId", "u1"));
get.put("ProjectionExpression", "#k");
get.set("ExpressionAttributeNames", mapper.createObjectNode().put("#k", "keep"));
ObjectNode request = mapper.createObjectNode();
request.set("TransactItems", mapper.createArrayNode().add(mapper.createObjectNode().set("Get", get)));
Response response = handler.handle("TransactGetItems", request, "eu-west-1");
JsonNode body = mapper.convertValue(response.getEntity(), JsonNode.class);
JsonNode returned = body.get("Responses").get(0).get("Item");
assertEquals(1, returned.size());
assertEquals("stay", returned.get("keep").get("S").asText());
}
@Test
void transactGetItemsLeavesAMissingKeyWithoutAnItemUnderAProjection() throws Exception {
seedTransactGetItem();
ObjectNode get = mapper.createObjectNode();
get.put("TableName", "Users");
get.set("Key", item("userId", "absent"));
get.put("ProjectionExpression", "keep");
ObjectNode request = mapper.createObjectNode();
request.set("TransactItems", mapper.createArrayNode().add(mapper.createObjectNode().set("Get", get)));
Response response = handler.handle("TransactGetItems", request, "eu-west-1");
JsonNode body = mapper.convertValue(response.getEntity(), JsonNode.class);
assertEquals(200, response.getStatus());
assertFalse(body.get("Responses").get(0).has("Item"));
}
@Test
void transactGetItemsOmitsItemWhenTheProjectionMatchesNothing() throws Exception {
seedTransactGetItem();
ObjectNode get = mapper.createObjectNode();
get.put("TableName", "Users");
get.set("Key", item("userId", "u1"));
get.put("ProjectionExpression", "#x");
get.set("ExpressionAttributeNames", mapper.createObjectNode().put("#x", "doesNotExist"));
ObjectNode request = mapper.createObjectNode();
request.set("TransactItems", mapper.createArrayNode().add(mapper.createObjectNode().set("Get", get)));
Response response = handler.handle("TransactGetItems", request, "eu-west-1");
JsonNode body = mapper.convertValue(response.getEntity(), JsonNode.class);
assertFalse(body.get("Responses").get(0).has("Item"));
}
private void seedTransactGetItem() throws Exception {
createUsersTable("eu-west-1");
ObjectNode putRequest = mapper.createObjectNode();
putRequest.put("TableName", "Users");
putRequest.set("Item", item("userId", "u1", "keep", "stay"));
handler.handle("PutItem", putRequest, "eu-west-1");
}
@Test
void executeStatementRejectsATooDeepParameter() {
createUsersTable("eu-west-1");