From a6c35dc671ee34add89063ee104dd517907590a3 Mon Sep 17 00:00:00 2001 From: wheene Date: Tue, 18 May 2021 20:15:05 +0200 Subject: [PATCH] Fixes variable binding in object destructuring ref #77 This change makes ObjectMatcher keep entries put into accumulator unchanged until they are emitted. This makes entries put into the accumulator by nested ObjectMatcher visible to subsequent matchers and eventually to the expression where whole accumulated capture is emitted. --- .../tree/matcher/matchers/ObjectMatcher.java | 3 +- .../src/test/resources/jq-test-extra-ok.json | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/tree/matcher/matchers/ObjectMatcher.java b/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/tree/matcher/matchers/ObjectMatcher.java index 8fce835e..72a8bc20 100644 --- a/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/tree/matcher/matchers/ObjectMatcher.java +++ b/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/tree/matcher/matchers/ObjectMatcher.java @@ -38,7 +38,8 @@ private void recursive(final Scope scope, final JsonNode in, final Functional.Co final int size = accumulate.size(); matcher.match(scope, value != null ? value : NullNode.getInstance(), out, accumulate, emit && index == matchers.size() - 1); recursive(scope, in, out, accumulate, emit, index + 1); - accumulate.setSize(size); + if(emit) // value in accumulate needs to remain visible until it is emitted + accumulate.setSize(size); } } diff --git a/jackson-jq/src/test/resources/jq-test-extra-ok.json b/jackson-jq/src/test/resources/jq-test-extra-ok.json index ed493f73..21bed6a2 100644 --- a/jackson-jq/src/test/resources/jq-test-extra-ok.json +++ b/jackson-jq/src/test/resources/jq-test-extra-ok.json @@ -729,5 +729,33 @@ "q": "\"\\/\"", "in": null, "out": ["/"] + }, + { + "q": ". as {\"a\":$a, \"b\":$b, \"c\":$c} | [$a,$b,$c]", + "in": {"a":1, "b": 2,"c": 3}, + "out": [ + [1,2,3] + ] + }, + { + "q": ". as {\"outer\":{\"x\": $x}, \"x\": $y} | [$x, $y]", + "in": {"outer":{"x": 1}, "x": 2}, + "out": [ + [1,2] + ] + }, + { + "q": ". as {\"outer\":{\"x\": $x}, \"x\": $x} | $x", + "in": {"outer":{"x": 1}, "x": 2}, + "out": [ + 1 + ] + }, + { + "q": ". as {\"x\": $x, \"outer\":{\"x\": $x}} | $x", + "in": {"outer":{"x": 1}, "x": 2}, + "out": [ + 2 + ] } ]