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
This commit is contained in:
Stas Burnashev authored and GitHub committed 2026-04-21 07:20:57 -05:00
1 parent 4605d7d38f
commit 59c1d727fc
2 files changed
+41 -3

No files matched your search

@@ -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();