Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(dynamodb): reject QueryFilter with KeyConditionExpression (#2935)

Aniket Shukla7d693de663bf
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbConformanceChangesTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbConformanceChangesTest.java
index f9ce394b3b..535bef9546 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbConformanceChangesTest.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbConformanceChangesTest.java
@@ -547,8 +547,10 @@ class DynamoDbConformanceChangesTest {
void queryWithQueryFilter() {
QueryResponse resp = ddb.query(QueryRequest.builder()
.tableName(TABLE)
- .keyConditionExpression("pk = :pk")
- .expressionAttributeValues(Map.of(":pk", av("p1")))
+ .keyConditions(Map.of("pk", Condition.builder()
+ .comparisonOperator(ComparisonOperator.EQ)
+ .attributeValueList(av("p1"))
+ .build()))
.queryFilter(Map.of("name", Condition.builder()
.comparisonOperator(ComparisonOperator.EQ)
.attributeValueList(av("Item-0"))
diff --git a/compatibility-tests/sdk-test-node/tests/dynamodb-conformance.test.ts b/compatibility-tests/sdk-test-node/tests/dynamodb-conformance.test.ts
index a1b79897e6..9bfb7922b3 100644
--- a/compatibility-tests/sdk-test-node/tests/dynamodb-conformance.test.ts
+++ b/compatibility-tests/sdk-test-node/tests/dynamodb-conformance.test.ts
@@ -707,8 +707,9 @@ describe('DynamoDB — Phase 10: Legacy API', () => {
it('QueryFilter (legacy) filters query results', async () => {
const resp = await ddb.send(new QueryCommand({
TableName: table,
- KeyConditionExpression: 'pk = :pk',
- ExpressionAttributeValues: { ':pk': s('p1') },
+ KeyConditions: {
+ pk: { ComparisonOperator: 'EQ', AttributeValueList: [s('p1')] },
+ },
QueryFilter: {
name: { ComparisonOperator: 'EQ', AttributeValueList: [s('Item-0')] },
},
@@ -717,6 +718,23 @@ describe('DynamoDB — Phase 10: Legacy API', () => {
expect(resp.Items![0].name.S).toBe('Item-0');
});
+ it('QueryFilter and KeyConditionExpression are mutually exclusive', async () => {
+ const err = await ddb.send(new QueryCommand({
+ TableName: table,
+ KeyConditionExpression: 'pk = :pk',
+ ExpressionAttributeValues: { ':pk': s('p1') },
+ QueryFilter: {
+ name: { ComparisonOperator: 'EQ', AttributeValueList: [s('Item-0')] },
+ },
+ })).catch(e => e);
+
+ expect(err.name).toBe('ValidationException');
+ expect(err.message).toBe(
+ 'Can not use both expression and non-expression parameters in the same request: '
+ + 'Non-expression parameters: {QueryFilter} Expression parameters: {KeyConditionExpression}',
+ );
+ });
+
it('ScanFilter (legacy) filters scan results', async () => {
const resp = await ddb.send(new ScanCommand({
TableName: table,
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 c4c88b9764..d9d7eb3c12 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
@@ -826,6 +826,11 @@ public class DynamoDbJsonHandler {
"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: "
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 b3cf9f940e..bb2ef8e2fd 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
@@ -491,6 +491,36 @@ class DynamoDbIntegrationTest {
+ "Non-expression parameters: {AttributesToGet} Expression parameters: {ProjectionExpression}"));
}
+ @Test
+ @Order(10)
+ void queryWithQueryFilterAndKeyConditionExpressionFails() {
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.Query")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "TestTable",
+ "KeyConditionExpression": "pk = :pk",
+ "ExpressionAttributeValues": {
+ ":pk": {"S": "user-1"}
+ },
+ "QueryFilter": {
+ "name": {
+ "ComparisonOperator": "EQ",
+ "AttributeValueList": [{"S": "Alice"}]
+ }
+ }
+ }
+ """)
+ .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: {QueryFilter} Expression parameters: {KeyConditionExpression}"));
+ }
+
@Test
@Order(10)
void queryWithBeginsWith() {