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 85a7e847f9..33e07a9e28 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 @@ -680,6 +680,11 @@ public class DynamoDbJsonHandler { "Can not use both expression and non-expression parameters in the same request: " + "Non-expression parameters: {QueryFilter} Expression parameters: {FilterExpression}", 400); } + if (attributesToGet != null && projectionExpression != null) { + throw new AwsException("ValidationException", + "Can not use both expression and non-expression parameters in the same request: " + + "Non-expression parameters: {AttributesToGet} Expression parameters: {ProjectionExpression}", 400); + } if (keyConditionExpr == null && keyConditions == null) { throw new AwsException("ValidationException", @@ -698,7 +703,8 @@ public class DynamoDbJsonHandler { + "Member must satisfy enum value set: [SPECIFIC_ATTRIBUTES, COUNT, ALL_ATTRIBUTES, ALL_PROJECTED_ATTRIBUTES]", 400); } - if ("SPECIFIC_ATTRIBUTES".equals(select) && projectionExpression == null) { + boolean hasAttributesToGet = attributesToGet != null && attributesToGet.size() > 0; + if ("SPECIFIC_ATTRIBUTES".equals(select) && projectionExpression == null && !hasAttributesToGet) { throw new AwsException("ValidationException", "Select type SPECIFIC_ATTRIBUTES requires the ProjectionExpression to be provided.", 400); } 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 ad8bb578e5..1f0eb091f8 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 @@ -293,6 +293,126 @@ class DynamoDbIntegrationTest { .body("Items.size()", equalTo(3)); } + @Test + @Order(10) + void queryWithSelectSpecificAttributesAndAttributesToGet() { + given() + .header("X-Amz-Target", "DynamoDB_20120810.Query") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "TestTable", + "KeyConditionExpression": "pk = :pk", + "ExpressionAttributeValues": { + ":pk": {"S": "user-1"} + }, + "Select": "SPECIFIC_ATTRIBUTES", + "AttributesToGet": ["pk", "sk"] + } + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("Count", equalTo(3)) + .body("Items[0].size()", equalTo(2)) + .body("Items[0].pk.S", equalTo("user-1")) + .body("Items[0].sk.S", equalTo("order-001")) + .body("Items[0].name", nullValue()) + .body("Items[0].age", nullValue()) + .body("Items[0].total", nullValue()) + .body("Items[1].size()", equalTo(2)) + .body("Items[1].pk.S", equalTo("user-1")) + .body("Items[1].sk.S", equalTo("order-002")) + .body("Items[1].total", nullValue()) + .body("Items[2].size()", equalTo(2)) + .body("Items[2].pk.S", equalTo("user-1")) + .body("Items[2].sk.S", equalTo("profile")) + .body("Items[2].name", nullValue()) + .body("Items[2].age", nullValue()); + } + + @Test + @Order(10) + void queryWithSelectSpecificAttributesAndProjectionExpression() { + given() + .header("X-Amz-Target", "DynamoDB_20120810.Query") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "TestTable", + "KeyConditionExpression": "pk = :pk", + "ExpressionAttributeValues": { + ":pk": {"S": "user-1"} + }, + "Select": "SPECIFIC_ATTRIBUTES", + "ProjectionExpression": "pk, sk" + } + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("Count", equalTo(3)) + .body("Items[0].size()", equalTo(2)) + .body("Items[0].pk.S", equalTo("user-1")) + .body("Items[0].sk.S", equalTo("order-001")) + .body("Items[0].name", nullValue()) + .body("Items[0].age", nullValue()) + .body("Items[0].total", nullValue()); + } + + @Test + @Order(10) + void queryWithSelectSpecificAttributesRequiresProjectionParameters() { + given() + .header("X-Amz-Target", "DynamoDB_20120810.Query") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "TestTable", + "KeyConditionExpression": "pk = :pk", + "ExpressionAttributeValues": { + ":pk": {"S": "user-1"} + }, + "Select": "SPECIFIC_ATTRIBUTES" + } + """) + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("Select type SPECIFIC_ATTRIBUTES requires the ProjectionExpression to be provided.")); + } + + @Test + @Order(10) + void queryWithProjectionExpressionAndAttributesToGetFails() { + given() + .header("X-Amz-Target", "DynamoDB_20120810.Query") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "TestTable", + "KeyConditionExpression": "pk = :pk", + "ExpressionAttributeValues": { + ":pk": {"S": "user-1"} + }, + "Select": "SPECIFIC_ATTRIBUTES", + "ProjectionExpression": "pk, sk", + "AttributesToGet": ["pk", "sk"] + } + """) + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("Can not use both expression and non-expression parameters in the same request: " + + "Non-expression parameters: {AttributesToGet} Expression parameters: {ProjectionExpression}")); + } + @Test @Order(10) void queryWithBeginsWith() {