Skip to content

Commit 08664b6

Browse files
authored
Merge pull request #108 from dashjoin/backport-operator-fixes
Fix ? and ?? operators with array predicate on LHS
2 parents fbb6878 + c6e6822 commit 08664b6

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

src/main/java/com/dashjoin/jsonata/Parser.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.ByteArrayOutputStream;
2929
import java.io.ObjectInputStream;
3030
import java.io.ObjectOutputStream;
31+
import java.io.Serializable;
3132
import java.util.ArrayList;
3233
import java.util.Arrays;
3334
import java.util.HashMap;
@@ -40,7 +41,7 @@
4041

4142
//var parseSignature = require('./signature');
4243
@SuppressWarnings({"unchecked"})
43-
public class Parser {
44+
public class Parser implements Serializable {
4445

4546
boolean dbg = false;
4647

@@ -92,7 +93,7 @@ public static<T> T clone(T object) {
9293
}
9394
}
9495

95-
public class Symbol implements Cloneable {
96+
public class Symbol implements Cloneable, Serializable {
9697

9798
//Symbol s;
9899
String id;
@@ -505,7 +506,7 @@ public Parser() {
505506
c.value = "(";
506507
Symbol p = new Symbol();
507508
c.procedure = p; p.type = "variable"; p.value = "exists";
508-
c.arguments = List.of(left);
509+
c.arguments = List.of(Parser.clone(left));
509510
}
510511
this.then = left;
511512
this._else = expression(0);
@@ -812,7 +813,12 @@ Symbol led(Symbol left) {
812813

813814
@Override Symbol led(Symbol left) {
814815
this.type = "condition";
815-
this.condition = left;
816+
// Deep-clone `left` so the condition and then branches have
817+
// independent AST nodes. Sharing the same reference causes
818+
// post-parse processing (e.g. predicate stages, unary minus
819+
// folding on number literals) to mutate the same node twice,
820+
// producing wrong results (see #773 for the equivalent ?? fix).
821+
this.condition = Parser.clone(left);
816822
this.then = left;
817823
this._else = expression(0);
818824
return this;

src/main/java/com/dashjoin/jsonata/Tokenizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import java.util.regex.Matcher;
2929
import java.util.regex.Pattern;
3030

31-
public class Tokenizer { // = function (path) {
31+
public class Tokenizer implements java.io.Serializable { // = function (path) {
3232

3333
static HashMap<String, Integer> operators = new HashMap<String, Integer>() {{
3434
put(".", 75);

0 commit comments

Comments
 (0)