From a2b20cc08537651aefc93dedf11567704fa9e2a6 Mon Sep 17 00:00:00 2001 From: Dmitriy Polomoshnov <92720808+diopolgg@users.noreply.github.com> Date: Mon, 10 Aug 2026 02:03:42 +0300 Subject: [PATCH] fix(dynamodb): compare Value when legacy Expected supplies Exists (#2125) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(dynamodb): compare Value when legacy Expected supplies Exists A legacy Expected entry that supplies both Exists:true and a Value was evaluated as an existence check only — the supplied Value was silently discarded, so any stale value passed the condition. This is the wire form emitted by version-guarded writes, where the caller pairs Exists:true with the version it last read: "Expected": {"version": {"Exists": true, "Value": {"N": "5"}}} Because the guard was present on every request and ignored on every request, conflicting writes were accepted with no visible symptom. Per the documented ExpectedAttributeValue semantics, Exists:true always carries a Value and means the attribute must exist AND equal it, which is the same check as a bare Value. Only Exists:false is a pure absence check. The three combinations DynamoDB documents as invalid now raise ValidationException instead of being silently accepted: - Exists:true with no Value - Exists:false alongside a Value - Exists combined with ComparisonOperator / AttributeValueList Validation runs over the whole Expected map before any condition is evaluated. PutItem, DeleteItem and UpdateItem all route through evaluateLegacyExpected, so the change applies to all three. * fix(dynamodb): compare collection values in legacy EQ conditions The EQ comparison used by legacy Expected, QueryFilter and ScanFilter reduced a non-scalar attribute value to an empty string, so any two values of the same collection type compared as equal. A stale expectation on an SS, NS, BS, L or M value was therefore always satisfied and the guarded write was applied. EQ now delegates to the existing attributeValuesEqual helper — the same one the ConditionExpression path uses. It is type-strict, compares numbers via BigDecimal, recurses into M and L, and treats set types as unordered. * fix(dynamodb): compare number set members numerically in equality Number set equality put the members in a HashSet, so membership went through BigDecimal.equals, which is scale-sensitive: 1 and 1.0 were treated as different members. Because NS elements are normalized on write while a supplied condition value is not, an otherwise valid condition on a numerically equivalent set was rejected and the guarded write blocked. Membership now goes through a TreeSet, whose contains uses compareTo, so members compare numerically. Applies to both the legacy Expected / QueryFilter / ScanFilter EQ path and ConditionExpression, which share this helper. * fix(dynamodb): compare collection values in legacy NE conditions NE had the inverse of the collapse the previous commit fixed for EQ: it compared the scalar projections of both values, which are empty strings for non-scalar types, so any two collections of the same type looked equal and NE was always false. A condition asserting an attribute differs from a set it genuinely differs from blocked the write. NE now negates attributeValuesEqual, mirroring EQ. Null handling is unchanged: a missing attribute or a missing comparison value still satisfies NE. * test(dynamodb): pin type-strict equality in legacy conditions EQ and NE compare attribute types as well as values, so a string "5" does not match a stored number 5. Nothing pinned that, which left room for a refactor to reintroduce equality via scalar string projections without any test failing. * fix(dynamodb): match DynamoDB validation for legacy Expected entries Verified the legacy Expected rules against DynamoDB itself and aligned the validation with what the service actually returns. Error messages now carry the "1 validation error detected: " prefix and the exact wording, including "Exists and ComparisonOperator cannot be used together", which differed from the message used before. Two rules were missing: - AttributeValueList without a ComparisonOperator is rejected, and that check takes precedence over the Exists rules. It applies whether or not Exists is present. - An entry with neither Exists nor Value is rejected as well, reported as "Value must be provided when Exists is null". DynamoDB reports only the first offending entry even when several are invalid, so validation still stops at the first error. --- .../test/DynamoDbConformanceChangesTest.java | 118 +++ .../dynamodb/DynamoDbJsonHandler.java | 57 +- .../services/dynamodb/DynamoDbService.java | 16 +- .../dynamodb/ExpressionEvaluator.java | 4 +- .../dynamodb/DynamoDbIntegrationTest.java | 778 ++++++++++++++++++ 5 files changed, 963 insertions(+), 10 deletions(-) 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 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: + * + * 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(); + // 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(); 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()