Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(dynamodb): fix FilterExpression for BOOL types, List/Set contains, and nested attribute paths (#137)

nakano16180453555a12474
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java
index 41c8a0c5dd..cd823efd97 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java
@@ -1072,15 +1072,13 @@ public class DynamoDbService {
if (condLower.startsWith("attribute_exists")) {
String attr = extractFunctionArg(condition);
- String attrName = resolveAttributeName(attr, exprAttrNames);
- // If item is null, attribute cannot exist
- return item != null && item.has(attrName);
+ String resolvedPath = resolveAttributePath(attr, exprAttrNames);
+ return item != null && resolveNestedAttribute(item, resolvedPath) != null;
}
if (condLower.startsWith("attribute_not_exists")) {
String attr = extractFunctionArg(condition);
- String attrName = resolveAttributeName(attr, exprAttrNames);
- // If item is null, attribute definitely doesn't exist
- return item == null || !item.has(attrName);
+ String resolvedPath = resolveAttributePath(attr, exprAttrNames);
+ return item == null || resolveNestedAttribute(item, resolvedPath) == null;
}
if (condLower.startsWith("begins_with")) {
String[] args = extractFunctionArgs(condition);
@@ -1096,9 +1094,54 @@ public class DynamoDbService {
String[] args = extractFunctionArgs(condition);
if (args.length == 2) {
String attrName = resolveAttributeName(args[0], exprAttrNames);
- String substring = resolveExprValue(args[1], exprAttrValues);
- String actual = item != null ? extractScalarValue(item.get(attrName)) : null;
- return actual != null && substring != null && actual.contains(substring);
+ if (item == null) return false;
+ JsonNode attrNode = item.get(attrName);
+ if (attrNode == null) return false;
+ // Resolve the raw AttributeValue node for type-aware comparisons
+ JsonNode searchAttrValue = exprAttrValues != null
+ ? exprAttrValues.get(args[1].trim()) : null;
+ if (searchAttrValue == null) return false;
+ // List type: type-aware element membership check
+ if (attrNode.has("L")) {
+ for (JsonNode element : attrNode.get("L")) {
+ if (attributeValuesEqual(element, searchAttrValue)) return true;
+ }
+ return false;
+ }
+ // SS (String Set): operand must be S type
+ if (attrNode.has("SS")) {
+ if (!searchAttrValue.has("S")) return false;
+ String target = searchAttrValue.get("S").asText();
+ for (JsonNode element : attrNode.get("SS")) {
+ if (target.equals(element.asText())) return true;
+ }
+ return false;
+ }
+ // NS (Number Set): operand must be N type, compare numerically
+ if (attrNode.has("NS")) {
+ if (!searchAttrValue.has("N")) return false;
+ try {
+ java.math.BigDecimal target = new java.math.BigDecimal(searchAttrValue.get("N").asText());
+ for (JsonNode element : attrNode.get("NS")) {
+ if (target.compareTo(new java.math.BigDecimal(element.asText())) == 0) return true;
+ }
+ } catch (NumberFormatException ignored) {}
+ return false;
+ }
+ // BS (Binary Set): operand must be B type
+ if (attrNode.has("BS")) {
+ if (!searchAttrValue.has("B")) return false;
+ String target = searchAttrValue.get("B").asText();
+ for (JsonNode element : attrNode.get("BS")) {
+ if (target.equals(element.asText())) return true;
+ }
+ return false;
+ }
+ // String type: operand must be S type, check substring
+ if (attrNode.has("S") && searchAttrValue.has("S")) {
+ return attrNode.get("S").asText().contains(searchAttrValue.get("S").asText());
+ }
+ return false;
}
return false;
}
@@ -1139,6 +1182,52 @@ public class DynamoDbService {
return placeholder;
}
+ private boolean attributeValuesEqual(JsonNode a, JsonNode b) {
+ if (a == null || b == null) return a == b;
+ // Scalar types: S, B, BOOL, NULL
+ for (String type : new String[]{"S", "B", "BOOL", "NULL"}) {
+ if (a.has(type) && b.has(type)) {
+ return a.get(type).asText().equals(b.get(type).asText());
+ }
+ if (a.has(type) || b.has(type)) return false; // type mismatch
+ }
+ // Numeric comparison with normalization
+ if (a.has("N") && b.has("N")) {
+ try {
+ return new java.math.BigDecimal(a.get("N").asText())
+ .compareTo(new java.math.BigDecimal(b.get("N").asText())) == 0;
+ } catch (NumberFormatException e) {
+ return false;
+ }
+ }
+ if (a.has("N") || b.has("N")) return false;
+ // Map type: compare all entries recursively
+ if (a.has("M") && b.has("M")) {
+ JsonNode aMap = a.get("M");
+ JsonNode bMap = b.get("M");
+ if (aMap.size() != bMap.size()) return false;
+ var fields = aMap.fields();
+ while (fields.hasNext()) {
+ var entry = fields.next();
+ if (!bMap.has(entry.getKey())) return false;
+ if (!attributeValuesEqual(entry.getValue(), bMap.get(entry.getKey()))) return false;
+ }
+ return true;
+ }
+ // List type: compare element by element
+ if (a.has("L") && b.has("L")) {
+ JsonNode aList = a.get("L");
+ JsonNode bList = b.get("L");
+ if (aList.size() != bList.size()) return false;
+ for (int i = 0; i < aList.size(); i++) {
+ if (!attributeValuesEqual(aList.get(i), bList.get(i))) return false;
+ }
+ return true;
+ }
+ // Different types are never equal
+ return false;
+ }
+
private int compareValues(String a, String b) {
// Try numeric comparison first
try {
@@ -1148,6 +1237,48 @@ public class DynamoDbService {
}
}
+ private static final String DOT_ESCAPE = "\uFF0E";
+
+ private String resolveAttributePath(String path, JsonNode exprAttrNames) {
+ // Resolve each segment of a dotted path, e.g. "passengerInformation.#name"
+ // ExpressionAttributeNames may resolve to names containing dots (e.g. "#a" -> "foo.bar").
+ // Escape those dots so resolveNestedAttribute treats them as single keys.
+ String[] segments = path.split("\\.");
+ StringBuilder resolved = new StringBuilder();
+ for (int i = 0; i < segments.length; i++) {
+ if (i > 0) resolved.append(".");
+ String original = segments[i];
+ String resolvedSegment = resolveAttributeName(original, exprAttrNames);
+ if (original.startsWith("#") && resolvedSegment != null) {
+ resolvedSegment = resolvedSegment.replace(".", DOT_ESCAPE);
+ }
+ resolved.append(resolvedSegment);
+ }
+ return resolved.toString();
+ }
+
+ private JsonNode resolveNestedAttribute(JsonNode item, String path) {
+ // Navigate a dotted path through DynamoDB's {"M": {...}} structure
+ String[] segments = path.split("\\.");
+ JsonNode current = item;
+ for (int i = 0; i < segments.length; i++) {
+ if (current == null) return null;
+ String segment = segments[i].replace(DOT_ESCAPE, ".");
+ if (i == 0) {
+ // First segment: resolve against the top-level item map
+ current = current.get(segment);
+ } else {
+ // Subsequent segments: descend into DynamoDB Map type
+ if (current.has("M")) {
+ current = current.get("M").get(segment);
+ } else {
+ current = current.get(segment);
+ }
+ }
+ }
+ return current;
+ }
+
private String extractFunctionArg(String funcCall) {
int open = funcCall.indexOf('(');
int close = funcCall.lastIndexOf(')');
@@ -1230,6 +1361,7 @@ public class DynamoDbService {
if (attrValue.has("S")) return attrValue.get("S").asText();
if (attrValue.has("N")) return attrValue.get("N").asText();
if (attrValue.has("B")) return attrValue.get("B").asText();
+ if (attrValue.has("BOOL")) return attrValue.get("BOOL").asText();
return attrValue.asText();
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java
index efd103a7a4..bc2dfa716e 100644
--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java
@@ -475,4 +475,213 @@ class DynamoDbServiceTest {
assertEquals("fallback", stored.get("target").get("S").asText(),
"target should receive the fallback value when source is absent");
}
+
+ @Test
+ void scanWithBoolFilterExpression() {
+ createUsersTable();
+ ObjectNode u1 = item("userId", "u1");
+ u1.set("deleted", boolAttributeValue(false));
+ service.putItem("Users", u1);
+
+ ObjectNode u2 = item("userId", "u2");
+ u2.set("deleted", boolAttributeValue(true));
+ service.putItem("Users", u2);
+
+ ObjectNode u3 = item("userId", "u3");
+ u3.set("deleted", boolAttributeValue(false));
+ service.putItem("Users", u3);
+
+ ObjectNode exprValues = mapper.createObjectNode();
+ exprValues.set(":d", boolAttributeValue(true));
+
+ DynamoDbService.ScanResult result = service.scan("Users", "deleted <> :d", null, exprValues, null, null);
+ assertEquals(2, result.items().size());
+ }
+
+ @Test
+ void scanContainsOnListAttribute() {
+ createUsersTable();
+ ObjectNode u1 = item("userId", "u1");
+ u1.set("tags", listAttributeValue("a", "b"));
+ service.putItem("Users", u1);
+
+ ObjectNode u2 = item("userId", "u2");
+ u2.set("tags", listAttributeValue("a", "c"));
+ service.putItem("Users", u2);
+
+ ObjectNode u3 = item("userId", "u3");
+ u3.set("tags", listAttributeValue("b", "c"));
+ service.putItem("Users", u3);
+
+ ObjectNode exprValues = mapper.createObjectNode();
+ exprValues.set(":v", attributeValue("S", "a"));
+
+ DynamoDbService.ScanResult result = service.scan("Users", "contains(tags, :v)", null, exprValues, null, null);
+ assertEquals(2, result.items().size());
+ }
+
+ @Test
+ void scanContainsOnStringSetAttribute() {
+ createUsersTable();
+ ObjectNode u1 = item("userId", "u1");
+ u1.set("roles", stringSetAttributeValue("admin", "user"));
+ service.putItem("Users", u1);
+
+ ObjectNode u2 = item("userId", "u2");
+ u2.set("roles", stringSetAttributeValue("user"));
+ service.putItem("Users", u2);
+
+ ObjectNode exprValues = mapper.createObjectNode();
+ exprValues.set(":r", attributeValue("S", "admin"));
+
+ DynamoDbService.ScanResult result = service.scan("Users", "contains(roles, :r)", null, exprValues, null, null);
+ assertEquals(1, result.items().size());
+ }
+
+ @Test
+ void scanAttributeExistsOnNestedMapPath() {
+ createUsersTable();
+ ObjectNode u1 = item("userId", "u1");
+ u1.set("info", mapAttributeValue("name", "Alice"));
+ service.putItem("Users", u1);
+
+ ObjectNode u2 = item("userId", "u2");
+ ObjectNode emptyMap = mapper.createObjectNode();
+ ObjectNode mapWrapper = mapper.createObjectNode();
+ mapWrapper.set("M", emptyMap);
+ u2.set("info", mapWrapper);
+ service.putItem("Users", u2);
+
+ ObjectNode u3 = item("userId", "u3");
+ u3.set("info", mapAttributeValue("name", "Bob"));
+ service.putItem("Users", u3);
+
+ ObjectNode exprNames = mapper.createObjectNode();
+ exprNames.put("#n", "name");
+
+ DynamoDbService.ScanResult result = service.scan("Users", "attribute_exists(info.#n)", exprNames, null, null, null);
+ assertEquals(2, result.items().size());
+
+ DynamoDbService.ScanResult result2 = service.scan("Users", "attribute_not_exists(info.#n)", exprNames, null, null, null);
+ assertEquals(1, result2.items().size());
+ }
+
+ private ObjectNode boolAttributeValue(boolean value) {
+ ObjectNode node = mapper.createObjectNode();
+ node.put("BOOL", value);
+ return node;
+ }
+
+ private ObjectNode listAttributeValue(String... values) {
+ ObjectNode node = mapper.createObjectNode();
+ var arrayNode = mapper.createArrayNode();
+ for (String v : values) {
+ arrayNode.add(attributeValue("S", v));
+ }
+ node.set("L", arrayNode);
+ return node;
+ }
+
+ private ObjectNode stringSetAttributeValue(String... values) {
+ ObjectNode node = mapper.createObjectNode();
+ var arrayNode = mapper.createArrayNode();
+ for (String v : values) {
+ arrayNode.add(v);
+ }
+ node.set("SS", arrayNode);
+ return node;
+ }
+
+ private ObjectNode mapAttributeValue(String key, String value) {
+ ObjectNode inner = mapper.createObjectNode();
+ inner.set(key, attributeValue("S", value));
+ ObjectNode node = mapper.createObjectNode();
+ node.set("M", inner);
+ return node;
+ }
+
+ private ObjectNode numberSetAttributeValue(String... values) {
+ ObjectNode node = mapper.createObjectNode();
+ var arrayNode = mapper.createArrayNode();
+ for (String v : values) {
+ arrayNode.add(v);
+ }
+ node.set("NS", arrayNode);
+ return node;
+ }
+
+ private ObjectNode binarySetAttributeValue(String... base64Values) {
+ ObjectNode node = mapper.createObjectNode();
+ var arrayNode = mapper.createArrayNode();
+ for (String v : base64Values) {
+ arrayNode.add(v);
+ }
+ node.set("BS", arrayNode);
+ return node;
+ }
+
+ @Test
+ void scanContainsOnNumberSetWithNumericNormalization() {
+ createUsersTable();
+ ObjectNode u1 = item("userId", "u1");
+ u1.set("scores", numberSetAttributeValue("1", "2", "3"));
+ service.putItem("Users", u1);
+
+ ObjectNode u2 = item("userId", "u2");
+ u2.set("scores", numberSetAttributeValue("4", "5"));
+ service.putItem("Users", u2);
+
+ // Search for "1.0" — should match "1" via numeric comparison
+ ObjectNode exprValues = mapper.createObjectNode();
+ exprValues.set(":v", attributeValue("N", "1.0"));
+
+ DynamoDbService.ScanResult result = service.scan("Users", "contains(scores, :v)", null, exprValues, null, null);
+ assertEquals(1, result.items().size(), "contains() on NS should match 1.0 == 1 numerically");
+ }
+
+ @Test
+ void scanContainsOnBinarySet() {
+ createUsersTable();
+ ObjectNode u1 = item("userId", "u1");
+ u1.set("bins", binarySetAttributeValue("AQID", "BAUG")); // base64 for [1,2,3] and [4,5,6]
+ service.putItem("Users", u1);
+
+ ObjectNode u2 = item("userId", "u2");
+ u2.set("bins", binarySetAttributeValue("BwgJ"));
+ service.putItem("Users", u2);
+
+ ObjectNode exprValues = mapper.createObjectNode();
+ exprValues.set(":v", attributeValue("B", "AQID"));
+
+ DynamoDbService.ScanResult result = service.scan("Users", "contains(bins, :v)", null, exprValues, null, null);
+ assertEquals(1, result.items().size());
+ }
+
+ @Test
+ void scanContainsOnListWithNumericElements() {
+ createUsersTable();
+ ObjectNode u1 = item("userId", "u1");
+ var list = mapper.createArrayNode();
+ list.add(attributeValue("N", "10"));
+ list.add(attributeValue("N", "20"));
+ ObjectNode listNode = mapper.createObjectNode();
+ listNode.set("L", list);
+ u1.set("values", listNode);
+ service.putItem("Users", u1);
+
+ ObjectNode u2 = item("userId", "u2");
+ var list2 = mapper.createArrayNode();
+ list2.add(attributeValue("N", "30"));
+ ObjectNode listNode2 = mapper.createObjectNode();
+ listNode2.set("L", list2);
+ u2.set("values", listNode2);
+ service.putItem("Users", u2);
+
+ // Search for N:10.0 — should match N:10 via type-aware comparison
+ ObjectNode exprValues = mapper.createObjectNode();
+ exprValues.set(":v", attributeValue("N", "10.0"));
+
+ DynamoDbService.ScanResult result = service.scan("Users", "contains(values, :v)", null, exprValues, null, null);
+ assertEquals(1, result.items().size(), "contains() on List with N elements should use type-aware numeric comparison");
+ }
}