Light, fluffy, and always free - The AWS Local Emulator alternative
fix(dynamodb): throw ValidationException when list_append operand is not a list type (#1702)
Prabu Dzaky2a9ab4598f47
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.javaindex 10c15e0fcf..3e0f81f224 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@@ -1586,16 +1586,22 @@ public class DynamoDbService {String arg2 = inner.substring(commaPos + 1).trim();JsonNode list1 = evaluateSetExpr(snapshot, arg1, exprAttrNames, exprAttrValues);JsonNode list2 = evaluateSetExpr(snapshot, arg2, exprAttrNames, exprAttrValues);- if (list1 != null && list2 != null && list1.has("L") && list2.has("L")) {- com.fasterxml.jackson.databind.node.ArrayNode merged =- com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.arrayNode();- list1.get("L").forEach(merged::add);- list2.get("L").forEach(merged::add);- com.fasterxml.jackson.databind.node.ObjectNode result =- com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();- result.set("L", merged);- item.set(attrName, result);+ if (list1 == null || list2 == null) {+ throw new AwsException("ValidationException",+ "The provided expression refers to an attribute that does not exist in the item", 400);+ }+ if (!list1.has("L") || !list2.has("L")) {+ throw new AwsException("ValidationException",+ "An operand in the update expression has an incorrect data type", 400);}+ com.fasterxml.jackson.databind.node.ArrayNode merged =+ com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.arrayNode();+ list1.get("L").forEach(merged::add);+ list2.get("L").forEach(merged::add);+ com.fasterxml.jackson.databind.node.ObjectNode result =+ com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();+ result.set("L", merged);+ item.set(attrName, result);}}} else if (valuePart.startsWith(":") && exprAttrValues != null) {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.javaindex f391017dca..021b694463 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@@ -1236,6 +1236,130 @@ class DynamoDbIntegrationTest {.statusCode(200);}+ @Test+ void updateItemListAppendAgainstNullAttributeReturnsValidationException() {+ given()+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {+ "TableName": "NullListAppendTable",+ "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": "NullListAppendTable",+ "Item": {"pk": {"S": "row-1"}, "Tags": {"NULL": true}}+ }+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200);++ given()+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {+ "TableName": "NullListAppendTable",+ "Key": {"pk": {"S": "row-1"}},+ "UpdateExpression": "SET #t = list_append(#t, :new)",+ "ExpressionAttributeNames": {"#t": "Tags"},+ "ExpressionAttributeValues": {":new": {"L": [{"S": "tag-a"}]}}+ }+ """)+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("ValidationException"));++ given()+ .header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName": "NullListAppendTable"}+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }++ @Test+ void updateItemListAppendAgainstMissingAttributeReturnsValidationException() {+ given()+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {+ "TableName": "MissingListAppendTable",+ "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": "MissingListAppendTable",+ "Item": {"pk": {"S": "row-1"}}+ }+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200);++ given()+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {+ "TableName": "MissingListAppendTable",+ "Key": {"pk": {"S": "row-1"}},+ "UpdateExpression": "SET #t = list_append(#t, :new)",+ "ExpressionAttributeNames": {"#t": "Tags"},+ "ExpressionAttributeValues": {":new": {"L": [{"S": "tag-a"}]}}+ }+ """)+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("ValidationException"));++ given()+ .header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName": "MissingListAppendTable"}+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }+@Test@Order(29)void deleteElementsFromStringSet() {diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.javaindex a1bd0c982e..36e5268719 100644--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java@@ -1076,6 +1076,55 @@ class DynamoDbServiceTest {assertEquals("b", stored.get("items").get("L").get(1).get("S").asText());}+ @Test+ void listAppendAgainstNullAttributeThrowsValidationException() {+ String region = "eu-west-1";+ createUsersTable(region);++ ObjectNode existing = mapper.createObjectNode();+ existing.set("userId", attributeValue("S", "u-null-list"));+ ObjectNode nullAttr = mapper.createObjectNode();+ nullAttr.put("NULL", true);+ existing.set("tags", nullAttr);+ service.putItem("Users", existing, region);++ ObjectNode key = item("userId", "u-null-list");+ ObjectNode exprValues = mapper.createObjectNode();+ exprValues.set(":new", listAttributeValue("tag-a"));++ ObjectNode exprNames = mapper.createObjectNode();+ exprNames.put("#t", "tags");++ AwsException ex = assertThrows(AwsException.class, () ->+ service.updateItem("Users", key, null,+ "SET #t = list_append(#t, :new)",+ exprNames, exprValues, null, region));+ assertEquals("ValidationException", ex.getErrorCode());+ assertTrue(ex.getMessage().contains("An operand in the update expression has an incorrect data type"));+ }++ @Test+ void listAppendAgainstMissingAttributeThrowsValidationException() {+ String region = "eu-west-1";+ createUsersTable(region);++ service.putItem("Users", item("userId", "u-no-tags"), region);++ ObjectNode key = item("userId", "u-no-tags");+ ObjectNode exprValues = mapper.createObjectNode();+ exprValues.set(":new", listAttributeValue("tag-a"));++ ObjectNode exprNames = mapper.createObjectNode();+ exprNames.put("#t", "tags");++ AwsException ex = assertThrows(AwsException.class, () ->+ service.updateItem("Users", key, null,+ "SET #t = list_append(#t, :new)",+ exprNames, exprValues, null, region));+ assertEquals("ValidationException", ex.getErrorCode());+ assertTrue(ex.getMessage().contains("The provided expression refers to an attribute that does not exist in the item"));+ }+@Testvoid scanContainsOnListWithNumericElements() {String region = "eu-west-1";