Skip to content

Commit f1bf677

Browse files
author
Piotr Bugara
committed
#807 JsonPointer double slashes validation
1 parent 7263b1b commit f1bf677

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/main/java/com/github/fge/jsonpatch/JsonPathParser.java

Lines changed: 3 additions & 2 deletions
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]");

src/test/java/com/github/fge/jsonpatch/JsonPathParserTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.testng.annotations.Test;
44

5+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
56
import static org.testng.Assert.*;
67

78
public class JsonPathParserTest {
@@ -45,4 +46,10 @@ public void shouldLeaveJsonPathStatementsUntouched() throws JsonPatchException {
4546
String result = JsonPathParser.tmfStringToJsonPath(filterQuery);
4647
assertEquals(result, expected);
4748
}
49+
50+
@Test(expectedExceptions = JsonPatchException.class, expectedExceptionsMessageRegExp = "Invalid path, `//` is not allowed in JsonPointer expressions.")
51+
public void shouldThrowExceptionWhenDoubleSlashesInJsonPointerPath() throws JsonPatchException {
52+
String filterQuery = "/characteristic/0//age";
53+
JsonPathParser.tmfStringToJsonPath(filterQuery);
54+
}
4855
}

0 commit comments

Comments
 (0)