fix(dynamodb): store map keys in code point order on plain API writes (#9227)

AWS sorts the keys of every map in a PutItem, BatchWriteItem or TransactWriteItems item and in update values. PartiQL literals keep their written order. An update sorts each map on its document path again.

Step Functions dynamodb:putItem and dynamodb:updateItem tasks and IoT dynamoDBv2 rule actions are plain PutItem and UpdateItem calls on AWS, so their maps are stored in code point order too.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-22 20:56:04 -04:00
1 parent 77c11493e7
commit 4d481ebccb
9 files changed
+705 -16

No files matched your search

@@ -0,0 +1,300 @@
package io.floci.aws.services.dynamodb;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.UncheckedIOException;
import static io.floci.aws.services.dynamodb.DynamoDbAttributeOrderFixture.call;
import static io.floci.aws.services.dynamodb.DynamoDbAttributeOrderFixture.createTable;
import static io.floci.aws.services.dynamodb.DynamoDbAttributeOrderFixture.deleteTable;
import static io.floci.aws.services.dynamodb.DynamoDbAttributeOrderFixture.order;
import static io.floci.aws.testing.DynamoDbRequests.ddb;
import static org.junit.jupiter.api.Assertions.assertEquals;
@QuarkusTest
class DynamoDbMapKeyOrderIntegrationTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final String ZBK = "{\"M\":{\"z\":{\"N\":\"1\"},\"b\":{\"N\":\"2\"},\"k\":{\"N\":\"3\"}}}";
private static final String UNSORTED_ITEM = "{'pk':'k','m':{'z':1,'k':{'z':1,'b':2,'a':3},'j':{'z':1,'b':2,'a':3}},"
+ "'l':[{'z':1,'b':2},{'z':1,'b':2}],'b':{'z':1,'b':2},'n':1}";
private String table;
@BeforeAll
static void configure() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@BeforeEach
void createTestTable() {
table = createTable("map-key-order");
}
@AfterEach
void deleteTestTable() {
deleteTable(table);
}
private JsonNode stored() {
byte[] body = ddb("GetItem", "{\"TableName\":\"" + table + "\",\"ConsistentRead\":true,"
+ "\"Key\":{\"pk\":{\"S\":\"k\"}}}").extract().asByteArray();
try {
return MAPPER.readTree(body).get("Item");
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private static String mapOrder(JsonNode node) {
return order(node.get("M"));
}
private void put(String item) {
call("PutItem", asciiJson("{\"TableName\":\"" + table + "\",\"Item\":" + item + "}"));
}
// The test client sends a String body as ISO-8859-1.
private static String asciiJson(String json) {
StringBuilder ascii = new StringBuilder();
json.chars().forEach(c -> {
if (c < 128) {
ascii.append((char) c);
} else {
ascii.append(String.format("\\u%04x", c));
}
});
return ascii.toString();
}
private void update(String expression, String values) {
String valuesMember = values == null ? "" : ",\"ExpressionAttributeValues\":" + values;
call("UpdateItem", "{\"TableName\":\"" + table + "\",\"Key\":{\"pk\":{\"S\":\"k\"}},"
+ "\"UpdateExpression\":\"" + expression + "\"" + valuesMember + "}");
}
private void partiQL(String statement) {
ObjectNode body = MAPPER.createObjectNode();
body.put("Statement", statement.replace("$T", "\"" + table + "\""));
call("ExecuteStatement", body.toString());
}
private void insertUnsorted() {
partiQL("INSERT INTO $T VALUE " + UNSORTED_ITEM);
}
@Test
void putItemStoresNestedMapsInCodePointOrder() {
put("{\"pk\":{\"S\":\"k\"},\"m\":{\"M\":{\"z\":" + ZBK + ",\"b\":{\"L\":[" + ZBK + "]},\"k\":" + ZBK + "}}}");
JsonNode m = stored().get("m");
assertEquals("b,k,z", mapOrder(m));
assertEquals("b,k,z", mapOrder(m.get("M").get("z")));
assertEquals("b,k,z", mapOrder(m.get("M").get("b").get("L").get(0)));
}
@Test
void putItemOrdersKeysByCodePointNotByUtf16Unit() {
put("{\"pk\":{\"S\":\"k\"},\"m\":{\"M\":{\"😀\":{\"S\":\"x\"},\"₩\":{\"S\":\"x\"},"
+ "\"é\":{\"S\":\"x\"},\"aa\":{\"S\":\"x\"},\"a b\":{\"S\":\"x\"},\"a\":{\"S\":\"x\"},"
+ "\"_\":{\"S\":\"x\"},\"B\":{\"S\":\"x\"},\"A\":{\"S\":\"x\"},\"1\":{\"S\":\"x\"}}}}");
assertEquals("1,A,B,_,a,a b,aa,é,₩,😀", mapOrder(stored().get("m")));
}
@Test
void batchWriteItemStoresMapsInCodePointOrder() {
call("BatchWriteItem", "{\"RequestItems\":{\"" + table + "\":[{\"PutRequest\":{\"Item\":"
+ "{\"pk\":{\"S\":\"k\"},\"m\":" + ZBK + "}}}]}}");
assertEquals("b,k,z", mapOrder(stored().get("m")));
}
@Test
void transactWriteItemsPutStoresMapsInCodePointOrder() {
call("TransactWriteItems", "{\"TransactItems\":[{\"Put\":{\"TableName\":\"" + table + "\",\"Item\":"
+ "{\"pk\":{\"S\":\"k\"},\"m\":" + ZBK + "}}}]}");
assertEquals("b,k,z", mapOrder(stored().get("m")));
}
@Test
void transactWriteItemsUpdateStoresMapsInCodePointOrder() {
put("{\"pk\":{\"S\":\"k\"}}");
call("TransactWriteItems", "{\"TransactItems\":[{\"Update\":{\"TableName\":\"" + table + "\","
+ "\"Key\":{\"pk\":{\"S\":\"k\"}},\"UpdateExpression\":\"SET a = :v\","
+ "\"ExpressionAttributeValues\":{\":v\":" + ZBK + "}}}]}");
assertEquals("b,k,z", mapOrder(stored().get("a")));
}
@Test
void updateItemSetStoresMapsInCodePointOrder() {
put("{\"pk\":{\"S\":\"k\"},\"m\":{\"M\":{}}}");
update("SET a = :v, m.x = :v", "{\":v\":" + ZBK + "}");
JsonNode item = stored();
assertEquals("b,k,z", mapOrder(item.get("a")));
assertEquals("b,k,z", mapOrder(item.get("m").get("M").get("x")));
}
@Test
void attributeUpdatesPutStoresMapsInCodePointOrder() {
put("{\"pk\":{\"S\":\"k\"}}");
call("UpdateItem", "{\"TableName\":\"" + table + "\",\"Key\":{\"pk\":{\"S\":\"k\"}},"
+ "\"AttributeUpdates\":{\"a\":{\"Action\":\"PUT\",\"Value\":" + ZBK + "}}}");
assertEquals("b,k,z", mapOrder(stored().get("a")));
}
@Test
void listAppendStoresMapsInCodePointOrder() {
put("{\"pk\":{\"S\":\"k\"},\"l\":{\"L\":[]}}");
update("SET l = list_append(l, :v)", "{\":v\":{\"L\":[" + ZBK + "]}}");
assertEquals("b,k,z", mapOrder(stored().get("l").get("L").get(0)));
}
@Test
void ifNotExistsStoresMapsInCodePointOrder() {
put("{\"pk\":{\"S\":\"k\"}}");
update("SET a = if_not_exists(a, :v)", "{\":v\":" + ZBK + "}");
assertEquals("b,k,z", mapOrder(stored().get("a")));
}
@Test
void partiQLInsertKeepsTheWrittenOrder() {
partiQL("INSERT INTO $T VALUE {'pk':'k','m':{'z':1,'b':{'z':1,'k':2,'b':3},'k':3}}");
JsonNode m = stored().get("m");
assertEquals("z,b,k", mapOrder(m));
assertEquals("z,k,b", mapOrder(m.get("M").get("b")));
}
@Test
void batchExecuteStatementInsertKeepsTheWrittenOrder() {
ObjectNode statement = MAPPER.createObjectNode();
statement.put("Statement", "INSERT INTO \"" + table + "\" VALUE {'pk':'k','m':{'z':1,'b':2,'k':3}}");
call("BatchExecuteStatement", "{\"Statements\":[" + statement + "]}");
assertEquals("z,b,k", mapOrder(stored().get("m")));
}
@Test
void executeTransactionInsertKeepsTheWrittenOrder() {
ObjectNode statement = MAPPER.createObjectNode();
statement.put("Statement", "INSERT INTO \"" + table + "\" VALUE {'pk':'k','m':{'z':1,'b':2,'k':3}}");
call("ExecuteTransaction", "{\"TransactStatements\":[" + statement + "]}");
assertEquals("z,b,k", mapOrder(stored().get("m")));
}
@Test
void partiQLUpdateSetKeepsTheWrittenOrderOfTheLiteral() {
partiQL("INSERT INTO $T VALUE {'pk':'k'}");
partiQL("UPDATE $T SET m = {'z':1,'b':2,'k':3} WHERE pk = 'k'");
assertEquals("z,b,k", mapOrder(stored().get("m")));
}
@Test
void partiQLUpdateSetOfNestedKeyKeepsTheLiteralAndSortsTheMap() {
insertUnsorted();
partiQL("UPDATE $T SET m.x = {'q':1,'d':2,'a':3} WHERE pk = 'k'");
JsonNode m = stored().get("m");
assertEquals("j,k,x,z", mapOrder(m));
assertEquals("q,d,a", mapOrder(m.get("M").get("x")));
}
@Test
void partiQLRemoveSortsTheMap() {
insertUnsorted();
partiQL("UPDATE $T REMOVE m.q WHERE pk = 'k'");
assertEquals("j,k,z", mapOrder(stored().get("m")));
}
@Test
void setOnAMapKeySortsItAndLeavesNestedMapsAlone() {
insertUnsorted();
update("SET m.x = :v", "{\":v\":{\"N\":\"1\"}}");
JsonNode m = stored().get("m");
assertEquals("j,k,x,z", mapOrder(m));
assertEquals("z,b,a", mapOrder(m.get("M").get("k")));
}
@Test
void setBelowANestedMapSortsEveryMapOnThePath() {
insertUnsorted();
update("SET m.k.w = :v", "{\":v\":{\"N\":\"1\"}}");
JsonNode m = stored().get("m");
assertEquals("j,k,z", mapOrder(m));
assertEquals("a,b,w,z", mapOrder(m.get("M").get("k")));
assertEquals("z,b,a", mapOrder(m.get("M").get("j")));
}
@Test
void setBelowAListElementSortsOnlyThatElement() {
insertUnsorted();
update("SET l[0].e = :v", "{\":v\":{\"N\":\"1\"}}");
JsonNode list = stored().get("l").get("L");
assertEquals("b,e,z", mapOrder(list.get(0)));
assertEquals("z,b", mapOrder(list.get(1)));
}
@Test
void copyFromAPathKeepsTheSourceOrder() {
insertUnsorted();
update("SET a = b", null);
JsonNode item = stored();
assertEquals("z,b", mapOrder(item.get("a")));
}
@Test
void laterPathThroughACopyDoesNotReorderTheSource() {
insertUnsorted();
update("SET a = b, c = b", null);
update("SET a.e = :v", "{\":v\":{\"N\":\"1\"}}");
JsonNode item = stored();
assertEquals("b,e,z", mapOrder(item.get("a")));
assertEquals("z,b", mapOrder(item.get("b")));
assertEquals("z,b", mapOrder(item.get("c")));
}
@Test
void copyOfANestedMapKeepsItsOrderWhileTheMapIsSorted() {
insertUnsorted();
update("SET m.k = m.j", null);
JsonNode m = stored().get("m");
assertEquals("j,k,z", mapOrder(m));
assertEquals("z,b,a", mapOrder(m.get("M").get("k")));
}
@Test
void updateThatTouchesNoMapLeavesEveryMapAsItWas() {
insertUnsorted();
update("ADD n :one", "{\":one\":{\"N\":\"1\"}}");
JsonNode item = stored();
assertEquals("z,k,j", mapOrder(item.get("m")));
assertEquals("z,b,a", mapOrder(item.get("m").get("M").get("k")));
assertEquals("z,b", mapOrder(item.get("b")));
}
}
@@ -0,0 +1,136 @@
package io.floci.aws.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 static io.floci.aws.services.dynamodb.DynamoDbAttributeOrderFixture.order;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
class DynamoDbMapOrderTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static JsonNode json(String text) throws Exception {
return MAPPER.readTree(text);
}
@Test
void keysSortByCodePointNotByUtf16Unit() throws Exception {
JsonNode item = json("""
{"m":{"M":{"😀":{"S":"x"},"₩":{"S":"x"},"é":{"S":"x"},"aa":{"S":"x"},
"a b":{"S":"x"},"a":{"S":"x"},"_":{"S":"x"},"B":{"S":"x"},"A":{"S":"x"},"1":{"S":"x"}}}}""");
JsonNode sorted = DynamoDbMapOrder.sortedItem(item);
assertEquals("1,A,B,_,a,a b,aa,é,₩,😀", order(sorted.get("m").get("M")));
}
@Test
void mapInsideListInsideMapIsSorted() throws Exception {
JsonNode item = json("""
{"m":{"M":{"z":{"S":"1"},"l":{"L":[{"M":{"z":{"S":"1"},"b":{"S":"2"}}},{"S":"s"}]},"b":{"S":"2"}}}}""");
JsonNode sorted = DynamoDbMapOrder.sortedItem(item);
JsonNode m = sorted.get("m").get("M");
assertEquals("b,l,z", order(m));
assertEquals("b,z", order(m.get("l").get("L").get(0).get("M")));
}
@Test
void topLevelOrderIsKept() throws Exception {
JsonNode item = json("{\"z\":{\"S\":\"1\"},\"a\":{\"S\":\"2\"},\"m\":{\"M\":{\"y\":{\"S\":\"1\"},\"x\":{\"S\":\"2\"}}}}");
JsonNode sorted = DynamoDbMapOrder.sortedItem(item);
assertEquals("z,a,m", order(sorted));
assertEquals("x,y", order(sorted.get("m").get("M")));
}
@Test
void inputIsNotChanged() throws Exception {
JsonNode item = json("{\"m\":{\"M\":{\"y\":{\"S\":\"1\"},\"x\":{\"S\":\"2\"}}}}");
DynamoDbMapOrder.sortedItem(item);
assertEquals("y,x", order(item.get("m").get("M")));
}
@Test
void itemWithoutMapIsReturnedAsIs() throws Exception {
JsonNode item = json("{\"b\":{\"S\":\"1\"},\"a\":{\"L\":[{\"N\":\"1\"}]}}");
assertSame(item, DynamoDbMapOrder.sortedItem(item));
}
@Test
void setsAreNotChanged() throws Exception {
JsonNode item = json("{\"m\":{\"M\":{\"b\":{\"SS\":[\"z\",\"a\"]},\"a\":{\"NS\":[\"2\",\"1\"]}}}}");
JsonNode sorted = DynamoDbMapOrder.sortedItem(item);
JsonNode m = sorted.get("m").get("M");
assertEquals("a,b", order(m));
assertEquals("[\"z\",\"a\"]", m.get("b").get("SS").toString());
assertEquals("[\"2\",\"1\"]", m.get("a").get("NS").toString());
}
@Test
void valueIsSortedAtAnyDepth() throws Exception {
JsonNode values = json("{\":v\":{\"L\":[{\"M\":{\"z\":{\"S\":\"1\"},\"a\":{\"M\":{\"y\":{\"S\":\"1\"},"
+ "\"x\":{\"S\":\"2\"}}}}}]}}");
JsonNode sorted = DynamoDbMapOrder.sortedItem(values);
JsonNode map = sorted.get(":v").get("L").get(0).get("M");
assertEquals("a,z", order(map));
assertEquals("x,y", order(map.get("a").get("M")));
}
@Test
void attributeUpdatesSortEachValue() throws Exception {
JsonNode updates = json("""
{"m":{"Action":"PUT","Value":{"M":{"z":{"S":"1"},"b":{"S":"2"}}}},"n":{"Action":"ADD","Value":{"N":"1"}}}""");
JsonNode sorted = DynamoDbMapOrder.sortedAttributeUpdates(updates);
assertEquals("b,z", order(sorted.get("m").get("Value").get("M")));
assertEquals("PUT", sorted.get("m").get("Action").asText());
assertEquals("z,b", order(updates.get("m").get("Value").get("M")));
}
@Test
void sortKeysReordersOneMapInPlace() throws Exception {
ObjectNode map = (ObjectNode) json("{\"z\":{\"M\":{\"y\":{\"S\":\"1\"},\"x\":{\"S\":\"2\"}}},\"a\":{\"S\":\"1\"}}");
JsonNode nested = map.get("z");
DynamoDbMapOrder.sortKeys(map);
assertEquals("a,z", order(map));
assertSame(nested, map.get("z"));
assertEquals("y,x", order(map.get("z").get("M")));
}
@Test
void withSortedMemberSortsTheMemberAndKeepsTheRest() throws Exception {
JsonNode request = json("""
{"Put":{"Item":{"m":{"M":{"z":{"S":"1"},"b":{"S":"2"}}}},"TableName":"t"},"Other":1}""");
JsonNode result = DynamoDbMapOrder.withSortedMember(request, "Put", "Item");
assertEquals("b,z", order(result.get("Put").get("Item").get("m").get("M")));
assertEquals("t", result.get("Put").get("TableName").asText());
assertEquals(1, result.get("Other").asInt());
assertEquals("z,b", order(request.get("Put").get("Item").get("m").get("M")));
}
@Test
void withSortedMemberReturnsTheRequestWhenTheOperationIsAbsent() throws Exception {
JsonNode request = json("{\"Delete\":{\"Key\":{\"id\":{\"S\":\"k\"}}}}");
assertSame(request, DynamoDbMapOrder.withSortedMember(request, "Put", "Item"));
}
}
@@ -184,6 +184,33 @@ class DynamoDbNativeBackendTest {
assertEquals(json("{\"id\":{\"S\":\"k\"},\"n\":{\"N\":\"5\"}}"), backend.getItem(SCOPE, TABLE, key));
}
@Test
void typedWritesStoreMapKeysInCodePointOrder() throws Exception {
backend.createTable(SCOPE, TABLE, List.of(new KeySchemaElement("id", "HASH")),
List.of(new AttributeDefinition("id", "S")), 5L, 5L, List.of(), List.of());
JsonNode key = json("{\"id\":{\"S\":\"k\"}}");
backend.putItem(SCOPE, TABLE,
json("{\"id\":{\"S\":\"k\"},\"m\":{\"M\":{\"z\":{\"S\":\"1\"},\"b\":{\"S\":\"2\"}}}}"),
null, null, null);
assertEquals("b,z", keys(backend.getItem(SCOPE, TABLE, key).get("m").get("M")));
backend.updateItem(SCOPE, TABLE, key, null, "SET p = :p", null,
json("{\":p\":{\"M\":{\"z\":{\"S\":\"1\"},\"b\":{\"S\":\"2\"}}}}"), "NONE", null);
assertEquals("b,z", keys(backend.getItem(SCOPE, TABLE, key).get("p").get("M")));
backend.updateItem(SCOPE, TABLE, key,
json("{\"q\":{\"Action\":\"PUT\",\"Value\":{\"M\":{\"z\":{\"S\":\"1\"},\"b\":{\"S\":\"2\"}}}}}"),
null, null, null, "NONE", null);
assertEquals("b,z", keys(backend.getItem(SCOPE, TABLE, key).get("q").get("M")));
}
private static String keys(JsonNode node) {
List<String> names = new ArrayList<>();
node.fieldNames().forEachRemaining(names::add);
return String.join(",", names);
}
@Test
void storedTagsAreTheTablesForItAndItsIndexesInTheArnsAccountAndRegionAndTaggingPutsANewMap() throws Exception {
call(Api.DYNAMODB, "CreateTable", json("""