diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/path/ArrayIndexOperation.java b/json-path/src/main/java/com/jayway/jsonpath/internal/path/ArrayIndexOperation.java index 0fd833a0..8fde93a9 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/path/ArrayIndexOperation.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/path/ArrayIndexOperation.java @@ -6,11 +6,14 @@ import com.jayway.jsonpath.internal.Utils; 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 indexes; private ArrayIndexOperation(List indexes) { @@ -39,13 +42,13 @@ public class ArrayIndexOperation { //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 tempIndexes = new ArrayList(); + List tempIndexes = new ArrayList(tokens.length); for (String token : tokens) { tempIndexes.add(parseInteger(token)); } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java b/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java index b8039b9f..4d003dc7 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java @@ -512,7 +512,7 @@ public class PathCompiler { return false; } - String expression = path.subSequence(expressionBeginIndex, expressionEndIndex).toString().replace(" ", ""); + String expression = path.subSequence(expressionBeginIndex, expressionEndIndex).toString().trim(); if ("*".equals(expression)) { return false; @@ -521,7 +521,7 @@ public class PathCompiler { //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; } } @@ -560,6 +560,7 @@ public class PathCompiler { int endPosition = 0; boolean inProperty = false; boolean inEscape = false; + boolean lastSignificantWasComma = false; while (path.inBounds(readPosition)) { char c = path.charAt(readPosition); @@ -569,6 +570,9 @@ public class PathCompiler { } 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) { @@ -579,7 +583,13 @@ public class PathCompiler { } else { startPosition = readPosition + 1; inProperty = true; + lastSignificantWasComma = false; + } + } else if (c == COMMA){ + if (lastSignificantWasComma){ + fail("Found empty property at index "+readPosition); } + lastSignificantWasComma = true; } readPosition++; } diff --git a/json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java b/json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java index 7708bc07..60a5ba83 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java @@ -251,4 +251,19 @@ public class PathCompilerTest { 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']"); + } }