mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): refuse BETWEEN bounds of different types (#9351)
A BETWEEN whose bounds have different types is now refused with DynamoDB's ValidationException in a FilterExpression, KeyConditionExpression, ConditionExpression and PartiQL statement, and PartiQL also refuses reversed bounds. Each check runs with the operand type checks, so the first failing condition in written order wins. The one-violation envelope moves to SmithyValidationErrors.inEnvelope. PutItem, UpdateItem and DeleteItem condition errors carry it, through the Step Functions tasks too, and TransactWriteItems ones no longer do, as on DynamoDB.
This commit is contained in:
1 parent
67a619d58e
commit
2f4b2c492a
9 files changed
+272
-32
No files matched your search
@@ -55,6 +55,14 @@ public final class SmithyValidationErrors {
|
||||
return validationException(List.of(violation));
|
||||
}
|
||||
|
||||
/** The error inside the one-violation envelope. Other errors, and messages already inside it, pass through. */
|
||||
public static AwsException inEnvelope(AwsException e) {
|
||||
if (!"ValidationException".equals(e.getErrorCode()) || e.getMessage().startsWith(message(List.of("")))) {
|
||||
return e;
|
||||
}
|
||||
return validationException(e.getMessage());
|
||||
}
|
||||
|
||||
/** The awsQuery protocol's code. */
|
||||
public static AwsException validationError(List<String> violations) {
|
||||
return exception("ValidationError", violations);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.floci.aws.services.dynamodb;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
@@ -1238,6 +1239,47 @@ public class DynamoDbPartiQLParser {
|
||||
}
|
||||
}
|
||||
|
||||
private void requireBetweenBounds(PVal lo, PVal hi) {
|
||||
String loType = typeCode(lo);
|
||||
String hiType = typeCode(hi);
|
||||
if (!ORDERED_TYPES.contains(loType) || !ORDERED_TYPES.contains(hiType)) {
|
||||
return;
|
||||
}
|
||||
String message = ExpressionEvaluator.betweenBoundsError(boundNode(lo), boundNode(hi),
|
||||
DynamoDbPartiQLParser::displayBound);
|
||||
if (message != null) {
|
||||
deferExpression(message);
|
||||
}
|
||||
}
|
||||
|
||||
private static JsonNode boundNode(PVal val) {
|
||||
return switch (val) {
|
||||
case PVal.Str s -> JsonNodeFactory.instance.objectNode().put("S", s.v());
|
||||
case PVal.Num n -> JsonNodeFactory.instance.objectNode().put("N", n.v());
|
||||
default -> ((PVal.Av) val).node();
|
||||
};
|
||||
}
|
||||
|
||||
private static String displayBound(JsonNode node) {
|
||||
String type = node.fieldNames().next();
|
||||
String text = node.get(type).asText();
|
||||
return switch (type) {
|
||||
case "N" -> new BigDecimal(text).toString();
|
||||
case "B" -> "[" + text + "]";
|
||||
default -> text;
|
||||
};
|
||||
}
|
||||
|
||||
static String betweenTypeMismatch(String lower, String upper) {
|
||||
return "The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: "
|
||||
+ lower + ", upper bound operand: " + upper;
|
||||
}
|
||||
|
||||
static String betweenReversed(String lower, String upper) {
|
||||
return "The BETWEEN operator requires upper bound to be greater than or equal to lower bound; "
|
||||
+ "lower bound operand: " + lower + ", upper bound operand: " + upper;
|
||||
}
|
||||
|
||||
static String incorrectOperandType(String op, String type) {
|
||||
return "Incorrect operand type for operator or function; "
|
||||
+ "operator or function: " + op + ", operand type: " + type;
|
||||
@@ -1288,6 +1330,7 @@ public class DynamoDbPartiQLParser {
|
||||
PVal hi = parseValue();
|
||||
requireOrdered("BETWEEN", lo);
|
||||
requireOrdered("BETWEEN", hi);
|
||||
requireBetweenBounds(lo, hi);
|
||||
return new Cond.Between(path, lo, hi);
|
||||
}
|
||||
if (peek().type() == TType.IN) {
|
||||
|
||||
@@ -3750,7 +3750,11 @@ public class DynamoDbService {
|
||||
|
||||
public void validateConditionExpression(String conditionExpression, JsonNode exprAttrNames,
|
||||
JsonNode exprAttrValues) {
|
||||
validateExpression(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
try {
|
||||
validateExpression(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
} catch (AwsException e) {
|
||||
throw SmithyValidationErrors.inEnvelope(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void validateFilterExpression(String filterExpression, JsonNode exprAttrNames, JsonNode exprAttrValues) {
|
||||
|
||||
@@ -16,6 +16,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@@ -572,30 +573,42 @@ final class ExpressionEvaluator {
|
||||
return invalidExpression(exprType, DynamoDbPartiQLParser.incorrectOperandType(operator, type));
|
||||
}
|
||||
|
||||
// AWS rejects a BETWEEN whose bounds are the wrong way round when it parses the
|
||||
// expression, rather than letting the condition fail at evaluation time.
|
||||
// AWS rejects a BETWEEN with bounds of different types or the wrong way round when it parses
|
||||
// the expression, rather than letting the condition fail at evaluation time.
|
||||
private static void validateBetween(BetweenExpr between, String exprType, JsonNode values) {
|
||||
JsonNode low = placeholderValue(between.low(), values);
|
||||
JsonNode high = placeholderValue(between.high(), values);
|
||||
String message = betweenBoundsError(placeholderValue(between.low(), values),
|
||||
placeholderValue(between.high(), values), ExpressionEvaluator::displayAttributeValue);
|
||||
if (message != null) {
|
||||
throw invalidExpression(exprType, message);
|
||||
}
|
||||
}
|
||||
|
||||
static String betweenBoundsError(JsonNode low, JsonNode high, Function<JsonNode, String> display) {
|
||||
if (low == null || high == null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
String lowType = low.fieldNames().next();
|
||||
if (!lowType.equals(high.fieldNames().next()) || compareBoundValues(low, high) <= 0) {
|
||||
return;
|
||||
try {
|
||||
if (!low.fieldNames().next().equals(high.fieldNames().next())) {
|
||||
return DynamoDbPartiQLParser.betweenTypeMismatch(displayWrapped(low, display),
|
||||
displayWrapped(high, display));
|
||||
}
|
||||
if (compareBoundValues(low, high) > 0) {
|
||||
return DynamoDbPartiQLParser.betweenReversed(displayWrapped(low, display),
|
||||
displayWrapped(high, display));
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
// Another check refuses a number that does not parse.
|
||||
}
|
||||
// AWS wraps the ConditionExpression form in its validation-error envelope, but reports
|
||||
// the FilterExpression and KeyConditionExpression forms on their own.
|
||||
String envelope = "ConditionExpression".equals(exprType) ? "1 validation error detected: " : "";
|
||||
throw new AwsException("ValidationException", envelope
|
||||
+ "Invalid " + exprType + ": The BETWEEN operator requires upper bound to be greater than "
|
||||
+ "or equal to lower bound; lower bound operand: " + displayAttributeValue(low)
|
||||
+ ", upper bound operand: " + displayAttributeValue(high), 400);
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String displayWrapped(JsonNode value, Function<JsonNode, String> display) {
|
||||
return "AttributeValue: {" + value.fieldNames().next() + ":" + display.apply(value) + "}";
|
||||
}
|
||||
|
||||
// DynamoDB orders strings by their UTF-8 bytes, which differs from Java's UTF-16
|
||||
// ordering above the basic plane: U+E000 sorts before U+10000 on AWS but after it here.
|
||||
private static int compareBoundValues(JsonNode low, JsonNode high) {
|
||||
static int compareBoundValues(JsonNode low, JsonNode high) {
|
||||
if (low.has("S") && high.has("S")) {
|
||||
return Arrays.compareUnsigned(
|
||||
low.get("S").asText().getBytes(StandardCharsets.UTF_8),
|
||||
@@ -629,8 +642,7 @@ final class ExpressionEvaluator {
|
||||
}
|
||||
|
||||
private static String displayAttributeValue(JsonNode value) {
|
||||
String type = value.fieldNames().next();
|
||||
return "AttributeValue: {" + type + ":" + value.get(type).asText() + "}";
|
||||
return value.get(value.fieldNames().next()).asText();
|
||||
}
|
||||
|
||||
private static void validateFunction(FunctionCallExpr f, String exprType,
|
||||
|
||||
@@ -893,18 +893,10 @@ public class NativeDynamoDbJsonHandler {
|
||||
checkUnusedEan(exprAttrNames, extractHashTokens(conditionExpression));
|
||||
checkUnusedEav(exprAttrValues, extractColonTokens(conditionExpression));
|
||||
} catch (AwsException e) {
|
||||
throw inValidationEnvelope(e);
|
||||
throw SmithyValidationErrors.inEnvelope(e);
|
||||
}
|
||||
}
|
||||
|
||||
// A size check and a BETWEEN error add the envelope themselves.
|
||||
private static AwsException inValidationEnvelope(AwsException e) {
|
||||
if (!"ValidationException".equals(e.getErrorCode()) || e.getMessage().startsWith(VALIDATION_ENVELOPE)) {
|
||||
return e;
|
||||
}
|
||||
return SmithyValidationErrors.validationException(e.getMessage());
|
||||
}
|
||||
|
||||
private static void requireNonEmptyMap(JsonNode map, String member) {
|
||||
if (map != null && map.isObject() && map.isEmpty()) {
|
||||
throw new AwsException("ValidationException", member + " must not be empty", 400);
|
||||
@@ -1090,7 +1082,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
checkUnusedEav(exprAttrValues, colonTokensAll);
|
||||
}
|
||||
} catch (AwsException e) {
|
||||
throw inValidationEnvelope(e);
|
||||
throw SmithyValidationErrors.inEnvelope(e);
|
||||
}
|
||||
|
||||
if (exprAttrValues != null) {
|
||||
|
||||
@@ -6036,6 +6036,16 @@ given()
|
||||
PutItem | {"TableName": "ConditionOrderTable", "Item": {"pk": {"S": "a"}}, "ConditionExpression": "nope(x)"} | 1 validation error detected: Invalid ConditionExpression: Invalid function name; function: nope
|
||||
PutItem | {"TableName": "ConditionOrderTable", "Item": {"pk": {"S": "a"}}, "ConditionExpression": "ATTRIBUTE_EXISTS(x)"} | 1 validation error detected: Invalid ConditionExpression: Invalid function name; function: ATTRIBUTE_EXISTS
|
||||
Query | {"TableName": "ConditionOrderTable", "KeyConditionExpression": "pk = :k AND BEGINS_WITH(sk, :c)", "ExpressionAttributeValues": {":k": {"S": "a"}, ":c": {"S": "b"}}} | Invalid KeyConditionExpression: Invalid function name; function: BEGINS_WITH
|
||||
Scan | {"TableName": "ConditionOrderTable", "FilterExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"S": "3"}}} | Invalid FilterExpression: The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {S:3}
|
||||
Scan | {"TableName": "NoSuchTable", "FilterExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"S": "3"}}} | Invalid FilterExpression: The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {S:3}
|
||||
Query | {"TableName": "ConditionOrderTable", "KeyConditionExpression": "pk = :k AND sk BETWEEN :a AND :b", "ExpressionAttributeValues": {":k": {"S": "k"}, ":a": {"N": "3"}, ":b": {"S": "3"}}} | Invalid KeyConditionExpression: The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {S:3}
|
||||
PutItem | {"TableName": "ConditionOrderTable", "Item": {"pk": {"S": "a"}}, "ConditionExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"S": "3"}}} | 1 validation error detected: Invalid ConditionExpression: The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {S:3}
|
||||
UpdateItem | {"TableName": "ConditionOrderTable", "Key": {"pk": {"S": "a"}}, "UpdateExpression": "SET q = :a", "ConditionExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"S": "3"}}} | 1 validation error detected: Invalid ConditionExpression: The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {S:3}
|
||||
DeleteItem | {"TableName": "ConditionOrderTable", "Key": {"pk": {"S": "a"}}, "ConditionExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"S": "3"}}} | 1 validation error detected: Invalid ConditionExpression: The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {S:3}
|
||||
TransactWriteItems | {"TransactItems": [{"ConditionCheck": {"TableName": "ConditionOrderTable", "Key": {"pk": {"S": "a"}}, "ConditionExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"S": "3"}}}}]} | Invalid ConditionExpression: The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {S:3}
|
||||
TransactWriteItems | {"TransactItems": [{"ConditionCheck": {"TableName": "ConditionOrderTable", "Key": {"pk": {"S": "a"}}, "ConditionExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"N": "1"}}}}]} | Invalid ConditionExpression: The BETWEEN operator requires upper bound to be greater than or equal to lower bound; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {N:1}
|
||||
TransactWriteItems | {"TransactItems": [{"Put": {"TableName": "ConditionOrderTable", "Item": {"pk": {"S": "a"}}, "ConditionExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"S": "3"}}}}]} | Invalid ConditionExpression: The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {S:3}
|
||||
TransactWriteItems | {"TransactItems": [{"Put": {"TableName": "ConditionOrderTable", "Item": {"pk": {"S": "a"}}, "ConditionExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"N": "1"}}}}]} | Invalid ConditionExpression: The BETWEEN operator requires upper bound to be greater than or equal to lower bound; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {N:1}
|
||||
""")
|
||||
void expressionErrorsComeFromTheFirstFailingConditionInWrittenOrder(String operation, String body, String message) {
|
||||
createOnDemandTableForGsiUpdates("ConditionOrderTable");
|
||||
|
||||
+125
@@ -19,6 +19,7 @@ import static io.floci.aws.testing.DynamoDbRequests.nestedMap;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
@@ -1052,6 +1053,130 @@ class DynamoDbPartiQLEdgeCaseIntegrationTest {
|
||||
getItem("grouped-or").body("Item", nullValue());
|
||||
}
|
||||
|
||||
private static final String NUMERIC_TABLE = "partiql-between-numeric-sk";
|
||||
private static final String MIXED_BOUNDS = "The BETWEEN operator requires same data type for lower and upper "
|
||||
+ "bounds; lower bound operand: ";
|
||||
private static final String REVERSED_BOUNDS = "The BETWEEN operator requires upper bound to be greater than "
|
||||
+ "or equal to lower bound; lower bound operand: ";
|
||||
private static final String BETWEEN_WHERE = " WHERE pk='between' AND sk='1' AND ";
|
||||
|
||||
@Test
|
||||
@Order(39)
|
||||
void refusesUpdateWithBetweenBoundsOfDifferentTypesOrReversed() {
|
||||
putItem("between", "\"y\":{\"N\":\"2\"}");
|
||||
String update = "UPDATE " + QUOTED_TABLE + " SET a=1" + BETWEEN_WHERE;
|
||||
|
||||
statement(update + "y BETWEEN 1 AND 'b'")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(MIXED_BOUNDS + "AttributeValue: {N:1}, upper bound operand: AttributeValue: {S:b}"));
|
||||
statement(update + "y BETWEEN 3 AND 1")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(REVERSED_BOUNDS + "AttributeValue: {N:3}, upper bound operand: AttributeValue: {N:1}"));
|
||||
statement(update + "NOT (y BETWEEN 3 AND 1)").statusCode(400);
|
||||
statement(update + "(y = 2 OR y BETWEEN 1 AND 'b')").statusCode(400);
|
||||
getItem("between").body("Item.a", nullValue());
|
||||
|
||||
statement(update + "y BETWEEN 2 AND 10").statusCode(200);
|
||||
statement(update + "y BETWEEN 2.0 AND 2").statusCode(200);
|
||||
getItem("between").body("Item.a.N", equalTo("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(40)
|
||||
void refusesSelectDeleteAndTheOtherPartiQLApisWithBetweenBounds() {
|
||||
String mixed = MIXED_BOUNDS + "AttributeValue: {N:1}, upper bound operand: AttributeValue: {S:b}";
|
||||
String select = "SELECT * FROM " + QUOTED_TABLE + " WHERE y BETWEEN 1 AND 'b'";
|
||||
String delete = "DELETE FROM " + QUOTED_TABLE + BETWEEN_WHERE + "y BETWEEN 1 AND 'b'";
|
||||
|
||||
statement(select).statusCode(400).body("message", equalTo(mixed));
|
||||
statement(delete).statusCode(400).body("message", equalTo(mixed));
|
||||
transaction(delete)
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo("Validation failed in TransactStatements[0]: " + mixed));
|
||||
request("DynamoDB_20120810.BatchExecuteStatement", "{\"Statements\":[{\"Statement\":" + json(delete) + "}]}")
|
||||
.statusCode(200)
|
||||
.body("Responses[0].Error.Code", equalTo("ValidationError"))
|
||||
.body("Responses[0].Error.Message", equalTo(mixed));
|
||||
getItem("between").body("Item.y.N", equalTo("2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(41)
|
||||
void showsBetweenBoundsTheWayDynamoDbDoes() {
|
||||
String update = "UPDATE " + QUOTED_TABLE + " SET a=2" + BETWEEN_WHERE;
|
||||
|
||||
statement(update + "y BETWEEN 1e2 AND 1")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(REVERSED_BOUNDS + "AttributeValue: {N:1E+2}, upper bound operand: AttributeValue: {N:1}"));
|
||||
statement(update + "y BETWEEN ? AND ?", "[{\"N\":\"3.50\"},{\"N\":\"1\"}]")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(REVERSED_BOUNDS + "AttributeValue: {N:3.50}, upper bound operand: AttributeValue: {N:1}"));
|
||||
statement(update + "y BETWEEN ? AND ?", "[{\"B\":\"AQI=\"},{\"S\":\"3\"}]")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(MIXED_BOUNDS + "AttributeValue: {B:[AQI=]}, upper bound operand: AttributeValue: {S:3}"));
|
||||
statement(update + "y BETWEEN ? AND ?", "[{\"B\":\"AQI=\"},{\"B\":\"AQ==\"}]")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(REVERSED_BOUNDS + "AttributeValue: {B:[AQI=]}, upper bound operand: AttributeValue: {B:[AQ==]}"));
|
||||
statement(update + "y BETWEEN 1 AND true")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("Incorrect operand type for operator or function; "
|
||||
+ "operator or function: BETWEEN, operand type: BOOL"));
|
||||
getItem("between").body("Item.a.N", equalTo("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(42)
|
||||
void reportsTheFirstFailingConditionInWrittenOrder() {
|
||||
String update = "UPDATE " + QUOTED_TABLE + " SET a=3" + BETWEEN_WHERE;
|
||||
String mixed = MIXED_BOUNDS + "AttributeValue: {N:1}, upper bound operand: AttributeValue: {S:b}";
|
||||
String reversed = REVERSED_BOUNDS + "AttributeValue: {N:3}, upper bound operand: AttributeValue: {N:1}";
|
||||
String beginsWith = "Incorrect operand type for operator or function; "
|
||||
+ "operator or function: begins_with, operand type: N";
|
||||
|
||||
statement(update + "y BETWEEN 1 AND 'b' AND y BETWEEN 3 AND 1").body("message", equalTo(mixed));
|
||||
statement(update + "y BETWEEN 3 AND 1 AND y BETWEEN 1 AND 'b'").body("message", equalTo(reversed));
|
||||
statement(update + "begins_with(z, 1) AND y BETWEEN 1 AND 'b'").body("message", equalTo(beginsWith));
|
||||
statement(update + "y BETWEEN 1 AND 'b' AND begins_with(z, 1)").body("message", equalTo(mixed));
|
||||
statement(update + "y BETWEEN 1 AND 'b' AND y = 1e999")
|
||||
.body("message", containsString("Number overflow"));
|
||||
statement(update + "y BETWEEN 1 AND 'b' AND nope(z)")
|
||||
.body("message", containsString("Unrecognized function: nope"));
|
||||
statement("UPDATE " + QUOTED_TABLE + " SET x = set_add(z, 1)" + BETWEEN_WHERE + "y BETWEEN 1 AND 'b'")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(mixed));
|
||||
getItem("between").body("Item.a.N", equalTo("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(43)
|
||||
void refusesBetweenBoundsOfASortKeyCondition() {
|
||||
request("DynamoDB_20120810.CreateTable", """
|
||||
{
|
||||
"TableName": "%s",
|
||||
"AttributeDefinitions": [
|
||||
{"AttributeName":"pk","AttributeType":"S"},
|
||||
{"AttributeName":"sk","AttributeType":"N"}
|
||||
],
|
||||
"KeySchema": [
|
||||
{"AttributeName":"pk","KeyType":"HASH"},
|
||||
{"AttributeName":"sk","KeyType":"RANGE"}
|
||||
],
|
||||
"BillingMode": "PAY_PER_REQUEST"
|
||||
}
|
||||
""".formatted(NUMERIC_TABLE))
|
||||
.statusCode(200);
|
||||
String select = "SELECT * FROM \"" + NUMERIC_TABLE + "\" WHERE pk = 'a' AND sk BETWEEN ";
|
||||
|
||||
statement(select + "1 AND 'b'")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(MIXED_BOUNDS + "AttributeValue: {N:1}, upper bound operand: AttributeValue: {S:b}"));
|
||||
statement(select + "3 AND 1")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(REVERSED_BOUNDS + "AttributeValue: {N:3}, upper bound operand: AttributeValue: {N:1}"));
|
||||
statement(select + "1 AND 3").statusCode(200);
|
||||
}
|
||||
|
||||
private static void putItem(String pk, String attributes) {
|
||||
putItem(pk, "1", attributes);
|
||||
}
|
||||
|
||||
@@ -706,11 +706,43 @@ class ExpressionEvaluatorTest {
|
||||
ExpressionEvaluator.validateExpression("n BETWEEN :hi AND :lo",
|
||||
"ConditionExpression", null, values));
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
assertEquals("1 validation error detected: Invalid ConditionExpression: The BETWEEN operator "
|
||||
assertEquals("Invalid ConditionExpression: The BETWEEN operator "
|
||||
+ "requires upper bound to be greater than or equal to lower bound; lower bound operand: "
|
||||
+ "AttributeValue: {N:10}, upper bound operand: AttributeValue: {N:1}", e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', textBlock = """
|
||||
ConditionExpression | {"N":"3"} | {"S":"3"} | {N:3} | {S:3}
|
||||
FilterExpression | {"S":"a"} | {"B":"AQI="} | {S:a} | {B:AQI=}
|
||||
KeyConditionExpression | {"B":"AQI="} | {"N":"1"} | {B:AQI=} | {N:1}
|
||||
""")
|
||||
void betweenBoundsOfDifferentTypesAreRejected(String exprType, String low, String high,
|
||||
String lowShown, String highShown) throws Exception {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
ObjectNode values = mapper.createObjectNode();
|
||||
values.set(":lo", mapper.readTree(low));
|
||||
values.set(":hi", mapper.readTree(high));
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () ->
|
||||
ExpressionEvaluator.validateExpression("n BETWEEN :lo AND :hi", exprType, null, values));
|
||||
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
assertEquals("Invalid " + exprType + ": The BETWEEN operator requires same data type for lower and "
|
||||
+ "upper bounds; lower bound operand: AttributeValue: " + lowShown
|
||||
+ ", upper bound operand: AttributeValue: " + highShown, e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void betweenWithAPathBoundIsNotCheckedForTypes() {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
ObjectNode values = mapper.createObjectNode();
|
||||
values.set(":lo", mapper.createObjectNode().put("N", "3"));
|
||||
|
||||
assertDoesNotThrow(() -> ExpressionEvaluator.validateExpression("n BETWEEN :lo AND other",
|
||||
"ConditionExpression", null, values));
|
||||
}
|
||||
|
||||
// Checked against real DynamoDB (us-east-1, 2026-09-07): U+E000 as the lower bound
|
||||
// and U+10000 as the upper bound is accepted, because DynamoDB orders strings by
|
||||
// their UTF-8 bytes. Java's UTF-16 ordering puts them the other way round.
|
||||
|
||||
+16
-2
@@ -204,8 +204,22 @@ class DynamoDbNativeBackendTest {
|
||||
});
|
||||
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertEquals("Invalid ConditionExpression: Invalid function name; function: " + function,
|
||||
refused.getMessage());
|
||||
assertEquals("1 validation error detected: Invalid ConditionExpression: Invalid function name; function: "
|
||||
+ function, refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void typedPutRefusesBetweenBoundsOfDifferentTypes() throws Exception {
|
||||
createTable("");
|
||||
JsonNode key = json("{\"id\":{\"S\":\"k\"}}");
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class, () -> backend.putItem(SCOPE, TABLE, key,
|
||||
"n BETWEEN :a AND :b", null, json("{\":a\":{\"N\":\"3\"},\":b\":{\"S\":\"3\"}}")));
|
||||
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertEquals("1 validation error detected: Invalid ConditionExpression: The BETWEEN operator requires "
|
||||
+ "same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, "
|
||||
+ "upper bound operand: AttributeValue: {S:3}", refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user