fix(dynamodb): honor legacy Expected condition in UpdateItem (#888)

UpdateItem was silently ignoring the legacy `Expected` parameter sent
by AWS SDK v1 clients, causing conditional writes to always succeed
regardless of the condition.

Two bugs fixed:
- `handleUpdateItem` never read the `Expected` field from the request
- `evaluateLegacyExpected` passed conditions directly to
  `matchesKeyConditionPublic`, which reads `AttributeValueList`, but
  the legacy wire format uses the singular `Value` field instead

The fix reads `Expected` and `ConditionalOperator` in `handleUpdateItem`,
validates mutual exclusion with `ConditionExpression`, fetches the
existing item before the update, and normalizes `{"Value": x}` →
`{"AttributeValueList": [x]}` before delegating to the condition
evaluator.

Closes #848
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-05-17 23:36:36 -05:00
1 parent becbbea5f9
commit 647b170494
3 files changed
+348 -1

No files matched your search

@@ -12,6 +12,7 @@ import software.amazon.awssdk.services.dynamodb.model.BillingMode;
import software.amazon.awssdk.services.dynamodb.model.ComparisonOperator;
import software.amazon.awssdk.services.dynamodb.model.Condition;
import software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException;
import software.amazon.awssdk.services.dynamodb.model.ExpectedAttributeValue;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
import software.amazon.awssdk.services.dynamodb.model.GetItemResponse;
@@ -572,6 +573,83 @@ class DynamoDbConformanceChangesTest {
assertThat(resp.items().get(0).get("name").s()).isEqualTo("Item-2");
}
// -----------------------------------------------------------------------
// Phase 10 — Legacy API: Expected (issue #848)
// -----------------------------------------------------------------------
@Test @Order(85)
@SuppressWarnings("deprecation")
void updateItemLegacyExpectedAttributeMissing() {
// Item does not exist; expecting a specific attribute value should fail
assertThatThrownBy(() -> ddb.updateItem(UpdateItemRequest.builder()
.tableName(TABLE)
.key(Map.of("pk", av("legacy-exp-missing"), "sk", av("s")))
.attributeUpdates(Map.of(
"val", AttributeValueUpdate.builder().value(av("updated")).action(AttributeAction.PUT).build()))
.expected(Map.of("val", ExpectedAttributeValue.builder()
.comparisonOperator(ComparisonOperator.EQ)
.attributeValueList(av("original"))
.build()))
.build()))
.isInstanceOf(ConditionalCheckFailedException.class);
}
@Test @Order(86)
@SuppressWarnings("deprecation")
void updateItemLegacyExpectedValueMismatch() {
ddb.putItem(r -> r.tableName(TABLE).item(Map.of(
"pk", av("legacy-exp-mismatch"), "sk", av("s"),
"count", AttributeValue.builder().n("2").build())));
assertThatThrownBy(() -> ddb.updateItem(UpdateItemRequest.builder()
.tableName(TABLE)
.key(Map.of("pk", av("legacy-exp-mismatch"), "sk", av("s")))
.attributeUpdates(Map.of(
"count", AttributeValueUpdate.builder()
.value(AttributeValue.builder().n("99").build())
.action(AttributeAction.PUT)
.build()))
.expected(Map.of("count", ExpectedAttributeValue.builder()
.comparisonOperator(ComparisonOperator.EQ)
.attributeValueList(AttributeValue.builder().n("1").build())
.build()))
.build()))
.isInstanceOf(ConditionalCheckFailedException.class);
// Verify item was not updated
GetItemResponse resp = ddb.getItem(r -> r
.tableName(TABLE)
.key(Map.of("pk", av("legacy-exp-mismatch"), "sk", av("s"))));
assertThat(resp.item().get("count").n()).isEqualTo("2");
}
@Test @Order(87)
@SuppressWarnings("deprecation")
void updateItemLegacyExpectedMatchSucceeds() {
ddb.putItem(r -> r.tableName(TABLE).item(Map.of(
"pk", av("legacy-exp-match"), "sk", av("s"),
"count", AttributeValue.builder().n("1").build())));
ddb.updateItem(UpdateItemRequest.builder()
.tableName(TABLE)
.key(Map.of("pk", av("legacy-exp-match"), "sk", av("s")))
.attributeUpdates(Map.of(
"count", AttributeValueUpdate.builder()
.value(AttributeValue.builder().n("99").build())
.action(AttributeAction.PUT)
.build()))
.expected(Map.of("count", ExpectedAttributeValue.builder()
.comparisonOperator(ComparisonOperator.EQ)
.attributeValueList(AttributeValue.builder().n("1").build())
.build()))
.build());
GetItemResponse resp = ddb.getItem(r -> r
.tableName(TABLE)
.key(Map.of("pk", av("legacy-exp-match"), "sk", av("s"))));
assertThat(resp.item().get("count").n()).isEqualTo("99");
}
// -----------------------------------------------------------------------
// Phase 11 — Enum validation fires before table lookup
// -----------------------------------------------------------------------