1
0
mirror of https://github.com/halaxa/json-machine.git synced 2025-01-18 05:28:14 +01:00

Check for wildcards when building JSON pointer paths

Consider wildcards when checking for intersecting paths
Added Unittest for intersecting paths
This commit is contained in:
Florian Wolfsjäger 2021-12-13 20:30:38 +01:00
parent ab9e786d85
commit 6849f3cf15
No known key found for this signature in database
GPG Key ID: 50B2172DEA035DFC
2 changed files with 29 additions and 3 deletions

View File

@ -71,7 +71,17 @@ class Parser implements \IteratorAggregate, PositionAware
}
$intersectingJsonPointers = array_filter($jsonPointers, static function($el) use ($jsonPointerEl) {
return $jsonPointerEl !== $el && strpos($jsonPointerEl, $el) === 0;
if ($jsonPointerEl === $el) {
return false;
}
if (strpos($jsonPointerEl, $el) === 0) {
return true;
}
$elWildcard = preg_replace('~/\d+(/|$)~', '/-$1', $el);
return strpos($jsonPointerEl, $elWildcard) === 0;
});
if (!empty($intersectingJsonPointers)) {
@ -93,18 +103,24 @@ class Parser implements \IteratorAggregate, PositionAware
*/
private function getMatchingJsonPointerPath($currentPath)
{
$matchingPointer = key($this->jsonPointerPaths);
if (count($this->jsonPointerPaths) === 1) {
$this->currentJsonPointer = $matchingPointer;
return current($this->jsonPointerPaths);
}
$matchingPointer = key($this->jsonPointerPaths);
$currentPathLength = count($currentPath);
$matchLength = -1;
foreach ($this->jsonPointerPaths as $jsonPointer => $jsonPointerPath) {
$matchingParts = [];
foreach ($jsonPointerPath as $i => $jsonPointerPathEl) {
if (!isset($currentPath[$i]) || $currentPath[$i] !== $jsonPointerPathEl) {
if (
!isset($currentPath[$i])
|| preg_replace('~/\d+(/|$)~', '/-$1', $currentPath[$i]) !== preg_replace('~/\d+(/|$)~', '/-$1', $jsonPointerPathEl)
) {
continue;
}
@ -121,6 +137,10 @@ class Parser implements \IteratorAggregate, PositionAware
$matchingPointer = $jsonPointer;
$matchLength = $currentMatchLength;
}
if ($matchLength === $currentPathLength) {
break;
}
}
$this->currentJsonPointer = $matchingPointer;

View File

@ -294,6 +294,12 @@ class ParserTest extends \PHPUnit_Framework_TestCase
}
}
public function testIntersectingPaths()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("JSON Pointers must not intersect: '/companies/-/id' is within '/companies/0/id'");
$this->createParser(new \ArrayObject(), ['/companies/-/id', '/companies/0/id']);
}
private function createParser($json, $jsonPointer = '')
{