Skip to content

Commit 06c0c4c

Browse files
authored
Merge pull request #26 from square/nullparent
If a parent object on the same path was previously set but was null and other properties need to still set that path to set themselves because they're not marked with `omit_empty: true`, allow nesting deeper
2 parents 59e0b50 + ed12cfe commit 06c0c4c

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/Json.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function appendValue(array &$data, $value)
168168
throw new \Exception('invalid path: '.json_encode($this->path));
169169
}
170170

171-
if (!array_key_exists($pathBit, $d) && $i < $max) {
171+
if ((!array_key_exists($pathBit, $d) || is_null($d[$pathBit])) && $i < $max) {
172172
$d[$pathBit] = [];
173173
}
174174

tests/Definitions/Child.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Square\Pjson\Tests\Definitions;
4+
5+
use Square\Pjson\Json;
6+
use Square\Pjson\JsonSerialize;
7+
8+
/**
9+
* Keep properties in this exact order in this class. We want to confirm valid serialization when serializing `parent`
10+
* set that value to `null` but something further down needs to set a nested property there.
11+
*/
12+
// phpcs:ignore PSR1.Classes.ClassDeclaration
13+
class Child
14+
{
15+
use JsonSerialize;
16+
17+
#[Json("identifier")]
18+
public $id;
19+
20+
#[Json(["parent"], omit_empty: true)]
21+
public ?ParentObject $parent = null;
22+
23+
#[Json(["parent", "id"])]
24+
public ?string $parentId = null;
25+
}

tests/Definitions/ParentObject.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Square\Pjson\Tests\Definitions;
4+
5+
use Square\Pjson\Json;
6+
use Square\Pjson\JsonSerialize;
7+
8+
// phpcs:ignore PSR1.Classes.ClassDeclaration
9+
class ParentObject
10+
{
11+
use JsonSerialize;
12+
13+
#[Json("identifier")]
14+
public $id;
15+
}

tests/SerializationTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Square\Pjson\Tests\Definitions\CatalogItem;
1111
use Square\Pjson\Tests\Definitions\CatalogObject;
1212
use Square\Pjson\Tests\Definitions\Category;
13+
use Square\Pjson\Tests\Definitions\Child;
1314
use Square\Pjson\Tests\Definitions\Collector;
1415
use Square\Pjson\Tests\Definitions\MenuList;
1516
use Square\Pjson\Tests\Definitions\Privateer;
@@ -286,4 +287,14 @@ public function testCollections()
286287

287288
$this->assertEquals(json_encode(json_decode($json)), $data->toJson());
288289
}
290+
291+
public function testMissingParent()
292+
{
293+
// Ensure that while the `parent` object is null, we can still serialize the parent.id property which is
294+
// nested under `parent`.
295+
$data = new Child();
296+
$json = '{"identifier":null,"parent":{"id":null}}';
297+
298+
$this->assertEquals(json_encode(json_decode($json)), $data->toJson());
299+
}
289300
}

0 commit comments

Comments
 (0)