From 59c1d727fc7ca04ba3c23ce76865927d12ec712c Mon Sep 17 00:00:00 2001 From: Stas Burnashev <7887482+stashev@users.noreply.github.com> Date: Tue, 21 Apr 2026 14:20:57 +0200 Subject: [PATCH] fix(dynamodb): return attributes for UPDATED_NEW/UPDATED_OLD on new items (#538) * fix(dynamodb): return attributes for UPDATED_NEW/UPDATED_OLD on new items When UpdateItem creates a new item, oldItem is null. The UPDATED_NEW branch required oldItem != null, so no attributes were returned. Handle null oldItem by returning all newItem attributes (everything is new). Same fix for UPDATED_OLD with null newItem. Closes #537 * fix: exclude key attributes from UPDATED_NEW, remove dead UPDATED_OLD branch - Diff newItem against the request key when oldItem is null, so key attributes are excluded from UPDATED_NEW (matches AWS behavior) - Remove unreachable UPDATED_OLD else branch (newItem is never null) - Assert key attributes are absent in the new-item test --- .../dynamodb/DynamoDbJsonHandler.java | 10 ++++-- .../dynamodb/DynamoDbJsonHandlerTest.java | 34 +++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java index 8a90e5d9ee..35a569d70f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java @@ -294,9 +294,13 @@ public class DynamoDbJsonHandler { response.set("Attributes", result.newItem()); } else if ("ALL_OLD" .equals(returnValues) && result.oldItem() != null) { response.set("Attributes", result.oldItem()); - } else if ("UPDATED_NEW".equals(returnValues) && result.oldItem() != null && result.newItem() != null) { - response.set("Attributes", getChangedAttributes(result.newItem(), result.oldItem())); - } else if ("UPDATED_OLD".equals(returnValues) && result.oldItem() != null && result.newItem() != null) { + } else if ("UPDATED_NEW".equals(returnValues) && result.newItem() != null) { + // When oldItem is null (new item created), diff against the key so key + // attributes are excluded - matching AWS behavior where UPDATED_NEW + // returns only the attributes set by the expression. + JsonNode baseline = result.oldItem() != null ? result.oldItem() : key; + response.set("Attributes", getChangedAttributes(result.newItem(), baseline)); + } else if ("UPDATED_OLD".equals(returnValues) && result.oldItem() != null) { response.set("Attributes", getChangedAttributes(result.oldItem(), result.newItem())); } addConsumedCapacity(response, request, tableName, 1, true); diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java index e909c8d33f..790da6f160 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java @@ -108,6 +108,40 @@ class DynamoDbJsonHandlerTest { assertFalse(attr.has("sameAttr"), "Attributes should not have sameAttr"); } + @Test + void updateItemReturnValuesUpdatedNewOnNewItem() throws Exception { + createUsersTable(); + + // Item does not exist - UpdateItem creates it + ObjectNode key = item("userId", "u-new"); + + ObjectNode exprValues = mapper.createObjectNode(); + ObjectNode startVal = mapper.createObjectNode(); + startVal.put("N", "60000000"); + ObjectNode incVal = mapper.createObjectNode(); + incVal.put("N", "1"); + exprValues.set(":start", startVal); + exprValues.set(":inc", incVal); + + JsonNode request = createRequest("Users", key, + "SET counter = if_not_exists(counter, :start) + :inc", + null, exprValues, "UPDATED_NEW"); + + Response response = handler.handle("UpdateItem", request, "us-east-1"); + assertNotNull(response); + + JsonNode responseData = mapper.convertValue(response.getEntity(), JsonNode.class); + + assertNotNull(responseData); + assertTrue(responseData.has("Attributes"), "Attributes must be present when item is newly created"); + JsonNode attr = responseData.get("Attributes"); + + assertTrue(attr.has("counter"), "Attributes should have counter"); + assertEquals("60000001", attr.get("counter").get("N").asText()); + + assertFalse(attr.has("userId"), "UPDATED_NEW should not include key attributes"); + } + @Test void updateItemReturnValuesUpdatedOld() throws Exception { createUsersTable();