Skip to content

Commit a7d8e11

Browse files
authored
Merge pull request java-json-tools#10 from gravity9piotr/bugfix/807_json_pointer_double_slashes
#807 JsonPointer double slashes validation
2 parents 7263b1b + ad576e9 commit a7d8e11

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

Diff for: src/main/java/com/github/fge/jsonpatch/JsonPathParser.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ public class JsonPathParser {
77
public static String tmfStringToJsonPath(String path) throws JsonPatchException {
88
if (path.startsWith("$")) {
99
return path;
10-
}
11-
if (path.contains("?")) {
10+
} else if (path.contains("?")) {
1211
throw new JsonPatchException("Invalid path, `?` are not allowed in JsonPointer expressions.");
12+
} else if (path.contains("//")) {
13+
throw new JsonPatchException("Invalid path, `//` is not allowed in JsonPointer expressions.");
1314
}
1415

1516
return "$" + path.replace('/', '.').replaceAll(ARRAY_ELEMENT_REGEX, "[$1]");

Diff for: src/test/java/com/github/fge/jsonpatch/JsonPathParserTest.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.testng.annotations.Test;
44

5-
import static org.testng.Assert.*;
5+
import static org.testng.Assert.assertEquals;
66

77
public class JsonPathParserTest {
88

@@ -45,4 +45,16 @@ public void shouldLeaveJsonPathStatementsUntouched() throws JsonPatchException {
4545
String result = JsonPathParser.tmfStringToJsonPath(filterQuery);
4646
assertEquals(result, expected);
4747
}
48+
49+
@Test(expectedExceptions = JsonPatchException.class, expectedExceptionsMessageRegExp = "Invalid path, `//` is not allowed in JsonPointer expressions.")
50+
public void shouldThrowExceptionWhenDoubleSlashesInJsonPointerPath() throws JsonPatchException {
51+
String filterQuery = "/characteristic/0//age";
52+
JsonPathParser.tmfStringToJsonPath(filterQuery);
53+
}
54+
55+
@Test(expectedExceptions = JsonPatchException.class)
56+
public void shouldThrowExceptionWhenQuestionMarkInJsonPointerPath() throws JsonPatchException {
57+
String filterQuery = "/characteristic/0/age?";
58+
JsonPathParser.tmfStringToJsonPath(filterQuery);
59+
}
4860
}

0 commit comments

Comments
 (0)