fix(dynamodb): merge nested map paths in ProjectionExpression (#1099)

When multiple ProjectionExpression paths share the same parent map
(e.g. data.title, data.answer, data.sources), copyPath() created a
fresh ObjectNode for each path and overwrote the previous projection.
Only the last sibling path was returned.

Fix: reuse the existing destination node when the parent key has already
been projected, so all sibling paths accumulate into the same node.
Applied to both the {"M": ...} DynamoDB-typed branch and the isObject()
plain-object fallback.

Additional fixes applied during review:
- List index [n] branch: same overwrite guard added (was also
  unconditionally overwriting dest[seg] on repeated access)
- Leaf deep-copy: dest.set(seg, child.deepCopy()) prevents placing a
  live source reference into the destination, which a subsequent sibling
  path could have mutated via the isObject() reuse logic
- Test cleanup: replaced @Order(100) teardown with @AfterAll so the
  table is always deleted even when a test fails
- Added direct ProjectionEvaluator unit test for the isObject() branch

Closes #852
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-05-30 16:05:17 -05:00
1 parent 9b7f8e40f0
commit f191982f25
3 files changed
+240 -12

No files matched your search

@@ -142,15 +142,17 @@ final class ProjectionEvaluator {
if (child == null) return;
if (idx == segments.size() - 1) {
// Leaf: copy the whole attribute node
dest.set(seg, child);
// Leaf: deep-copy to avoid placing a live source reference into the
// destination, which could be mutated by a subsequent sibling path.
dest.set(seg, child.deepCopy());
} else {
String nextSeg = segments.get(idx + 1);
if (nextSeg.matches("\\[\\d+\\]")) {
// Next is a list index — extract just that element
// Next is a list index — extract just that element.
// Reuse existing wrapper if already projected (same fix as the M branch).
int listIdx = Integer.parseInt(nextSeg.substring(1, nextSeg.length() - 1));
JsonNode listNode = child.has("L") ? child.get("L") : child;
if (listNode.isArray() && listIdx < listNode.size()) {
if (listNode.isArray() && listIdx < listNode.size() && !dest.has(seg)) {
JsonNode element = listNode.get(listIdx);
// Build {"L": [element]} wrapper
ObjectNode wrapper = MAPPER.createObjectNode();
@@ -160,16 +162,25 @@ final class ProjectionEvaluator {
dest.set(seg, wrapper);
}
} else if (child.has("M")) {
// Nested map — recurse
ObjectNode nestedDest = MAPPER.createObjectNode();
// Nested map — reuse existing projected map if present
ObjectNode existing = dest.has(seg) && dest.get(seg).has("M")
? (ObjectNode) dest.get(seg).get("M") : null;
ObjectNode nestedDest = existing != null ? existing : MAPPER.createObjectNode();
copyPath(child.get("M"), nestedDest, segments, idx + 1);
ObjectNode wrapper = MAPPER.createObjectNode();
wrapper.set("M", nestedDest);
dest.set(seg, wrapper);
if (existing == null) {
ObjectNode wrapper = MAPPER.createObjectNode();
wrapper.set("M", nestedDest);
dest.set(seg, wrapper);
}
} else if (child.isObject()) {
ObjectNode nestedDest = MAPPER.createObjectNode();
// Reuse existing nested object if present
ObjectNode existing = dest.has(seg) && dest.get(seg).isObject()
? (ObjectNode) dest.get(seg) : null;
ObjectNode nestedDest = existing != null ? existing : MAPPER.createObjectNode();
copyPath(child, nestedDest, segments, idx + 1);
dest.set(seg, nestedDest);
if (existing == null) {
dest.set(seg, nestedDest);
}
} else {
dest.set(seg, child);
}