Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(dynamodb): return attributes for UPDATED_NEW/UPDATED_OLD on new items (#538)

Stas Burnashev59c1d727fc7c
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();