Skip to content

Commit

Permalink
Merge pull request java-json-tools#10 from gravity9piotr/bugfix/807_j…
Browse files Browse the repository at this point in the history
…son_pointer_double_slashes

#807 JsonPointer double slashes validation
  • Loading branch information
pbugara authored Jul 22, 2022
2 parents 7263b1b + ad576e9 commit a7d8e11
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/main/java/com/github/fge/jsonpatch/JsonPathParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ public class JsonPathParser {
public static String tmfStringToJsonPath(String path) throws JsonPatchException {
if (path.startsWith("$")) {
return path;
}
if (path.contains("?")) {
} else if (path.contains("?")) {
throw new JsonPatchException("Invalid path, `?` are not allowed in JsonPointer expressions.");
} else if (path.contains("//")) {
throw new JsonPatchException("Invalid path, `//` is not allowed in JsonPointer expressions.");
}

return "$" + path.replace('/', '.').replaceAll(ARRAY_ELEMENT_REGEX, "[$1]");
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/com/github/fge/jsonpatch/JsonPathParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.testng.annotations.Test;

import static org.testng.Assert.*;
import static org.testng.Assert.assertEquals;

public class JsonPathParserTest {

Expand Down Expand Up @@ -45,4 +45,16 @@ public void shouldLeaveJsonPathStatementsUntouched() throws JsonPatchException {
String result = JsonPathParser.tmfStringToJsonPath(filterQuery);
assertEquals(result, expected);
}

@Test(expectedExceptions = JsonPatchException.class, expectedExceptionsMessageRegExp = "Invalid path, `//` is not allowed in JsonPointer expressions.")
public void shouldThrowExceptionWhenDoubleSlashesInJsonPointerPath() throws JsonPatchException {
String filterQuery = "/characteristic/0//age";
JsonPathParser.tmfStringToJsonPath(filterQuery);
}

@Test(expectedExceptions = JsonPatchException.class)
public void shouldThrowExceptionWhenQuestionMarkInJsonPointerPath() throws JsonPatchException {
String filterQuery = "/characteristic/0/age?";
JsonPathParser.tmfStringToJsonPath(filterQuery);
}
}

0 comments on commit a7d8e11

Please sign in to comment.