Skip to content

Commit

Permalink
Breaking change: Change how the recursive operator works
Browse files Browse the repository at this point in the history
I found a bug in how the recursive operator works (thanks to issue 64).
It was only using the latest selected result (not the whole array) to
apply the recursive search on.

Once this was fixed, two existing tests failed. Both using the same
JsonPath `$.store..*[?(@..model == null)].color` on the following JSON
document:

    { "store": {
        "book": [
          { "category": "reference",
            "author": "Nigel Rees",
            "title": "Sayings of the Century",
            "price": 8.95,
            "available": true
          },
          { "category": "fiction",
            "author": "Evelyn Waugh",
            "title": "Sword of Honour",
            "price": 12.99,
            "available": false
          },
          { "category": "fiction",
            "author": "Herman Melville",
            "title": "Moby Dick",
            "isbn": "0-553-21311-3",
            "price": 8.99,
            "available": true
          },
          { "category": "fiction",
            "author": "J. R. R. Tolkien",
            "title": "The Lord of the Rings",
            "isbn": "0-395-19395-8",
            "price": 22.99,
            "available": false
          }
        ],
        "bicycle": {
          "color": "red",
          "price": 19.95,
          "available": true,
          "model": null,
          "sku-number": "BCCLE-0001-RD"
        }
      },
      "authors": [
        "Nigel Rees",
        "Evelyn Waugh",
        "Herman Melville",
        "J. R. R. Tolkien"
      ],
      "Bike models": [
        1,
        2,
        3
      ]
    }

After a long analysis I have realized that the test was wrong. Let's
break that down:

1. `$` selects the whole json.

2. `.store` selects the value for the key "store":

    {
      "book": [
        { "category": "reference",
          "author": "Nigel Rees",
          "title": "Sayings of the Century",
          "price": 8.95,
          "available": true
        },
        { "category": "fiction",
          "author": "Evelyn Waugh",
          "title": "Sword of Honour",
          "price": 12.99,
          "available": false
        },
        { "category": "fiction",
          "author": "Herman Melville",
          "title": "Moby Dick",
          "isbn": "0-553-21311-3",
          "price": 8.99,
          "available": true
        },
        { "category": "fiction",
          "author": "J. R. R. Tolkien",
          "title": "The Lord of the Rings",
          "isbn": "0-395-19395-8",
          "price": 22.99,
          "available": false
        }
      ],
      "bicycle": {
        "color": "red",
        "price": 19.95,
        "available": true,
        "model": null,
        "sku-number": "BCCLE-0001-RD"
      }
    }

3. `..*` selects for all values recursively, all their children (this is
   equivalent to doing `.*` and then `.*.*` and then `.*.*.*` etc.).

For the object in `$.store..*` it will return:

    [
      {
        "color": "red",
        "price": 19.95,
        "available": true,
        "model": null,
        "sku-number": "BCCLE-0001-RD"
      },
      "red",
      19.95,
      true,
      null,
      "BCCLE-0001-RD",
      ...
      // the list of books,
      // all the book objects...,
      // all the properties for all the books...
    ]

4. Let's take the first result above and apply the next operation of the
   query: `[?(@..model == null)]`. This means "select the children that
   contain any field, recursively, with name 'model' and value null".

The children of

    {
      "color": "red",
      "price": 19.95,
      "available": true,
      "model": null,
      "sku-number": "BCCLE-0001-RD"
    }

are the following:

    [
      "red",
      19.95,
      true,
      null,
      "BCCLE-0001-RD"
    ]

Neither of which have a member named "model", let alone with value null,
yet the test expected a successful result for the query.

The correct query would be `$.store..[?(@..model == null)].color` to
avoid an extra step down with the `*` after the `..`.

Having a Child Selection operation just after a Recursive Selection one
was unsupported but this has been implemented as part of this commit.
  • Loading branch information
