Skip to content

Commit

Permalink
#61 JsonPathParser wrongly parse unescaped reference-token if its con…
Browse files Browse the repository at this point in the history
…tains a number
  • Loading branch information
MateuszZarembaMongoDB committed Feb 27, 2024
1 parent 6e29691 commit 4088b3a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/main/java/com/gravity9/jsonpatch/JsonPathParser.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
package com.gravity9.jsonpatch;

import java.util.Arrays;

public class JsonPathParser {

private JsonPathParser() {}
private JsonPathParser() {
}

private static final String ARRAY_ELEMENT_REGEX = "(?<=\\.)(\\d+)";
private static final String ARRAY_ELEMENT_REGEX = "\\A((\\d+)[^a-zA-Z]*)\\z";

/**
* Method parses JsonPointer or JsonPath path to JsonPath syntax
* @param path String containing JsonPath or JsonPointer expression
* @return String containing JsonPath expression
* @throws JsonPatchException throws when invalid JsonPointer expression provided
*/
public static String parsePathToJsonPath(String path) throws JsonPatchException {
if (path.startsWith("$")) {
return path;
} 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.");
}
/**
* Method parses JsonPointer or JsonPath path to JsonPath syntax
*
* @param path String containing JsonPath or JsonPointer expression
* @return String containing JsonPath expression
* @throws JsonPatchException throws when invalid JsonPointer expression provided
*/
public static String parsePathToJsonPath(String path) throws JsonPatchException {
if (path.startsWith("$")) {
return path;
} 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('/', '.')
.replace("~1", "/") // / must be escaped in JsonPointer using ~1
.replace("~0", "~") // ~ must be escaped in JsonPointer using ~0
.replaceAll(ARRAY_ELEMENT_REGEX, "[$1]");
}
return "$" + Arrays.stream(path.replace('/', '.')
.replace("~1", "/") // / must be escaped in JsonPointer using ~1
.replace("~0", "~") // ~ must be escaped in JsonPointer using ~0
.split("\\.")) // split string to analyze every path field separately
.filter(s -> !s.isEmpty()) // skip empty string on the beginning of the array
.map(s -> s.replaceAll(ARRAY_ELEMENT_REGEX, "[$1]"))
.reduce("", (s, s2) -> s + "." + s2); //connect the path
}
}
18 changes: 18 additions & 0 deletions src/test/resources/jsonpatch/move.json
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,24 @@
},
"expensive": 10
}
},
{
"op": { "op": "move", "from": "/5gStringToMove", "path": "/5gStringMoved" },
"node": {
"5gStringToMove":"exists"
},
"expected": {
"5gStringMoved":"exists"
}
},
{
"op": { "op": "move", "from": "/String123ToMove", "path": "/StringMoved5g" },
"node": {
"String123ToMove":"exists"
},
"expected": {
"StringMoved5g":"exists"
}
}
]
}

0 comments on commit 4088b3a

Please sign in to comment.