mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): refuse an UpdateExpression operand that reads an absent attribute (#8266)
DynamoDB refuses the whole update when a path in a SET value reads nothing. That covers a missing attribute or map key, a path through a scalar, a list index past the end, and an if_not_exists whose path and fallback are both absent. It reads the operands of a sum or list_append one by one, so the first absent operand or the first of the wrong type gives the error. A sum operand of the wrong type gets the same message as list_append and ADD. In TransactWriteItems both errors cancel the transaction with a ValidationError reason.
This commit is contained in:
1 parent
f087599883
commit
287c44b8f8
3 files changed
+142
-27
No files matched your search
@@ -4075,6 +4075,105 @@ class DynamoDbServiceTest {
|
||||
assertEquals(mapper.readTree(expected), service.getItem("Users", item("userId", "u1"), region).get(attribute));
|
||||
}
|
||||
|
||||
private static final String ABSENT_ATTRIBUTE =
|
||||
"The provided expression refers to an attribute that does not exist in the item";
|
||||
private static final String INCORRECT_TYPE = "An operand in the update expression has an incorrect data type";
|
||||
|
||||
private ObjectNode putOperandItem(String region) throws IOException {
|
||||
createUsersTable(region);
|
||||
ObjectNode stored = (ObjectNode) mapper.readTree("""
|
||||
{"userId":{"S":"u1"},"n":{"N":"10"},"k":{"L":[{"N":"4"},{"N":"5"}]},
|
||||
"m":{"M":{"x":{"N":"1"}}},"s":{"S":"str"}}""");
|
||||
service.putItem("Users", stored, region);
|
||||
return stored;
|
||||
}
|
||||
|
||||
private ObjectNode operandValues() throws IOException {
|
||||
return (ObjectNode) mapper.readTree("""
|
||||
{":v":{"N":"2"},":l":{"L":[{"S":"x"}]}}""");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', textBlock = """
|
||||
SET a = z | absent
|
||||
SET a = (z) | absent
|
||||
SET a = m.y | absent
|
||||
SET a = m.x.y | absent
|
||||
SET a = n.x | absent
|
||||
SET a = k[5] | absent
|
||||
SET a = m[0] | absent
|
||||
SET a = if_not_exists(z, y) | absent
|
||||
SET a = if_not_exists(z, if_not_exists(y, x)) | absent
|
||||
SET a = if_not_exists(m.y, m.z) | absent
|
||||
SET a = z + :v | absent
|
||||
SET a = :v + z | absent
|
||||
SET a = if_not_exists(z, y) + :v | absent
|
||||
SET l = list_append(z, :l) | absent
|
||||
SET l = list_append(if_not_exists(z, y), :l) | absent
|
||||
SET b = :v, a = z | absent
|
||||
SET a = z REMOVE n | absent
|
||||
SET a = z + s | absent
|
||||
SET a = s + :v | type
|
||||
SET a = s + z | type
|
||||
SET l = list_append(s, :l) | type
|
||||
SET l = list_append(s, z) | type
|
||||
ADD s :v | type
|
||||
""")
|
||||
void updateItemRefusesAnOperandTheItemCannotServe(String expression, String error) throws IOException {
|
||||
String region = "eu-west-1";
|
||||
ObjectNode stored = putOperandItem(region);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () -> service.updateItem("Users", item("userId", "u1"),
|
||||
null, expression, null, operandValues(), null, region));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("absent".equals(error) ? ABSENT_ATTRIBUTE : INCORRECT_TYPE, ex.getMessage());
|
||||
assertEquals(stored, service.getItem("Users", item("userId", "u1"), region));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateItemRefusingAnAbsentOperandCreatesNoItem() {
|
||||
String region = "eu-west-1";
|
||||
createUsersTable(region);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () -> service.updateItem("Users", item("userId", "new"),
|
||||
null, "SET a = z", null, null, null, region));
|
||||
assertEquals(ABSENT_ATTRIBUTE, ex.getMessage());
|
||||
assertNull(service.getItem("Users", item("userId", "new"), region));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', textBlock = """
|
||||
SET a = z | absent
|
||||
SET a = if_not_exists(z, y) | absent
|
||||
SET a = s + :v | type
|
||||
SET l = list_append(s, :l) | type
|
||||
ADD s :v | type
|
||||
""")
|
||||
void transactWriteItemsCancelsAnUpdateWhoseOperandTheItemCannotServe(String expression, String error)
|
||||
throws IOException {
|
||||
String region = "eu-west-1";
|
||||
ObjectNode stored = putOperandItem(region);
|
||||
JsonNode put = mapper.readTree("""
|
||||
{"Put":{"TableName":"Users","Item":{"userId":{"S":"other"}}}}""");
|
||||
ObjectNode update = mapper.createObjectNode();
|
||||
ObjectNode member = update.putObject("Update");
|
||||
member.put("TableName", "Users");
|
||||
member.set("Key", item("userId", "u1"));
|
||||
member.put("UpdateExpression", expression);
|
||||
if (expression.contains(":")) {
|
||||
member.set("ExpressionAttributeValues", operandValues());
|
||||
}
|
||||
|
||||
TransactionCanceledException ex = assertThrows(TransactionCanceledException.class,
|
||||
() -> service.transactWriteItems(List.of(put, update), region));
|
||||
List<TransactionCanceledException.CancellationReason> reasons = ex.getCancellationReasons();
|
||||
assertEquals("", reasons.get(0).code());
|
||||
assertEquals("ValidationError", reasons.get(1).code());
|
||||
assertEquals("absent".equals(error) ? ABSENT_ATTRIBUTE : INCORRECT_TYPE, reasons.get(1).message());
|
||||
assertEquals(stored, service.getItem("Users", item("userId", "u1"), region));
|
||||
assertNull(service.getItem("Users", item("userId", "other"), region));
|
||||
}
|
||||
|
||||
@Test
|
||||
void transactWriteItemsReplayWithSameTokenAndBodyIsNoOp() {
|
||||
// ClientRequestToken contract: same token + same body → no re-application of writes.
|
||||
|
||||
Reference in new issue
Block a user