Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(dynamodb): reject every mix of expression and non-expression parameters in Query and Scan (#5619)

TATSUNO “Taz” Yasuhiro804335d071e7
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 e1fd3fac51..7c024c70e7 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
@@ -1098,26 +1098,12 @@ public class NativeDynamoDbJsonHandler {
+ "Member must satisfy enum value set: [INDEXES, TOTAL, NONE]", 400);
}
- if (keyConditionExpr != null && keyConditions != null) {
- throw new AwsException("ValidationException",
- "Can not use both expression and non-expression parameters in the same request: "
- + "Non-expression parameters: {KeyConditions} Expression parameters: {KeyConditionExpression}", 400);
- }
- if (keyConditionExpr != null && queryFilter != null) {
- throw new AwsException("ValidationException",
- "Can not use both expression and non-expression parameters in the same request: "
- + "Non-expression parameters: {QueryFilter} Expression parameters: {KeyConditionExpression}", 400);
- }
- if (filterExpr != null && queryFilter != null) {
- throw new AwsException("ValidationException",
- "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);
- }
+ // DynamoDB counts KeyConditions as a non-expression parameter only next to a KeyConditionExpression.
+ rejectMixedParameters(request,
+ keyConditionExpr == null
+ ? List.of("AttributesToGet", "QueryFilter", "ConditionalOperator")
+ : List.of("AttributesToGet", "QueryFilter", "ConditionalOperator", "KeyConditions"),
+ List.of("ProjectionExpression", "FilterExpression", "KeyConditionExpression"));
if (keyConditionExpr == null && keyConditions == null) {
throw new AwsException("ValidationException",
@@ -1254,12 +1240,6 @@ public class NativeDynamoDbJsonHandler {
Integer segment = request.has("Segment") ? request.get("Segment").asInt() : null;
Integer totalSegments = request.has("TotalSegments") ? request.get("TotalSegments").asInt() : null;
- if (filterExpr != null && scanFilter != null) {
- throw new AwsException("ValidationException",
- "Can not use both expression and non-expression parameters in the same request: "
- + "Non-expression parameters: {ScanFilter} Expression parameters: {FilterExpression}", 400);
- }
-
List<String> rangeErrors = new ArrayList<>();
if (limit != null && limit < 1) {
rangeErrors.add("Value at 'Limit' failed to satisfy constraint: "
@@ -1279,6 +1259,10 @@ public class NativeDynamoDbJsonHandler {
}
throwValidationErrors(rangeErrors);
+ rejectMixedParameters(request,
+ List.of("AttributesToGet", "ScanFilter", "ConditionalOperator"),
+ List.of("ProjectionExpression", "FilterExpression"));
+
if (segment != null && totalSegments == null) {
throw new AwsException("ValidationException",
"The TotalSegments parameter is required but was not present in the request when Segment parameter is present", 400);
@@ -1311,11 +1295,6 @@ public class NativeDynamoDbJsonHandler {
"1 validation error detected: Value '" + select + "' at 'select' failed to satisfy constraint: "
+ "Member must satisfy enum value set: [SPECIFIC_ATTRIBUTES, COUNT, ALL_ATTRIBUTES, ALL_PROJECTED_ATTRIBUTES]", 400);
}
- if (projectionExpressionScan != null && attributesToGetScan != 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);
- }
boolean hasAttributesToGetScan = attributesToGetScan != null && attributesToGetScan.size() > 0;
if ("SPECIFIC_ATTRIBUTES".equals(select)
&& projectionExpressionScan == null && !hasAttributesToGetScan) {
@@ -2040,6 +2019,18 @@ public class NativeDynamoDbJsonHandler {
throwValidationErrors(validationErrors);
}
+ private static void rejectMixedParameters(JsonNode request, List<String> nonExpressionNames,
+ List<String> expressionNames) {
+ List<String> nonExpression = nonExpressionNames.stream().filter(request::has).toList();
+ List<String> expression = expressionNames.stream().filter(request::has).toList();
+ if (!nonExpression.isEmpty() && !expression.isEmpty()) {
+ throw new AwsException("ValidationException",
+ "Can not use both expression and non-expression parameters in the same request: "
+ + "Non-expression parameters: {" + String.join(", ", nonExpression)
+ + "} Expression parameters: {" + String.join(", ", expression) + "}", 400);
+ }
+ }
+
private static void throwValidationErrors(List<String> validationErrors) {
if (validationErrors.isEmpty()) {
return;
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 1bc3ee2e9a..eebbf47ad3 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
@@ -947,9 +947,8 @@ class DynamoDbIntegrationTest {
.body("""
{
"TableName": "TestTable",
- "KeyConditionExpression": "pk = :pk",
- "ExpressionAttributeValues": {
- ":pk": {"S": "user-1"}
+ "KeyConditions": {
+ "pk": {"ComparisonOperator": "EQ", "AttributeValueList": [{"S": "user-1"}]}
},
"Select": "SPECIFIC_ATTRIBUTES",
"AttributesToGet": ["pk", "sk"]
@@ -1106,7 +1105,8 @@ class DynamoDbIntegrationTest {
.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}"));
+ + "Non-expression parameters: {AttributesToGet} "
+ + "Expression parameters: {ProjectionExpression, KeyConditionExpression}"));
}
@Test
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 cb33efd9de..e0c78ce95a 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
@@ -1183,6 +1183,56 @@ class NativeDynamoDbJsonHandlerTest {
+ "when choosing to get ALL_PROJECTED_ATTRIBUTES", ex.getMessage());
}
+ @Test
+ void queryListsEveryMixedParameter() {
+ AwsException ex = expectValidationException("Query", json("""
+ {"TableName": "Users", "KeyConditionExpression": "userId = :u",
+ "ExpressionAttributeValues": {":u": {"S": "a"}},
+ "KeyConditions": {"userId": {"ComparisonOperator": "EQ", "AttributeValueList": [{"S": "a"}]}},
+ "FilterExpression": "attribute_exists(x)", "ProjectionExpression": "userId",
+ "AttributesToGet": ["userId"], "ConditionalOperator": "AND",
+ "QueryFilter": {"x": {"ComparisonOperator": "NOT_NULL"}}}
+ """));
+ assertEquals("Can not use both expression and non-expression parameters in the same request: "
+ + "Non-expression parameters: {AttributesToGet, QueryFilter, ConditionalOperator, KeyConditions} "
+ + "Expression parameters: {ProjectionExpression, FilterExpression, KeyConditionExpression}",
+ ex.getMessage());
+ }
+
+ @Test
+ void queryAllowsAFilterExpressionWithKeyConditions() throws Exception {
+ createUsersTable("eu-west-1");
+
+ Response response = handler.handle("Query", json("""
+ {"TableName": "Users",
+ "KeyConditions": {"userId": {"ComparisonOperator": "EQ", "AttributeValueList": [{"S": "a"}]}},
+ "FilterExpression": "attribute_exists(x)"}
+ """), "eu-west-1");
+ assertEquals(200, response.getStatus());
+ }
+
+ @Test
+ void scanListsEveryMixedParameter() {
+ AwsException ex = expectValidationException("Scan", json("""
+ {"TableName": "Users", "FilterExpression": "attribute_exists(x)",
+ "ProjectionExpression": "userId", "AttributesToGet": ["userId"],
+ "ConditionalOperator": "AND", "ScanFilter": {"x": {"ComparisonOperator": "NOT_NULL"}}}
+ """));
+ assertEquals("Can not use both expression and non-expression parameters in the same request: "
+ + "Non-expression parameters: {AttributesToGet, ScanFilter, ConditionalOperator} "
+ + "Expression parameters: {ProjectionExpression, FilterExpression}", ex.getMessage());
+ }
+
+ @Test
+ void scanReportsTheLimitBeforeMixedParameters() {
+ AwsException ex = expectValidationException("Scan", json("""
+ {"TableName": "Users", "Limit": 0, "FilterExpression": "attribute_exists(x)",
+ "ScanFilter": {"x": {"ComparisonOperator": "NOT_NULL"}}}
+ """));
+ assertEquals("1 validation error detected: Value at 'Limit' failed to satisfy constraint: "
+ + "Member must have value greater than or equal to 1", ex.getMessage());
+ }
+
@Test
void scanAcceptsSegmentsAtTheBounds() throws Exception {
createUsersTable("eu-west-1");