Skip to content

Commit 4bfa932

Browse files
committed
fail when there are spaces inside array indexes
1 parent 2cb9b47 commit 4bfa932

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-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: 2 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
}

0 commit comments

Comments
 (0)