mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(dynamodb): trim number zeros in items written by UpdateItem (#6994)
This commit is contained in:
1 parent
e54b072afb
commit
cc76085322
4 files changed
+57
-47
No files matched your search
@@ -1490,7 +1490,7 @@ public class DynamoDbService {
|
||||
}
|
||||
}
|
||||
|
||||
DynamoDbSetOrder.sortSetsIn(item);
|
||||
item.setAll(DynamoDbNumberUtils.normalizeNumbersInItem(item));
|
||||
|
||||
// Reject any attempt to modify a key attribute
|
||||
validateKeyNotModified(table, key, item);
|
||||
|
||||
@@ -3,7 +3,6 @@ package io.github.hectorvent.floci.services.dynamodb;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -20,8 +19,6 @@ import java.util.function.Function;
|
||||
*/
|
||||
final class DynamoDbSetOrder {
|
||||
|
||||
private static final List<String> SET_TYPES = List.of("SS", "NS", "BS");
|
||||
|
||||
private record Keyed<K>(JsonNode member, K key) {}
|
||||
|
||||
private DynamoDbSetOrder() {}
|
||||
@@ -38,33 +35,6 @@ final class DynamoDbSetOrder {
|
||||
};
|
||||
}
|
||||
|
||||
/** Reorders every set in the attributes in place, through maps and lists. */
|
||||
static void sortSetsIn(JsonNode attributes) {
|
||||
if (attributes == null || !attributes.isObject()) {
|
||||
return;
|
||||
}
|
||||
for (JsonNode value : attributes) {
|
||||
sortSetsInValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
private static void sortSetsInValue(JsonNode value) {
|
||||
if (!(value instanceof ObjectNode object)) {
|
||||
return;
|
||||
}
|
||||
for (String setType : SET_TYPES) {
|
||||
if (object.has(setType)) {
|
||||
object.set(setType, sorted(setType, object.get(setType)));
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (object.has("M")) {
|
||||
sortSetsIn(object.get("M"));
|
||||
} else if (object.has("L")) {
|
||||
object.get("L").forEach(DynamoDbSetOrder::sortSetsInValue);
|
||||
}
|
||||
}
|
||||
|
||||
private static <K> ArrayNode sortedBy(JsonNode members, Function<String, K> key, Comparator<K> order) {
|
||||
List<Keyed<K>> keyed = new ArrayList<>(members.size());
|
||||
for (JsonNode member : members) {
|
||||
|
||||
@@ -3723,6 +3723,62 @@ class DynamoDbServiceTest {
|
||||
ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateItemSetArithmeticTrimsTrailingZeros() {
|
||||
String region = "eu-west-1";
|
||||
createUsersTable(region);
|
||||
ObjectNode exprValues = mapper.createObjectNode();
|
||||
exprValues.set(":a", attributeValue("N", "10"));
|
||||
exprValues.set(":b", attributeValue("N", "9.0"));
|
||||
|
||||
service.updateItem("Users", item("userId", "u1"), null,
|
||||
"SET n = :a - :b", null, exprValues, null, region);
|
||||
|
||||
assertEquals("1", service.getItem("Users", item("userId", "u1"), region).get("n").get("N").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateItemAddTrimsTrailingZeros() {
|
||||
String region = "eu-west-1";
|
||||
createUsersTable(region);
|
||||
ObjectNode existing = item("userId", "u1");
|
||||
existing.set("n", attributeValue("N", "1.5"));
|
||||
service.putItem("Users", existing, region);
|
||||
ObjectNode exprValues = mapper.createObjectNode();
|
||||
exprValues.set(":a", attributeValue("N", "1.5"));
|
||||
|
||||
service.updateItem("Users", item("userId", "u1"), null,
|
||||
"ADD n :a", null, exprValues, null, region);
|
||||
|
||||
assertEquals("3", service.getItem("Users", item("userId", "u1"), region).get("n").get("N").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateItemAddToAMissingAttributeTrimsTrailingZeros() {
|
||||
String region = "eu-west-1";
|
||||
createUsersTable(region);
|
||||
ObjectNode exprValues = mapper.createObjectNode();
|
||||
exprValues.set(":a", attributeValue("N", "1.50"));
|
||||
|
||||
service.updateItem("Users", item("userId", "u1"), null,
|
||||
"ADD n :a", null, exprValues, null, region);
|
||||
|
||||
assertEquals("1.5", service.getItem("Users", item("userId", "u1"), region).get("n").get("N").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateItemSetValueTrimsTrailingZeros() {
|
||||
String region = "eu-west-1";
|
||||
createUsersTable(region);
|
||||
ObjectNode exprValues = mapper.createObjectNode();
|
||||
exprValues.set(":a", attributeValue("N", "01.50"));
|
||||
|
||||
service.updateItem("Users", item("userId", "u1"), null,
|
||||
"SET n = :a", null, exprValues, null, region);
|
||||
|
||||
assertEquals("1.5", service.getItem("Users", item("userId", "u1"), region).get("n").get("N").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateItemSetParenthesizedArithmeticAppliesSubtraction() {
|
||||
String region = "eu-west-1";
|
||||
|
||||
@@ -2,7 +2,6 @@ package io.github.hectorvent.floci.services.dynamodb;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -37,21 +36,6 @@ class DynamoDbSetOrderTest {
|
||||
base64(0x02), base64(0x7f), base64(0x80), base64(0xc0), base64(0xff)), sorted);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sortSetsInReachesMapsAndLists() throws Exception {
|
||||
ObjectNode item = (ObjectNode) MAPPER.readTree("""
|
||||
{"pk":{"S":"a"},"ss":{"SS":["b","a"]},
|
||||
"m":{"M":{"deep":{"SS":["b","a",""]},"nums":{"NS":["10","9"]}}},
|
||||
"l":{"L":[{"NS":["2","1"]},{"M":{"inner":{"BS":["gA==","AQ=="]}}}]}}
|
||||
""");
|
||||
DynamoDbSetOrder.sortSetsIn(item);
|
||||
assertEquals(MAPPER.readTree("""
|
||||
{"pk":{"S":"a"},"ss":{"SS":["a","b"]},
|
||||
"m":{"M":{"deep":{"SS":["","a","b"]},"nums":{"NS":["9","10"]}}},
|
||||
"l":{"L":[{"NS":["1","2"]},{"M":{"inner":{"BS":["AQ==","gA=="]}}}]}}
|
||||
"""), item);
|
||||
}
|
||||
|
||||
@Test
|
||||
void membersThatDoNotParseKeepAPlace() {
|
||||
assertEquals(List.of("abc", "0", "1"), sorted("NS", List.of("1", "abc", "0")));
|
||||
|
||||
Reference in new issue
Block a user