Skip to content

Commit 91f21d0

Browse files
committed
Merge pull request #194 from jochenberger/fix-array-subscriptions
PR for #173
2 parents 2f690f0 + 7115c61 commit 91f21d0

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

json-path/src/main/java/com/jayway/jsonpath/internal/path/ArrayIndexOperation.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
import java.util.ArrayList;
77
import java.util.Collections;
88
import java.util.List;
9+
import java.util.regex.Pattern;
910

1011
import static java.lang.Character.isDigit;
1112

1213
public class ArrayIndexOperation {
1314

15+
private final static Pattern COMMA = Pattern.compile("\\s*,\\s*");
16+
1417
private final List<Integer> indexes;
1518

1619
private ArrayIndexOperation(List<Integer> indexes) {
@@ -39,13 +42,13 @@ public static ArrayIndexOperation parse(String operation) {
3942
//check valid chars
4043
for (int i = 0; i < operation.length(); i++) {
4144
char c = operation.charAt(i);
42-
if (!isDigit(c) && c != ',') {
45+
if (!isDigit(c) && c != ',' && c != ' ') {
4346
throw new InvalidPathException("Failed to parse ArrayIndexOperation: " + operation);
4447
}
4548
}
46-
String[] tokens = operation.split(",");
49+
String[] tokens = COMMA.split(operation, -1);
4750

48-
List<Integer> tempIndexes = new ArrayList<Integer>();
51+
List<Integer> tempIndexes = new ArrayList<Integer>(tokens.length);
4952
for (String token : tokens) {
5053
tempIndexes.add(parseInteger(token));
5154
}

json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ private boolean readArrayToken(PathTokenAppender appender) {
512512
return false;
513513
}
514514

515-
String expression = path.subSequence(expressionBeginIndex, expressionEndIndex).toString().replace(" ", "");
515+
String expression = path.subSequence(expressionBeginIndex, expressionEndIndex).toString().trim();
516516

517517
if ("*".equals(expression)) {
518518
return false;
@@ -521,7 +521,7 @@ private boolean readArrayToken(PathTokenAppender appender) {
521521
//check valid chars
522522
for (int i = 0; i < expression.length(); i++) {
523523
char c = expression.charAt(i);
524-
if (!isDigit(c) && c != COMMA && c != MINUS && c != SPLIT) {
524+
if (!isDigit(c) && c != COMMA && c != MINUS && c != SPLIT && c != SPACE) {
525525
return false;
526526
}
527527
}
@@ -560,6 +560,7 @@ private boolean readBracketPropertyToken(PathTokenAppender appender) {
560560
int endPosition = 0;
561561
boolean inProperty = false;
562562
boolean inEscape = false;
563+
boolean lastSignificantWasComma = false;
563564

564565
while (path.inBounds(readPosition)) {
565566
char c = path.charAt(readPosition);
@@ -569,6 +570,9 @@ private boolean readBracketPropertyToken(PathTokenAppender appender) {
569570
} else if('\\' == c){
570571
inEscape = true;
571572
} else if (c == CLOSE_SQUARE_BRACKET && !inProperty) {
573+
if (lastSignificantWasComma){
574+
fail("Found empty property at index "+readPosition);
575+
}
572576
break;
573577
} else if (c == potentialStringDelimiter) {
574578
if (inProperty && !inEscape) {
@@ -579,7 +583,13 @@ private boolean readBracketPropertyToken(PathTokenAppender appender) {
579583
} else {
580584
startPosition = readPosition + 1;
581585
inProperty = true;
586+
lastSignificantWasComma = false;
587+
}
588+
} else if (c == COMMA){
589+
if (lastSignificantWasComma){
590+
fail("Found empty property at index "+readPosition);
582591
}
592+
lastSignificantWasComma = true;
583593
}
584594
readPosition++;
585595
}

json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,19 @@ public void issue_predicate_can_have_square_bracket_in_prop() {
251251

252252
assertThat(result).containsExactly("] it");
253253
}
254+
255+
@Test(expected = InvalidPathException.class)
256+
public void array_indexes_must_be_separated_by_commas() {
257+
compile("$[0, 1, 2 4]");
258+
}
259+
260+
@Test(expected = InvalidPathException.class)
261+
public void trailing_comma_after_list_is_not_accepted() {
262+
compile("$['1','2',]");
263+
}
264+
265+
@Test(expected = InvalidPathException.class)
266+
public void accept_only_a_single_comma_between_indexes() {
267+
compile("$['1', ,'3']");
268+
}
254269
}

0 commit comments

Comments
 (0)