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

@@ -463,6 +463,9 @@ public class DynamoDbJsonHandler {
}
JsonNode updateData = attributeUpdates.isMissingNode() ? null : attributeUpdates;
JsonNode expectedUpd = request.has("Expected") ? request.get("Expected") : null;
String conditionalOperatorUpd = request.has("ConditionalOperator")
? request.get("ConditionalOperator").asText() : "AND";
if (updateData != null && updateExpression != null) {
throw new AwsException("ValidationException",
@@ -470,6 +473,12 @@ public class DynamoDbJsonHandler {
+ "Non-expression parameters: {AttributeUpdates} Expression parameters: {UpdateExpression}", 400);
}
if (conditionExpression != null && expectedUpd != null) {
throw new AwsException("ValidationException",
"Can not use both expression and non-expression parameters in the same request: "
+ "Non-expression parameters: {Expected} Expression parameters: {ConditionExpression}", 400);
}
// Validate EAN/EAV usage across UpdateExpression + ConditionExpression
if (updateExpression != null) {
// Pre-validate syntax: expression must start with a valid clause keyword
@@ -499,6 +508,11 @@ public class DynamoDbJsonHandler {
checkUnusedEav(exprAttrValues, colonTokensAll);
}
if (expectedUpd != null) {
JsonNode existingForUpd = dynamoDbService.getItem(tableName, key, region);
evaluateLegacyExpected(existingForUpd, expectedUpd, conditionalOperatorUpd, returnValuesOnConditionCheckFailure);
}
DynamoDbService.UpdateResult result = dynamoDbService.updateItem(
tableName, key, updateData, updateExpression, exprAttrNames, exprAttrValues,
returnValues, conditionExpression, region, returnValuesOnConditionCheckFailure);
@@ -538,7 +552,7 @@ public class DynamoDbJsonHandler {
boolean exists = condition.get("Exists").asBoolean();
condResult = exists ? attrValue != null : attrValue == null;
} else {
condResult = dynamoDbService.matchesKeyConditionPublic(attrValue, condition);
condResult = dynamoDbService.matchesKeyConditionPublic(attrValue, normalizeLegacyCondition(condition));
}
if (useOr) overall = overall || condResult;
else overall = overall && condResult;
@@ -552,6 +566,27 @@ public class DynamoDbJsonHandler {
}
}
/**
* Converts a legacy Expected condition from the {"Value": {...}} form to the
* {"AttributeValueList": [{...}]} form expected by matchesKeyCondition.
* Operators that already use AttributeValueList (IN, BETWEEN) pass through unchanged.
*/
private JsonNode normalizeLegacyCondition(JsonNode condition) {
if (!condition.has("Value") || condition.has("AttributeValueList")) {
return condition;
}
com.fasterxml.jackson.databind.node.ObjectNode normalized = objectMapper.createObjectNode();
condition.fields().forEachRemaining(e -> {
if (!"Value".equals(e.getKey())) {
normalized.set(e.getKey(), e.getValue());
}
});
com.fasterxml.jackson.databind.node.ArrayNode avl = objectMapper.createArrayNode();
avl.add(condition.get("Value"));
normalized.set("AttributeValueList", avl);
return normalized;
}
private void addItemCollectionMetrics(ObjectNode response, JsonNode request, String tableName,
JsonNode itemOrKey, String region) {
String ricm = request.has("ReturnItemCollectionMetrics")