Galbar committed Mar 3, 2023
1 parent b0c41b1 commit 5ac1783
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ childpath = '@' operator*
operator = (childname | childfilter | recursive) operator*
childname = '.' (var_name | '*')
recursive = '..' (var_name | '*')
childfilter = '[' ('*' | namelist | indexlist | arrayslice | filterexpr) ']'
recursive = '..' (var_name | childfilter | '*')
namelist = var_name (',' (var_name | '\'' .*? '\'' | '"' .*? '"'))*
indexlist = index (',' index)*
Expand Down
14 changes: 11 additions & 3 deletions src/Galbar/JsonPath/JsonPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,21 @@ public static function subtreeGet(&$root, &$partial, $jsonPath, $createInexisten
$jsonPath = $match[1];
}
} else if (preg_match(Language\Regex::RECURSIVE_SELECTOR, $jsonPath, $match)) {
list($result, $newHasDiverged) = Operation\GetRecursive::apply($partial, $match[1]);
$newSelection = array_merge($newSelection, $result);
$recursivePath = $match[1];
if ($recursivePath[0] === '[') {
$recursivePath = "$$recursivePath";
} else {
$recursivePath = "$.$recursivePath";
}
foreach ($selection as &$partial) {
list($result, $newHasDiverged) = Operation\GetRecursive::apply($root, $partial, $recursivePath);
$newSelection = array_merge($newSelection, $result);
}
if (empty($newSelection)) {
$selection = false;
break;
} else {
$jsonPath = $match[2];
$jsonPath = "";
}
} else {
throw new \JsonPath\InvalidJsonPathException($jsonPath);
Expand Down
2 changes: 1 addition & 1 deletion src/Galbar/JsonPath/Language/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Regex

// Child regex
const CHILD_NAME = '/^\.([\p{L}\_\$][\w\-\$]*|\*)(.*)/u';
const RECURSIVE_SELECTOR = '/^\.\.([\p{L}\_\$][\w\-\$]*|\*)(.*)/u';
const RECURSIVE_SELECTOR = '/^\.\.(.+)/u';

// Array expressions
const ARRAY_INTERVAL = '/^(?:(-?\d*:-?\d*)|(-?\d*:-?\d*:-?\d*))$/';
Expand Down
19 changes: 12 additions & 7 deletions src/Galbar/JsonPath/Operation/GetRecursive.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@

class GetRecursive
{
public static function apply(&$jsonObject, $childName)
public static function apply(&$root, &$partial, $jsonPath)
{
list($result, $_) = GetChild::apply($jsonObject, $childName);
if (is_array($jsonObject)) {
foreach ($jsonObject as &$item) {
list($localResult, $_) = GetRecursive::apply($item, $childName);
$result = array_merge($result, $localResult);
}
list($result, $_) = \JsonPath\JsonPath::subtreeGet($root, $partial, $jsonPath);
if ($result === false) {
$result = array();
}

if (!is_array($partial)) {
return array($result, true);
}
foreach ($partial as &$item) {
list($localResult, $_) = GetRecursive::apply($root, $item, $jsonPath);
$result = array_merge($result, $localResult);
}
return array($result, true);
}
Expand Down
70 changes: 70 additions & 0 deletions tests/Galbar/JsonPath/JsonObjectIssue64Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Copyright 2023 Alessio Linares
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Tests;

use JsonPath\InvalidJsonException;
use JsonPath\InvalidJsonPathException;
use JsonPath\JsonObject;

/**
* Class JsonObjectTest
* @author Alessio Linares
*/
class JsonObjectIssue64Test extends \PHPUnit_Framework_TestCase
{

private $json = '
{
"success": true,
"result": [
{
"data": {
"someField": "value1",
"nestedObj": {
"someField": "otherValue1"
}
}
},
{
"data": {
"someField": "value2",
"nestedObj": {
"someField": "otherValue2"
}
}
},
{
"data": {
"someField": "value3",
"nestedObj": {
"someField": "otherValue3"
}
}
}
]
}
';

public function testCase1()
{
$jsonObject = new JsonObject($this->json);
$result = $jsonObject->get('$.result[*].data..someField');
$expected = ["value1", "otherValue1", "value2", "otherValue2", "value3", "otherValue3"];
$this->assertEquals($expected, $result);
}
}
4 changes: 2 additions & 2 deletions tests/Galbar/JsonPath/JsonObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ public function testGetProvider()
array(
"red"
),
"$.store..*[?(@..model == null)].color"
"$.store..[?(@..model == null)].color"
),
array(
array(
Expand Down Expand Up @@ -732,7 +732,7 @@ public function testSmartGetProvider()
array(
"red"
),
"$.store..*[?(@..model == null)].color"
"$.store..[?(@..model == null)].color"
),
array(
array(1, 2, 3),
Expand Down

0 comments on commit 5ac1783

Please sign in to comment.