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

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