Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

import static java.lang.Character.isDigit;

public class ArrayIndexOperation {

private final static Pattern COMMA = Pattern.compile("\\s*,\\s*");

private final List<Integer> indexes;

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

List<Integer> tempIndexes = new ArrayList<Integer>();
List<Integer> tempIndexes = new ArrayList<Integer>(tokens.length);
for (String token : tokens) {
tempIndexes.add(parseInteger(token));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ private boolean readArrayToken(PathTokenAppender appender) {
return false;
}

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

if ("*".equals(expression)) {
return false;
Expand All @@ -521,7 +521,7 @@ private boolean readArrayToken(PathTokenAppender appender) {
//check valid chars
for (int i = 0; i < expression.length(); i++) {
char c = expression.charAt(i);
if (!isDigit(c) && c != COMMA && c != MINUS && c != SPLIT) {
if (!isDigit(c) && c != COMMA && c != MINUS && c != SPLIT && c != SPACE) {
return false;
}
}
Expand Down Expand Up @@ -560,6 +560,7 @@ private boolean readBracketPropertyToken(PathTokenAppender appender) {
int endPosition = 0;
boolean inProperty = false;
boolean inEscape = false;
boolean lastSignificantWasComma = false;

while (path.inBounds(readPosition)) {
char c = path.charAt(readPosition);
Expand All @@ -569,6 +570,9 @@ private boolean readBracketPropertyToken(PathTokenAppender appender) {
} else if('\\' == c){
inEscape = true;
} else if (c == CLOSE_SQUARE_BRACKET && !inProperty) {
if (lastSignificantWasComma){
fail("Found empty property at index "+readPosition);
}
break;
} else if (c == potentialStringDelimiter) {
if (inProperty && !inEscape) {
Expand All @@ -579,7 +583,13 @@ private boolean readBracketPropertyToken(PathTokenAppender appender) {
} else {
startPosition = readPosition + 1;
inProperty = true;
lastSignificantWasComma = false;
}
} else if (c == COMMA){
if (lastSignificantWasComma){
fail("Found empty property at index "+readPosition);
}
lastSignificantWasComma = true;
}
readPosition++;
}
Expand Down
15 changes: 15 additions & 0 deletions json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,19 @@ public void issue_predicate_can_have_square_bracket_in_prop() {

assertThat(result).containsExactly("] it");
}

@Test(expected = InvalidPathException.class)
public void array_indexes_must_be_separated_by_commas() {
compile("$[0, 1, 2 4]");
}

@Test(expected = InvalidPathException.class)
public void trailing_comma_after_list_is_not_accepted() {
compile("$['1','2',]");
}

@Test(expected = InvalidPathException.class)
public void accept_only_a_single_comma_between_indexes() {
compile("$['1', ,'3']");
}
}