mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(dynamodb): remove several list indexes against the original list (#7989)
UpdateItem applied the paths of a REMOVE clause one after another, so removing L[0] shifted the list and L[2] then named a different element or none at all. Every action in an update expression addresses the item as it was before the update, so remove a list's higher indexes first. Closes #7971
This commit is contained in:
1 parent
b144879127
commit
3a5610d90d
2 files changed
+78
-1
No files matched your search
@@ -3716,6 +3716,7 @@ public class DynamoDbService {
|
||||
|
||||
private String applyRemoveClause(ObjectNode item, String clause, JsonNode exprAttrNames,
|
||||
List<String> touched) {
|
||||
List<String> paths = new ArrayList<>();
|
||||
while (!clause.isEmpty()) {
|
||||
String upper = clause.toUpperCase();
|
||||
if (upper.startsWith("SET ") || upper.startsWith("ADD ") || upper.startsWith("DELETE ")) {
|
||||
@@ -3741,11 +3742,37 @@ public class DynamoDbService {
|
||||
}
|
||||
|
||||
touched.add(attrPart);
|
||||
removeValueAtPath(item, attrPart, exprAttrNames);
|
||||
paths.add(attrPart);
|
||||
}
|
||||
// Every action addresses the item as it was before the update, so a list loses its later
|
||||
// indexes first and an earlier removal never shifts the element a later one names.
|
||||
paths.sort(Comparator.comparing((String path) -> parsePath(path, exprAttrNames), DynamoDbService::removalOrder));
|
||||
for (String path : paths) {
|
||||
removeValueAtPath(item, path, exprAttrNames);
|
||||
}
|
||||
return clause;
|
||||
}
|
||||
|
||||
/** Orders parsed paths so that, within one list, a higher index sorts first. */
|
||||
private static int removalOrder(List<Object> left, List<Object> right) {
|
||||
for (int i = 0; i < Math.min(left.size(), right.size()); i++) {
|
||||
Object a = left.get(i);
|
||||
Object b = right.get(i);
|
||||
int order;
|
||||
if (a instanceof Long x && b instanceof Long y) {
|
||||
order = Long.compare(y, x);
|
||||
} else if (a instanceof String x && b instanceof String y) {
|
||||
order = x.compareTo(y);
|
||||
} else {
|
||||
order = a instanceof Long ? -1 : 1;
|
||||
}
|
||||
if (order != 0) {
|
||||
return order;
|
||||
}
|
||||
}
|
||||
return Integer.compare(right.size(), left.size());
|
||||
}
|
||||
|
||||
private String applyAddClause(ObjectNode item, String clause,
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues, List<String> touched) {
|
||||
while (!clause.isEmpty()) {
|
||||
|
||||
@@ -2064,6 +2064,56 @@ class DynamoDbServiceTest {
|
||||
assertTrue(exception.getMessage().contains("List index is not within the allowable range"));
|
||||
assertTrue(exception.getMessage().contains("4294967295"));
|
||||
}
|
||||
@Test
|
||||
void removeOfSeveralListIndexesAddressesTheOriginalList() {
|
||||
assertEquals(List.of("one"), listAfter("REMOVE tags[0], tags[2]", "zero", "one", "two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeOfAnIndexInsideAnElementAddressesTheOriginalElement() {
|
||||
String region = "us-east-1";
|
||||
createUsersTable(region);
|
||||
ObjectNode item = mapper.createObjectNode();
|
||||
item.set("userId", attributeValue("S", "user-1"));
|
||||
ArrayNode inner = mapper.createArrayNode().add(attributeValue("S", "a")).add(attributeValue("S", "b"));
|
||||
ArrayNode outer = mapper.createArrayNode()
|
||||
.add(attributeValue("S", "first"))
|
||||
.add(mapper.createObjectNode().set("L", inner));
|
||||
item.set("nested", mapper.createObjectNode().set("L", outer));
|
||||
service.putItem("Users", item, region);
|
||||
ObjectNode key = mapper.createObjectNode();
|
||||
key.set("userId", attributeValue("S", "user-1"));
|
||||
|
||||
JsonNode updated = service.updateItem("Users", key, null,
|
||||
"REMOVE nested[0], nested[1][0]", null, null, "ALL_NEW", region).newItem();
|
||||
|
||||
JsonNode remaining = updated.get("nested").get("L");
|
||||
assertEquals(1, remaining.size());
|
||||
assertEquals("b", remaining.get(0).get("L").get(0).get("S").asText());
|
||||
assertEquals(1, remaining.get(0).get("L").size());
|
||||
}
|
||||
|
||||
private List<String> listAfter(String updateExpression, String... values) {
|
||||
String region = "us-east-1";
|
||||
createUsersTable(region);
|
||||
ObjectNode item = mapper.createObjectNode();
|
||||
item.set("userId", attributeValue("S", "user-1"));
|
||||
ArrayNode list = mapper.createArrayNode();
|
||||
for (String value : values) {
|
||||
list.add(attributeValue("S", value));
|
||||
}
|
||||
item.set("tags", mapper.createObjectNode().set("L", list));
|
||||
service.putItem("Users", item, region);
|
||||
ObjectNode key = mapper.createObjectNode();
|
||||
key.set("userId", attributeValue("S", "user-1"));
|
||||
|
||||
JsonNode updated = service.updateItem("Users", key, null, updateExpression,
|
||||
null, null, "ALL_NEW", region).newItem();
|
||||
List<String> remaining = new ArrayList<>();
|
||||
updated.get("tags").get("L").forEach(element -> remaining.add(element.get("S").asText()));
|
||||
return remaining;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test REMOVE with nested map paths (e.g. "ratings.foo").
|
||||
* Reproduces GitHub issue #402: REMOVE on a map key succeeds but data is unchanged.
|
||||
|
||||
Reference in new issue
Block a user