Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(dynamodb): compare Value when legacy Expected supplies Exists (#2125)

Dmitriy Polomoshnova2b20cc08537
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbConformanceChangesTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbConformanceChangesTest.java
index 33b716f3d3..f9ce394b3b 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbConformanceChangesTest.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbConformanceChangesTest.java
@@ -651,6 +651,124 @@ class DynamoDbConformanceChangesTest {
assertThat(resp.item().get("count").n()).isEqualTo("99");
}
+ // -----------------------------------------------------------------------
+ // Phase 10 — Legacy API: Expected with Exists
+ // -----------------------------------------------------------------------
+
+ @Test @Order(88)
+ @SuppressWarnings("deprecation")
+ void legacyExpectedExistsTrueComparesValue() {
+ // Version-guarded write pattern: Exists:true is paired with the version the caller
+ // last read. Both must be honoured — the attribute must exist AND still equal that value.
+ ddb.putItem(r -> r.tableName(TABLE).item(Map.of(
+ "pk", av("legacy-exp-version"), "sk", av("s"),
+ "version", AttributeValue.builder().n("5").build(),
+ "session", av("original"))));
+
+ Map<String, ExpectedAttributeValue> expectVersion5 = Map.of("version",
+ ExpectedAttributeValue.builder()
+ .exists(true)
+ .value(AttributeValue.builder().n("5").build())
+ .build());
+
+ // The expectation is current → the write lands and bumps the version to 6.
+ ddb.updateItem(UpdateItemRequest.builder()
+ .tableName(TABLE)
+ .key(Map.of("pk", av("legacy-exp-version"), "sk", av("s")))
+ .attributeUpdates(Map.of("version", AttributeValueUpdate.builder()
+ .value(AttributeValue.builder().n("6").build())
+ .action(AttributeAction.PUT)
+ .build()))
+ .expected(expectVersion5)
+ .build());
+
+ // The same expectation is now stale (stored version is 6) → must be rejected.
+ assertThatThrownBy(() -> ddb.updateItem(UpdateItemRequest.builder()
+ .tableName(TABLE)
+ .key(Map.of("pk", av("legacy-exp-version"), "sk", av("s")))
+ .attributeUpdates(Map.of("session", AttributeValueUpdate.builder()
+ .action(AttributeAction.DELETE)
+ .build()))
+ .expected(expectVersion5)
+ .build()))
+ .isInstanceOf(ConditionalCheckFailedException.class);
+
+ // PutItem and DeleteItem route through the same evaluation and must agree.
+ assertThatThrownBy(() -> ddb.putItem(r -> r
+ .tableName(TABLE)
+ .item(Map.of("pk", av("legacy-exp-version"), "sk", av("s"),
+ "version", AttributeValue.builder().n("7").build()))
+ .expected(expectVersion5)))
+ .isInstanceOf(ConditionalCheckFailedException.class);
+
+ assertThatThrownBy(() -> ddb.deleteItem(r -> r
+ .tableName(TABLE)
+ .key(Map.of("pk", av("legacy-exp-version"), "sk", av("s")))
+ .expected(expectVersion5)))
+ .isInstanceOf(ConditionalCheckFailedException.class);
+
+ // None of the stale writes took effect.
+ GetItemResponse resp = ddb.getItem(r -> r
+ .tableName(TABLE)
+ .key(Map.of("pk", av("legacy-exp-version"), "sk", av("s"))));
+ assertThat(resp.item().get("version").n()).isEqualTo("6");
+ assertThat(resp.item().get("session").s()).isEqualTo("original");
+
+ // Exists:false still means "attribute must be absent".
+ assertThatThrownBy(() -> ddb.updateItem(UpdateItemRequest.builder()
+ .tableName(TABLE)
+ .key(Map.of("pk", av("legacy-exp-version"), "sk", av("s")))
+ .attributeUpdates(Map.of("note", AttributeValueUpdate.builder()
+ .value(av("x")).action(AttributeAction.PUT).build()))
+ .expected(Map.of("version", ExpectedAttributeValue.builder().exists(false).build()))
+ .build()))
+ .isInstanceOf(ConditionalCheckFailedException.class);
+ }
+
+ @Test @Order(89)
+ @SuppressWarnings("deprecation")
+ void legacyExpectedRejectsInvalidExistsCombinations() {
+ // Exists:true with no Value — nothing to compare against.
+ DynamoDbException noValue = (DynamoDbException) catchThrowable(() -> ddb.updateItem(UpdateItemRequest.builder()
+ .tableName(TABLE)
+ .key(Map.of("pk", av("legacy-exp-invalid"), "sk", av("s")))
+ .attributeUpdates(Map.of("note", AttributeValueUpdate.builder()
+ .value(av("x")).action(AttributeAction.PUT).build()))
+ .expected(Map.of("version", ExpectedAttributeValue.builder().exists(true).build()))
+ .build()));
+ assertThat(noValue).isNotNull();
+ assertThat(noValue.awsErrorDetails().errorCode()).isEqualTo("ValidationException");
+
+ // Exists:false alongside a Value — contradictory.
+ DynamoDbException falseWithValue = (DynamoDbException) catchThrowable(() -> ddb.updateItem(UpdateItemRequest.builder()
+ .tableName(TABLE)
+ .key(Map.of("pk", av("legacy-exp-invalid"), "sk", av("s")))
+ .attributeUpdates(Map.of("note", AttributeValueUpdate.builder()
+ .value(av("x")).action(AttributeAction.PUT).build()))
+ .expected(Map.of("version", ExpectedAttributeValue.builder()
+ .exists(false)
+ .value(AttributeValue.builder().n("5").build())
+ .build()))
+ .build()));
+ assertThat(falseWithValue).isNotNull();
+ assertThat(falseWithValue.awsErrorDetails().errorCode()).isEqualTo("ValidationException");
+
+ // Exists mixed with the ComparisonOperator form.
+ DynamoDbException mixedForms = (DynamoDbException) catchThrowable(() -> ddb.updateItem(UpdateItemRequest.builder()
+ .tableName(TABLE)
+ .key(Map.of("pk", av("legacy-exp-invalid"), "sk", av("s")))
+ .attributeUpdates(Map.of("note", AttributeValueUpdate.builder()
+ .value(av("x")).action(AttributeAction.PUT).build()))
+ .expected(Map.of("version", ExpectedAttributeValue.builder()
+ .exists(true)
+ .comparisonOperator(ComparisonOperator.EQ)
+ .attributeValueList(AttributeValue.builder().n("5").build())
+ .build()))
+ .build()));
+ assertThat(mixedForms).isNotNull();
+ assertThat(mixedForms.awsErrorDetails().errorCode()).isEqualTo("ValidationException");
+ }
+
// -----------------------------------------------------------------------
// Phase 11 — Enum validation fires before table lookup
// -----------------------------------------------------------------------
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 b9332a6345..8a79f4157c 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
@@ -570,6 +570,7 @@ public class DynamoDbJsonHandler {
private void evaluateLegacyExpected(JsonNode existing, JsonNode expected, String conditionalOperator,
String returnValuesOnConditionCheckFailure) {
if (expected == null) return;
+ validateLegacyExpected(expected);
boolean useOr = "OR".equals(conditionalOperator);
boolean overall = !useOr;
var fields = expected.fields();
@@ -579,10 +580,11 @@ public class DynamoDbJsonHandler {
JsonNode condition = entry.getValue();
JsonNode attrValue = existing != null ? existing.get(attrName) : null;
boolean condResult;
- if (condition.has("Exists")) {
- boolean exists = condition.get("Exists").asBoolean();
- condResult = exists ? attrValue != null : attrValue == null;
+ if (condition.has("Exists") && !condition.get("Exists").asBoolean()) {
+ condResult = attrValue == null;
} else {
+ // Exists:true always carries a Value, so it evaluates as an attribute
+ // comparison exactly like a bare Value: must exist AND match.
condResult = dynamoDbService.matchesKeyConditionPublic(attrValue, normalizeLegacyCondition(condition));
}
if (useOr) overall = overall || condResult;
@@ -597,6 +599,55 @@ public class DynamoDbJsonHandler {
}
}
+ /**
+ * Validates every entry of a legacy Expected map before any condition is evaluated.
+ * The rules and their precedence mirror ExpectedAttributeValue:
+ * <ul>
+ * <li>{@code AttributeValueList} is only meaningful alongside a {@code ComparisonOperator}.</li>
+ * <li>{@code Exists} cannot be combined with a {@code ComparisonOperator} — they are
+ * alternative forms.</li>
+ * <li>Without a {@code ComparisonOperator}, a {@code Value} is required unless
+ * {@code Exists: false} asks for plain absence.</li>
+ * <li>{@code Exists: false} forbids a {@code Value} — an attribute cannot be expected to
+ * hold a value while also being expected to be absent.</li>
+ * </ul>
+ * DynamoDB reports only the first offending entry, so validation stops at the first error.
+ */
+ private void validateLegacyExpected(JsonNode expected) {
+ var fields = expected.fields();
+ while (fields.hasNext()) {
+ var entry = fields.next();
+ String attrName = entry.getKey();
+ JsonNode condition = entry.getValue();
+ boolean hasComparisonOperator = condition.has("ComparisonOperator");
+ boolean hasExists = condition.has("Exists");
+ boolean hasValue = condition.has("Value");
+
+ if (condition.has("AttributeValueList") && !hasComparisonOperator) {
+ throw legacyExpectedValidationError(
+ "AttributeValueList can only be used with a ComparisonOperator for Attribute: " + attrName);
+ }
+ if (hasExists && hasComparisonOperator) {
+ throw legacyExpectedValidationError(
+ "Exists and ComparisonOperator cannot be used together for Attribute: " + attrName);
+ }
+ boolean expectsValue = !hasExists || condition.get("Exists").asBoolean();
+ if (!hasComparisonOperator && expectsValue && !hasValue) {
+ throw legacyExpectedValidationError("Value must be provided when Exists is "
+ + (hasExists ? "true" : "null") + " for Attribute: " + attrName);
+ }
+ if (!expectsValue && hasValue) {
+ throw legacyExpectedValidationError(
+ "Value cannot be used when Exists is false for Attribute: " + attrName);
+ }
+ }
+ }
+
+ private AwsException legacyExpectedValidationError(String detail) {
+ return new AwsException("ValidationException",
+ "1 validation error detected: One or more parameter values were invalid: " + detail, 400);
+ }
+
/**
* Converts a legacy Expected condition from the {"Value": {...}} form to the
* {"AttributeValueList": [{...}]} form expected by matchesKeyCondition.
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java
index c02f2c0cb7..729b37551a 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java
@@ -2461,14 +2461,18 @@ public class DynamoDbService {
return switch (op) {
case "EQ" -> {
- if (attrValue == null) yield false;
- yield ExpressionEvaluator.compareAttributeValues(attrValue, compareAttr != null ? compareAttr : attrValue) == 0
- && actual != null && actual.equals(compareValue);
+ if (attrValue == null || compareAttr == null) {
+ yield false;
+ }
+ // Deep equality, so set/list/map values compare by content rather than
+ // collapsing to an empty scalar and matching everything.
+ yield attributeValuesEqual(attrValue, compareAttr);
}
case "NE" -> {
- if (attrValue == null) yield true;
- if (actual == null || compareValue == null) yield true;
- yield !actual.equals(compareValue);
+ if (attrValue == null || compareAttr == null) {
+ yield true;
+ }
+ yield !attributeValuesEqual(attrValue, compareAttr);
}
case "NULL" -> attrValue == null;
case "NOT_NULL" -> attrValue != null;
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluator.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluator.java
index 0c28ebf8a2..b7c5eb20d7 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluator.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluator.java
@@ -1001,7 +1001,9 @@ final class ExpressionEvaluator {
if (a.has("NS") && b.has("NS")) {
JsonNode aArr = a.get("NS"), bArr = b.get("NS");
if (aArr.size() != bArr.size()) return false;
- var aSet = new java.util.HashSet<BigDecimal>();
+ // TreeSet membership goes through compareTo, so 1 and 1.0 are the same member.
+ // BigDecimal.equals() is scale-sensitive and would treat them as different.
+ var aSet = new java.util.TreeSet<BigDecimal>();
try {
aArr.forEach(e -> aSet.add(new BigDecimal(e.asText())));
for (JsonNode e : bArr) { if (!aSet.contains(new BigDecimal(e.asText()))) return false; }
diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java
index 021b694463..dfb847e197 100644
--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java
@@ -1929,6 +1929,784 @@ given()
.statusCode(200);
}
+ @Test
+ void updateItemLegacyExpectedExistsTrueComparesValue() {
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable4",
+ "KeySchema": [{"AttributeName": "PK", "KeyType": "HASH"}],
+ "AttributeDefinitions": [{"AttributeName": "PK", "AttributeType": "S"}],
+ "BillingMode": "PAY_PER_REQUEST"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.PutItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable4",
+ "Item": {"PK": {"S": "k1"}, "VERSION": {"N": "5"}, "SESSION_DATA": {"S": "{}"}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ // A current expectation (stored VERSION == 5) is honoured → the write lands.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable4",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"VERSION": {"Exists": true, "Value": {"N": "5"}}},
+ "AttributeUpdates": {"VERSION": {"Action": "PUT", "Value": {"N": "6"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ // A stale expectation (still claiming VERSION == 5, stored is 6) must be rejected.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable4",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"VERSION": {"Exists": true, "Value": {"N": "5"}}},
+ "AttributeUpdates": {"SESSION_DATA": {"Action": "DELETE"}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ConditionalCheckFailedException"));
+
+ // The stale write must not have taken effect.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.GetItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable4",
+ "Key": {"PK": {"S": "k1"}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Item.VERSION.N", equalTo("6"))
+ .body("Item.SESSION_DATA.S", equalTo("{}"));
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {"TableName": "LegacyExpectedTable4"}
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
+
+ @Test
+ void putAndDeleteItemLegacyExpectedExistsTrueComparesValue() {
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable5",
+ "KeySchema": [{"AttributeName": "PK", "KeyType": "HASH"}],
+ "AttributeDefinitions": [{"AttributeName": "PK", "AttributeType": "S"}],
+ "BillingMode": "PAY_PER_REQUEST"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.PutItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable5",
+ "Item": {"PK": {"S": "k1"}, "VERSION": {"N": "5"}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ // PutItem with a stale expectation → rejected.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.PutItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable5",
+ "Item": {"PK": {"S": "k1"}, "VERSION": {"N": "6"}},
+ "Expected": {"VERSION": {"Exists": true, "Value": {"N": "4"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ConditionalCheckFailedException"));
+
+ // DeleteItem with a stale expectation → rejected.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.DeleteItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable5",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"VERSION": {"Exists": true, "Value": {"N": "4"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ConditionalCheckFailedException"));
+
+ // Neither write took effect.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.GetItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable5",
+ "Key": {"PK": {"S": "k1"}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Item.VERSION.N", equalTo("5"));
+
+ // DeleteItem with a matching expectation → succeeds.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.DeleteItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable5",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"VERSION": {"Exists": true, "Value": {"N": "5"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {"TableName": "LegacyExpectedTable5"}
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
+
+ @Test
+ void legacyExpectedExistsFalseAndConditionalOperator() {
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable6",
+ "KeySchema": [{"AttributeName": "PK", "KeyType": "HASH"}],
+ "AttributeDefinitions": [{"AttributeName": "PK", "AttributeType": "S"}],
+ "BillingMode": "PAY_PER_REQUEST"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.PutItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable6",
+ "Item": {"PK": {"S": "k1"}, "VERSION": {"N": "5"}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ // Exists:false on a present attribute → rejected.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable6",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"VERSION": {"Exists": false}},
+ "AttributeUpdates": {"NOTE": {"Action": "PUT", "Value": {"S": "x"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ConditionalCheckFailedException"));
+
+ // Exists:false on an absent attribute → accepted.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable6",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"NOTE": {"Exists": false}},
+ "AttributeUpdates": {"NOTE": {"Action": "PUT", "Value": {"S": "x"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ // AND (the default) requires every entry to hold — the second one does not.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable6",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {
+ "VERSION": {"Exists": true, "Value": {"N": "5"}},
+ "NOTE": {"Exists": true, "Value": {"S": "other"}}
+ },
+ "AttributeUpdates": {"VERSION": {"Action": "PUT", "Value": {"N": "9"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ConditionalCheckFailedException"));
+
+ // OR only requires one entry to hold.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable6",
+ "Key": {"PK": {"S": "k1"}},
+ "ConditionalOperator": "OR",
+ "Expected": {
+ "VERSION": {"Exists": true, "Value": {"N": "5"}},
+ "NOTE": {"Exists": true, "Value": {"S": "other"}}
+ },
+ "AttributeUpdates": {"VERSION": {"Action": "PUT", "Value": {"N": "9"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {"TableName": "LegacyExpectedTable6"}
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
+
+ @Test
+ void legacyExpectedComparesCollectionValues() {
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable8",
+ "KeySchema": [{"AttributeName": "PK", "KeyType": "HASH"}],
+ "AttributeDefinitions": [{"AttributeName": "PK", "AttributeType": "S"}],
+ "BillingMode": "PAY_PER_REQUEST"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.PutItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable8",
+ "Item": {
+ "PK": {"S": "k1"},
+ "TAGS": {"SS": ["alpha", "beta"]},
+ "ITEMS": {"L": [{"N": "1"}, {"S": "two"}]},
+ "SCORES": {"NS": ["1", "2.5"]}
+ }
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ // A string set that differs from the stored one must not satisfy the condition.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable8",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"TAGS": {"Exists": true, "Value": {"SS": ["alpha", "gamma"]}}},
+ "AttributeUpdates": {"NOTE": {"Action": "PUT", "Value": {"S": "x"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ConditionalCheckFailedException"));
+
+ // A list that differs from the stored one must not satisfy the condition.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable8",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"ITEMS": {"Exists": true, "Value": {"L": [{"N": "1"}, {"S": "three"}]}}},
+ "AttributeUpdates": {"NOTE": {"Action": "PUT", "Value": {"S": "x"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ConditionalCheckFailedException"));
+
+ // A number set with different members must not satisfy the condition.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable8",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"SCORES": {"Exists": true, "Value": {"NS": ["1", "3.5"]}}},
+ "AttributeUpdates": {"NOTE": {"Action": "PUT", "Value": {"S": "x"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ConditionalCheckFailedException"));
+
+ // Number set members compare numerically, so trailing zeros still match.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable8",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"SCORES": {"Exists": true, "Value": {"NS": ["1.0", "2.50"]}}},
+ "AttributeUpdates": {"NOTE": {"Action": "PUT", "Value": {"S": "numeric"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ // NE is the inverse: a set that differs from the stored one satisfies the condition.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable8",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {
+ "TAGS": {
+ "ComparisonOperator": "NE",
+ "AttributeValueList": [{"SS": ["alpha", "gamma"]}]
+ }
+ },
+ "AttributeUpdates": {"NOTE": {"Action": "PUT", "Value": {"S": "differs"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ // ...and a set equal to the stored one does not.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable8",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {
+ "TAGS": {
+ "ComparisonOperator": "NE",
+ "AttributeValueList": [{"SS": ["beta", "alpha"]}]
+ }
+ },
+ "AttributeUpdates": {"NOTE": {"Action": "PUT", "Value": {"S": "x"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ConditionalCheckFailedException"));
+
+ // Set equality is order-independent, so the same members in another order matches.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable8",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"TAGS": {"Exists": true, "Value": {"SS": ["beta", "alpha"]}}},
+ "AttributeUpdates": {"NOTE": {"Action": "PUT", "Value": {"S": "matched"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.GetItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable8",
+ "Key": {"PK": {"S": "k1"}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Item.NOTE.S", equalTo("matched"));
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {"TableName": "LegacyExpectedTable8"}
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
+
+ @Test
+ void legacyExpectedComparisonIsTypeStrict() {
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable9",
+ "KeySchema": [{"AttributeName": "PK", "KeyType": "HASH"}],
+ "AttributeDefinitions": [{"AttributeName": "PK", "AttributeType": "S"}],
+ "BillingMode": "PAY_PER_REQUEST"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.PutItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable9",
+ "Item": {"PK": {"S": "k1"}, "COUNT": {"N": "5"}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ // A string "5" is a different attribute value than the number 5, so EQ must not match.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable9",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"COUNT": {"Exists": true, "Value": {"S": "5"}}},
+ "AttributeUpdates": {"NOTE": {"Action": "PUT", "Value": {"S": "x"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ConditionalCheckFailedException"));
+
+ // ...and NE, being the inverse, must be satisfied by the type mismatch.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable9",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {
+ "COUNT": {
+ "ComparisonOperator": "NE",
+ "AttributeValueList": [{"S": "5"}]
+ }
+ },
+ "AttributeUpdates": {"NOTE": {"Action": "PUT", "Value": {"S": "type-strict"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.GetItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable9",
+ "Key": {"PK": {"S": "k1"}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("Item.NOTE.S", equalTo("type-strict"));
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {"TableName": "LegacyExpectedTable9"}
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
+
+ @Test
+ void legacyExpectedRejectsInvalidExistsCombinations() {
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable7",
+ "KeySchema": [{"AttributeName": "PK", "KeyType": "HASH"}],
+ "AttributeDefinitions": [{"AttributeName": "PK", "AttributeType": "S"}],
+ "BillingMode": "PAY_PER_REQUEST"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ // Exists:true without a Value — nothing to compare against.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable7",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"VERSION": {"Exists": true}},
+ "AttributeUpdates": {"NOTE": {"Action": "PUT", "Value": {"S": "x"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"))
+ .body("message", equalTo("1 validation error detected: One or more parameter values were invalid: "
+ + "Value must be provided when Exists is true for Attribute: VERSION"));
+
+ // Exists:false alongside a Value — contradictory.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable7",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"VERSION": {"Exists": false, "Value": {"N": "5"}}},
+ "AttributeUpdates": {"NOTE": {"Action": "PUT", "Value": {"S": "x"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"))
+ .body("message", equalTo("1 validation error detected: One or more parameter values were invalid: "
+ + "Value cannot be used when Exists is false for Attribute: VERSION"));
+
+ // Exists mixed with the ComparisonOperator form.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.PutItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable7",
+ "Item": {"PK": {"S": "k1"}},
+ "Expected": {
+ "VERSION": {
+ "Exists": true,
+ "ComparisonOperator": "EQ",
+ "AttributeValueList": [{"N": "5"}]
+ }
+ }
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"))
+ .body("message", equalTo("1 validation error detected: One or more parameter values were invalid: "
+ + "Exists and ComparisonOperator cannot be used together for Attribute: VERSION"));
+
+ // AttributeValueList is only meaningful with a ComparisonOperator, whether or not
+ // Exists is present — and that check takes precedence over the Exists rules.
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable7",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"VERSION": {"Exists": true, "AttributeValueList": [{"N": "5"}]}},
+ "AttributeUpdates": {"NOTE": {"Action": "PUT", "Value": {"S": "x"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"))
+ .body("message", equalTo("1 validation error detected: One or more parameter values were invalid: "
+ + "AttributeValueList can only be used with a ComparisonOperator for Attribute: VERSION"));
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable7",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"VERSION": {"AttributeValueList": [{"N": "5"}]}},
+ "AttributeUpdates": {"NOTE": {"Action": "PUT", "Value": {"S": "x"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"))
+ .body("message", equalTo("1 validation error detected: One or more parameter values were invalid: "
+ + "AttributeValueList can only be used with a ComparisonOperator for Attribute: VERSION"));
+
+ // An entry with neither Exists nor Value still needs a Value, reported as "Exists is null".
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "LegacyExpectedTable7",
+ "Key": {"PK": {"S": "k1"}},
+ "Expected": {"VERSION": {}},
+ "AttributeUpdates": {"NOTE": {"Action": "PUT", "Value": {"S": "x"}}}
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"))
+ .body("message", equalTo("1 validation error detected: One or more parameter values were invalid: "
+ + "Value must be provided when Exists is null for Attribute: VERSION"));
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {"TableName": "LegacyExpectedTable7"}
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
+
@Test
void updateAndDescribeContinuousBackups() {
given()