diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathValidator.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathValidator.java index 03ac0bf9c8..54ce75265d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathValidator.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathValidator.java @@ -55,18 +55,12 @@ final class DynamoDbAccessPathValidator { static void validateSelection(TableDefinition table, DynamoDbAccessPath accessPath, String select, String projectionExpression, JsonNode attributesToGet, JsonNode expressionAttributeNames) { - if (projectionExpression != null && select != null && !"SPECIFIC_ATTRIBUTES".equals(select)) { - throw validationException("1 validation error detected: " - + "Cannot specify the ProjectionExpression when choosing to get " - + ("COUNT".equals(select) ? "only the Count" : select)); - } + rejectWithSelect("ProjectionExpression", projectionExpression != null, select); + rejectWithSelect("AttributesToGet", attributesToGet != null, select); if ("ALL_PROJECTED_ATTRIBUTES".equals(select) && !accessPath.isIndex()) { throw validationException("1 validation error detected: " + "ALL_PROJECTED_ATTRIBUTES can be used only when Querying using an IndexName"); } - if (attributesToGet != null && select != null && !"SPECIFIC_ATTRIBUTES".equals(select)) { - throw validationException("Cannot use both Select and AttributesToGet unless Select is SPECIFIC_ATTRIBUTES"); - } if (!accessPath.isGlobalSecondaryIndex() || "ALL".equals(accessPath.projectionType())) { return; } @@ -93,6 +87,14 @@ final class DynamoDbAccessPathValidator { } } + private static void rejectWithSelect(String parameter, boolean present, String select) { + if (present && select != null && !"SPECIFIC_ATTRIBUTES".equals(select)) { + throw validationException("1 validation error detected: " + + "Cannot specify the " + parameter + " when choosing to get " + + ("COUNT".equals(select) ? "only the Count" : select)); + } + } + private static String validateKeyConditionExpression(TableDefinition table, DynamoDbAccessPath accessPath, String expression, JsonNode names, diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandlerTest.java index a3d21cba65..cb33efd9de 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandlerTest.java @@ -1159,6 +1159,30 @@ class NativeDynamoDbJsonHandlerTest { + "when Querying using an IndexName", allProjected.getMessage()); } + @Test + void scanNamesTheSelectWhenRejectingAttributesToGet() { + createUsersTable("eu-west-1"); + + AwsException ex = expectValidationException("Scan", json(""" + {"TableName": "Users", "Select": "COUNT", "AttributesToGet": ["userId"]} + """)); + assertEquals("1 validation error detected: Cannot specify the AttributesToGet " + + "when choosing to get only the Count", ex.getMessage()); + } + + @Test + void queryReportsTheAttributesToGetConflictBeforeTheMissingIndex() { + createUsersTable("eu-west-1"); + + AwsException ex = expectValidationException("Query", json(""" + {"TableName": "Users", + "KeyConditions": {"userId": {"ComparisonOperator": "EQ", "AttributeValueList": [{"S": "a"}]}}, + "Select": "ALL_PROJECTED_ATTRIBUTES", "AttributesToGet": ["userId"]} + """)); + assertEquals("1 validation error detected: Cannot specify the AttributesToGet " + + "when choosing to get ALL_PROJECTED_ATTRIBUTES", ex.getMessage()); + } + @Test void scanAcceptsSegmentsAtTheBounds() throws Exception { createUsersTable("eu-west-1");