mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): more request validations in Query and Scan (#3360)
* fix(dynamodb): accept and report Query key conditions the way AWS does Three gaps in KeyConditionExpression validation. A sort-key comparison with the value on the left, such as ":lo <= sk", is valid on AWS and means "sk >= :lo". Floci rejected it because it only looked for the attribute on the left. The operands are now normalised before the key checks run. A nested path on a key attribute now reports that it cannot carry a condition, instead of the generic unsupported message. A condition on a non-key attribute now reports the partition key it left unconditioned, which is what AWS answers. Closes 2 TIER 1 and 1 TIER 3 case in the dynamodb-conformance suite. * fix(dynamodb): bound Segment and TotalSegments on Scan A parallel Scan accepted a negative Segment. A TotalSegments of 0 was rejected, but with the zero-based wording instead of the AWS constraint wording. Both are now rejected with the AWS constraint wording, before the required-parameter checks run. The TotalSegments upper bound from #3287 moves into the same block, so several violations are reported in one message. Closes 1 TIER 3 case in the dynamodb-conformance suite. * fix(dynamodb): report TotalSegments before Segment on Scan and test the lower bound
This commit is contained in:
1 parent
6b17ef2b1c
commit
fbe92aa411
5 files changed
+158
-11
No files matched your search
+37
@@ -350,6 +350,43 @@ class DynamoDbAccessPathValidatorTest {
|
||||
table, tablePath, "COUNT", "pk", null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void acceptsAKeyConditionWithTheValueOnTheLeft() {
|
||||
assertDoesNotThrow(() -> validateExpression(tablePath, "pk = :pk AND :lo <= sk"));
|
||||
assertDoesNotThrow(() -> validateExpression(tablePath, "pk = :pk AND :hi > sk"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsANestedPathOnAKeyAttribute() {
|
||||
var names = mapper.createObjectNode();
|
||||
names.put("#sk", "sk");
|
||||
var values = expressionValues(":pk", ":v");
|
||||
|
||||
var error = assertThrows(AwsException.class,
|
||||
() -> DynamoDbAccessPathValidator.validateQuery(
|
||||
table, tablePath, null, "pk = :pk AND #sk.foo = :v",
|
||||
null, null, names, values));
|
||||
|
||||
assertEquals("KeyConditionExpressions cannot have conditions on nested attributes",
|
||||
error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportsTheMissingKeySchemaElementForANonKeyAttribute() {
|
||||
var error = assertThrows(AwsException.class,
|
||||
() -> validateExpression(tablePath, "attr1 = :v"));
|
||||
|
||||
assertEquals("Query condition missed key schema element: pk", error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stillRejectsANonKeyAttributeAlongsideTheFullKey() {
|
||||
var error = assertThrows(AwsException.class,
|
||||
() -> validateExpression(tablePath, "pk = :pk AND attr1 = :v"));
|
||||
|
||||
assertEquals("Query key condition not supported", error.getMessage());
|
||||
}
|
||||
|
||||
private void validateExpression(DynamoDbAccessPath accessPath, String expression) {
|
||||
DynamoDbAccessPathValidator.validateQuery(
|
||||
table, accessPath, null, expression, null, null, null,
|
||||
|
||||
+40
@@ -914,4 +914,44 @@ class DynamoDbJsonHandlerTest {
|
||||
assertEquals("ExpressionAttributeNames can only be specified when using expressions: "
|
||||
+ "UpdateExpression is null, ConditionExpression is null", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void scanRejectsANegativeSegment() {
|
||||
var ex = expectValidationException("Scan", json("""
|
||||
{"TableName": "Users", "Segment": -1, "TotalSegments": 4}
|
||||
"""));
|
||||
assertEquals("1 validation error detected: Value '-1' at 'segment' failed to satisfy constraint: "
|
||||
+ "Member must have value greater than or equal to 0", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void scanRejectsTotalSegmentsBelowOne() {
|
||||
var ex = expectValidationException("Scan", json("""
|
||||
{"TableName": "Users", "Segment": 0, "TotalSegments": 0}
|
||||
"""));
|
||||
assertEquals("1 validation error detected: Value '0' at 'totalSegments' failed to satisfy constraint: "
|
||||
+ "Member must have value greater than or equal to 1", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void scanReportsTotalSegmentsBeforeSegmentWhenBothAreOutOfRange() {
|
||||
var ex = expectValidationException("Scan", json("""
|
||||
{"TableName": "Users", "Segment": -1, "TotalSegments": 0}
|
||||
"""));
|
||||
assertEquals("2 validation errors detected: "
|
||||
+ "Value '0' at 'totalSegments' failed to satisfy constraint: "
|
||||
+ "Member must have value greater than or equal to 1; "
|
||||
+ "Value '-1' at 'segment' failed to satisfy constraint: "
|
||||
+ "Member must have value greater than or equal to 0", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void scanAcceptsSegmentsAtTheBounds() throws Exception {
|
||||
createUsersTable("eu-west-1");
|
||||
|
||||
var response = handler.handle("Scan", json("""
|
||||
{"TableName": "Users", "Segment": 0, "TotalSegments": 1000000}
|
||||
"""), "eu-west-1");
|
||||
assertEquals(200, response.getStatus());
|
||||
}
|
||||
}
|
||||
@@ -624,6 +624,23 @@ class DynamoDbServiceTest {
|
||||
assertEquals(2, results.items().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryWithTheSortKeyValueOnTheLeft() {
|
||||
var region = "eu-west-1";
|
||||
createOrdersTable(region);
|
||||
service.putItem("Orders", item("customerId", "c1", "orderId", "o1"), region);
|
||||
service.putItem("Orders", item("customerId", "c1", "orderId", "o3"), region);
|
||||
|
||||
var exprValues = mapper.createObjectNode();
|
||||
exprValues.set(":pk", mapper.createObjectNode().put("S", "c1"));
|
||||
exprValues.set(":lo", mapper.createObjectNode().put("S", "o2"));
|
||||
|
||||
DynamoDbService.QueryResult results = service.query("Orders", null, exprValues,
|
||||
"customerId = :pk AND :lo <= orderId", null, null, region);
|
||||
assertEquals(1, results.items().size());
|
||||
assertEquals("o3", results.items().getFirst().get("orderId").get("S").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryWithBeginsWith() {
|
||||
String region = "eu-west-1";
|
||||
|
||||
Reference in new issue
Block a user