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 a4099e9155..b6998da274 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 @@ -427,9 +427,9 @@ final class DynamoDbAccessPathValidator { return new AwsException("ValidationException", message, 400); } // DynamoDB requires an ExclusiveStartKey to exactly match the paged key schema's - // attribute names and scalar types. Query and Scan report different messages for faults. + // attribute names and scalar types. static void validateExclusiveStartKey(JsonNode exclusiveStartKey, TableDefinition table, - DynamoDbAccessPath accessPath, boolean isScan) { + DynamoDbAccessPath accessPath) { if (exclusiveStartKey == null || exclusiveStartKey.isNull()) return; Set expected = new HashSet<>(); expected.add(table.getPartitionKeyName()); @@ -443,7 +443,7 @@ final class DynamoDbAccessPathValidator { Set actual = new HashSet<>(); exclusiveStartKey.fieldNames().forEachRemaining(actual::add); if (!actual.equals(expected) || hasInvalidKeyValue(exclusiveStartKey, table, expected)) { - throw invalidStartingKey(isScan); + throw invalidStartingKey(); } } @@ -483,9 +483,7 @@ final class DynamoDbAccessPathValidator { return false; } - private static AwsException invalidStartingKey(boolean isScan) { - return new AwsException("ValidationException", isScan - ? "The provided starting key is invalid: The provided key element does not match the schema" - : "The provided starting key is invalid", 400); + private static AwsException invalidStartingKey() { + return new AwsException("ValidationException", "The provided starting key is invalid", 400); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLHandler.java index 3266683d02..8e14c706cc 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLHandler.java @@ -216,7 +216,7 @@ class DynamoDbPartiQLHandler { boolean partialProjection = !"ALL".equals(accessPath.projectionType()); String fe = partialProjection ? null : buildFe(scanFilters, eav, ean); JsonNode exclusiveStartKey = startKey.get(); - DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, table, accessPath, true); + DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, table, accessPath); DynamoDbService.ScanResult result = service.scan(stmt.table(), fe, ean.isEmpty() ? null : ean.toNode(mapper), eav.isEmpty() ? null : eav.toNode(mapper), @@ -241,7 +241,7 @@ class DynamoDbPartiQLHandler { String kce = buildKce(pkEq, skCond, pkName, skName, eav, ean); String fe = buildFe(filterConds, eav, ean); JsonNode exclusiveStartKey = startKey.get(); - DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, table, accessPath, false); + DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, table, accessPath); DynamoDbService.QueryResult result = service.query( stmt.table(), null, eav.toNode(mapper), kce, fe, limit, null, accessPath.indexName(), exclusiveStartKey, @@ -255,7 +255,7 @@ class DynamoDbPartiQLHandler { ExprAttrNameBuilder ean = new ExprAttrNameBuilder(); String kce = ean.alias(accessPath.partitionKeyName()) + " = " + eav.add(toTypedNode(routing.partition())); JsonNode exclusiveStartKey = startKey.get(); - DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, table, accessPath, false); + DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, table, accessPath); DynamoDbService.QueryResult result = service.query(stmt.table(), null, eav.toNode(mapper), kce, null, limit, null, accessPath.indexName(), exclusiveStartKey, ean.toNode(mapper), region); List matching = matchingIndexView(result.items(), routing.filterConds(), accessPath, table); 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 10a6cee92d..2d49f87e0d 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 @@ -4424,7 +4424,7 @@ public class DynamoDbService { if (exclusiveStartKey == null || exclusiveStartKey.isNull()) { return; } - DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, table, accessPath, false); + DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, table, accessPath); boolean withinBounds = keyConditionExpression != null ? ExpressionEvaluator.matches(keyConditionExpression, exclusiveStartKey, diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java index 66afdb0cb9..f17adc1623 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java @@ -1123,7 +1123,6 @@ public class NativeDynamoDbJsonHandler { boolean hasAttributesToGet = attributesToGet != null && attributesToGet.size() > 0; if ("SPECIFIC_ATTRIBUTES".equals(select) && projectionExpression == null && !hasAttributesToGet) { - // Query prefixes the count here, Scan does not. throw new AwsException("ValidationException", "1 validation error detected: " + SELECT_NEEDS_PROJECTION, 400); } @@ -1144,7 +1143,7 @@ public class NativeDynamoDbJsonHandler { checkUnusedEan(exprAttrNames, hashTokens); checkUnusedEav(exprAttrValues, extractColonTokens(keyConditionExpr, filterExpr)); if (exclusiveStartKey != null) { - DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, queryTable, queryAccessPath, false); + DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, queryTable, queryAccessPath); } DynamoDbService.QueryResult result = dynamoDbService.query(tableName, keyConditions, @@ -1228,33 +1227,24 @@ public class NativeDynamoDbJsonHandler { + "Non-expression parameters: {ScanFilter} Expression parameters: {FilterExpression}", 400); } + List rangeErrors = new ArrayList<>(); if (limit != null && limit < 1) { - throw new AwsException("ValidationException", - "1 validation error detected: Value '" + limit + "' at 'limit' failed to satisfy constraint: " - + "Member must have value greater than or equal to 1", 400); - } - - var segmentErrors = new ArrayList(); - if (totalSegments != null) { - if (totalSegments < 1) { - segmentErrors.add("Value '" + totalSegments + "' at 'totalSegments' failed to satisfy constraint: " - + "Member must have value greater than or equal to 1"); - } - if (MAX_TOTAL_SEGMENTS < totalSegments) { - segmentErrors.add("Value '" + totalSegments + "' at 'totalSegments' failed to satisfy constraint: " - + "Member must have value less than or equal to " + MAX_TOTAL_SEGMENTS); - } + rangeErrors.add("Value at 'Limit' failed to satisfy constraint: " + + "Member must have value greater than or equal to 1"); } if (segment != null && segment < 0) { - segmentErrors.add("Value '" + segment + "' at 'segment' failed to satisfy constraint: " + rangeErrors.add("Value at 'Segment' failed to satisfy constraint: " + "Member must have value greater than or equal to 0"); } - if (!segmentErrors.isEmpty()) { - var n = segmentErrors.size(); - throw new AwsException("ValidationException", - n + " validation error" + (n > 1 ? "s" : "") + " detected: " - + String.join("; ", segmentErrors), 400); + if (totalSegments != null && totalSegments < 1) { + rangeErrors.add("Value at 'TotalSegments' failed to satisfy constraint: " + + "Member must have value greater than or equal to 1"); } + if (totalSegments != null && MAX_TOTAL_SEGMENTS < totalSegments) { + rangeErrors.add("Value at 'TotalSegments' failed to satisfy constraint: " + + "Member must have value less than or equal to " + MAX_TOTAL_SEGMENTS); + } + throwValidationErrors(rangeErrors); if (segment != null && totalSegments == null) { throw new AwsException("ValidationException", @@ -1266,8 +1256,7 @@ public class NativeDynamoDbJsonHandler { } if (segment != null && totalSegments != null && segment >= totalSegments) { throw new AwsException("ValidationException", - "The Segment parameter is zero-based and must be less than parameter TotalSegments: " - + "Segment: " + segment + " is not less than TotalSegments: " + totalSegments, 400); + "The Segment parameter is zero-based and must be less than parameter TotalSegments", 400); } DynamoDbExpressionSize.checkReadWithSize(filterExpr, "FilterExpression"); @@ -1297,7 +1286,8 @@ public class NativeDynamoDbJsonHandler { boolean hasAttributesToGetScan = attributesToGetScan != null && attributesToGetScan.size() > 0; if ("SPECIFIC_ATTRIBUTES".equals(select) && projectionExpressionScan == null && !hasAttributesToGetScan) { - throw new AwsException("ValidationException", SELECT_NEEDS_PROJECTION, 400); + throw new AwsException("ValidationException", + "1 validation error detected: " + SELECT_NEEDS_PROJECTION, 400); } TableDefinition scanTable = dynamoDbService.requireTableForItemCall(tableName, region); @@ -1307,7 +1297,7 @@ public class NativeDynamoDbJsonHandler { rejectConsistentReadOnGsi(request, scanAccessPath); if (exclusiveStartKey != null) { - DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, scanTable, scanAccessPath, true); + DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, scanTable, scanAccessPath); } DynamoDbService.ScanResult result = dynamoDbService.scan( diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathIntegrationTest.java index 31de391e07..962ff9409f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathIntegrationTest.java @@ -341,8 +341,7 @@ class DynamoDbAccessPathIntegrationTest { """.formatted(TABLE)) .statusCode(400) .body("__type", equalTo("ValidationException")) - .body("message", equalTo("The provided starting key is invalid: " - + "The provided key element does not match the schema")); + .body("message", equalTo("The provided starting key is invalid")); request("DynamoDB_20120810.Query", """ { 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 a93a624a14..1bc3ee2e9a 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 @@ -1078,7 +1078,7 @@ class DynamoDbIntegrationTest { .then() .statusCode(400) .body("__type", equalTo("ValidationException")) - .body("message", equalTo("Must specify the AttributesToGet or " + .body("message", equalTo("1 validation error detected: Must specify the AttributesToGet or " + "ProjectionExpression when choosing to get SPECIFIC_ATTRIBUTES")); } 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 88f6dbe940..f5bd5cf7d5 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 @@ -1043,7 +1043,7 @@ class NativeDynamoDbJsonHandlerTest { AwsException ex = expectValidationException("Scan", json(""" {"TableName": "Users", "Segment": 0, "TotalSegments": 1000001} """)); - assertEquals("1 validation error detected: Value '1000001' at 'totalSegments' failed to " + assertEquals("1 validation error detected: Value at 'TotalSegments' failed to " + "satisfy constraint: Member must have value less than or equal to 1000000", ex.getMessage()); } @@ -1066,7 +1066,7 @@ class NativeDynamoDbJsonHandlerTest { AwsException ex = expectValidationException("Scan", json(""" {"TableName": "Users", "Segment": -1, "TotalSegments": 4} """)); - assertEquals("1 validation error detected: Value '-1' at 'segment' failed to satisfy constraint: " + assertEquals("1 validation error detected: Value at 'Segment' failed to satisfy constraint: " + "Member must have value greater than or equal to 0", ex.getMessage()); } @@ -1075,22 +1075,52 @@ class NativeDynamoDbJsonHandlerTest { AwsException ex = expectValidationException("Scan", json(""" {"TableName": "Users", "Segment": 0, "TotalSegments": 0} """)); - assertEquals("1 validation error detected: Value '0' at 'totalSegments' failed to satisfy constraint: " + assertEquals("1 validation error detected: Value at 'TotalSegments' failed to satisfy constraint: " + "Member must have value greater than or equal to 1", ex.getMessage()); } @Test - void scanReportsTotalSegmentsBeforeSegmentWhenBothAreOutOfRange() { + void scanReportsSegmentBeforeTotalSegmentsWhenBothAreOutOfRange() { AwsException ex = expectValidationException("Scan", json(""" {"TableName": "Users", "Segment": -1, "TotalSegments": 0} """)); assertEquals("2 validation errors detected: " - + "Value '0' at 'totalSegments' failed to satisfy constraint: " + + "Value at 'Segment' failed to satisfy constraint: " + + "Member must have value greater than or equal to 0; " + + "Value at 'TotalSegments' failed to satisfy constraint: " + + "Member must have value greater than or equal to 1", ex.getMessage()); + } + + @Test + void scanReportsLimitAndSegmentTogetherBeforeTheTableLookup() { + AwsException ex = expectValidationException("Scan", json(""" + {"TableName": "Missing", "Limit": 0, "Segment": -1, "TotalSegments": 2} + """)); + assertEquals("2 validation errors detected: " + + "Value at 'Limit' failed to satisfy constraint: " + "Member must have value greater than or equal to 1; " - + "Value '-1' at 'segment' failed to satisfy constraint: " + + "Value at 'Segment' failed to satisfy constraint: " + "Member must have value greater than or equal to 0", ex.getMessage()); } + @Test + void scanRejectsASegmentNotBelowTotalSegments() { + AwsException ex = expectValidationException("Scan", json(""" + {"TableName": "Users", "Segment": 5, "TotalSegments": 5} + """)); + assertEquals("The Segment parameter is zero-based and must be less than parameter TotalSegments", + ex.getMessage()); + } + + @Test + void scanPrefixesTheMissingProjectionForSpecificAttributes() { + AwsException ex = expectValidationException("Scan", json(""" + {"TableName": "Users", "Select": "SPECIFIC_ATTRIBUTES"} + """)); + assertEquals("1 validation error detected: Must specify the AttributesToGet or ProjectionExpression " + + "when choosing to get SPECIFIC_ATTRIBUTES", ex.getMessage()); + } + @Test void scanAcceptsSegmentsAtTheBounds() throws Exception { createUsersTable("eu-west-1");