Browse Source

Only look for PatternFlags after a regular expression (#661)

pull/614/merge
Felix Schumacher 4 years ago committed by GitHub
parent
commit
eb8db779b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java
  2. 10
      json-path/src/test/java/com/jayway/jsonpath/InlineFilterTest.java

25
json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java

@ -274,17 +274,32 @@ public class FilterCompiler {
}
private int endOfFlags(int position) {
int endIndex = position;
char[] currentChar = new char[1];
while (filter.inBounds(endIndex)) {
currentChar[0] = filter.charAt(endIndex);
if (PatternFlag.parseFlags(currentChar) > 0) {
endIndex++;
continue;
}
break;
}
return endIndex;
}
private PatternNode readPattern() {
int begin = filter.position();
int closingIndex = filter.nextIndexOfUnescaped(PATTERN);
if (closingIndex == -1) {
throw new InvalidPathException("Pattern not closed. Expected " + PATTERN + " in " + filter);
} else {
if(filter.inBounds(closingIndex+1)) {
int equalSignIndex = filter.nextIndexOf('=');
int endIndex = equalSignIndex > closingIndex ? equalSignIndex : filter.nextIndexOfUnescaped(CLOSE_PARENTHESIS);
CharSequence flags = filter.subSequence(closingIndex + 1, endIndex);
closingIndex += flags.length();
if (filter.inBounds(closingIndex+1)) {
int endFlagsIndex = endOfFlags(closingIndex + 1);
if (endFlagsIndex > closingIndex) {
CharSequence flags = filter.subSequence(closingIndex + 1, endFlagsIndex);
closingIndex += flags.length();
}
}
filter.setPosition(closingIndex + 1);
}

10
json-path/src/test/java/com/jayway/jsonpath/InlineFilterTest.java

@ -210,6 +210,16 @@ public class InlineFilterTest extends BaseTest {
assertHasOneResult("[\"x\"]", "$[?(@ =~ /\\/|x/)]", conf);
}
@Test
public void escape_pattern_after_literal() {
assertHasOneResult("[\"x\"]", "$[?(@ == \"abc\" || @ =~ /\\/|x/)]", conf);
}
@Test
public void escape_pattern_before_literal() {
assertHasOneResult("[\"x\"]", "$[?(@ =~ /\\/|x/ || @ == \"abc\")]", conf);
}
@Test
public void filter_evaluation_does_not_break_path_evaluation() {
assertHasOneResult("[{\"s\": \"fo\", \"expected_size\": \"m\"}, {\"s\": \"lo\", \"expected_size\": 2}]", "$[?(@.s size @.expected_size)]", conf);

Loading…
Cancel
Save