Skip to content

Commit 0834f9a

Browse files
bug #62622 [JsonStreamer] fix invalid json output for list of self (DjordyKoert)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- [JsonStreamer] fix invalid json output for list of self | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | yes | New feature? | no <!-- if yes, also update src/**/CHANGELOG.md --> | Deprecations? | no <!-- if yes, also update UPGRADE-*.md and src/**/CHANGELOG.md --> | Issues | Fix #... <!-- prefix each issue number with "Fix #"; no need to create an issue if none exists, explain below --> | License | MIT Using the `symfony/json-streamer` component on an object which contains a list of self (`list<int, self>`, `self[]`) generates invalid JSON. This happens because the generated code does not yield a comma (`,`) for the first entry in the list and instead outputs an additional comma at the end of the list. A class like: ```php class Dummy { /** `@var` Dummy[] */ public array $dummies = []; } ``` Currently outputs invalid JSON: ```json {"dummies":[{"dummies":[]}{"dummies":[]},{"dummies":[]},]} ``` This PR fixes the above example to output valid JSON: ```json {"dummies":[{"dummies":[]},{"dummies":[]},{"dummies":[]}]} ``` Commits ------- 5e371e7 [JsonStreamer] fix invalid json output for list of self
2 parents b8b7c3e + 5e371e7 commit 0834f9a

11 files changed

+218
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Fixtures\Model;
4+
5+
namespace Symfony\Component\JsonStreamer\Tests\Fixtures\Model;
6+
7+
class DummyWithNestedDictDummies
8+
{
9+
/** @var array<string, DummyWithNestedDictDummies> */
10+
public array $dummies = [];
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Fixtures\Model;
4+
5+
namespace Symfony\Component\JsonStreamer\Tests\Fixtures\Model;
6+
7+
class DummyWithNestedListDummies
8+
{
9+
/** @var DummyWithNestedListDummies[] */
10+
public array $dummies = [];
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\Component\JsonStreamer\Tests\Fixtures\Model;
4+
5+
class SelfReferencingDummyDict
6+
{
7+
/**
8+
* @var array<string, self>
9+
*/
10+
public array $items = [];
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\Component\JsonStreamer\Tests\Fixtures\Model;
4+
5+
class SelfReferencingDummyList
6+
{
7+
/**
8+
* @var self[]
9+
*/
10+
public array $items = [];
11+
}

src/Symfony/Component/JsonStreamer/Tests/Fixtures/stream_writer/object_with_nested_dict_self.php

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Symfony/Component/JsonStreamer/Tests/Fixtures/stream_writer/object_with_nested_list_self.php

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Symfony/Component/JsonStreamer/Tests/Fixtures/stream_writer/self_referencing_object_dict.php

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Symfony/Component/JsonStreamer/Tests/Fixtures/stream_writer/self_referencing_object_list.php

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Symfony/Component/JsonStreamer/Tests/JsonStreamWriterTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@
2525
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithGenerics;
2626
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNameAttributes;
2727
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNestedArray;
28+
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNestedDictDummies;
29+
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNestedListDummies;
2830
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNullableProperties;
2931
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithPhpDoc;
3032
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithSyntheticProperties;
3133
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithUnionProperties;
3234
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithValueTransformerAttributes;
3335
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\SelfReferencingDummy;
36+
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\SelfReferencingDummyDict;
37+
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\SelfReferencingDummyList;
3438
use Symfony\Component\JsonStreamer\Tests\Fixtures\ValueTransformer\BooleanToStringValueTransformer;
3539
use Symfony\Component\JsonStreamer\Tests\Fixtures\ValueTransformer\DoubleIntAndCastToStringValueTransformer;
3640
use Symfony\Component\JsonStreamer\ValueTransformer\DateTimeToStringValueTransformer;
@@ -283,6 +287,56 @@ public function testWriteObjectWithSyntheticProperty()
283287
$this->assertSame('{"synthetic":true}', (string) $writer->write(new DummyWithSyntheticProperties(), Type::object(DummyWithSyntheticProperties::class)));
284288
}
285289

290+
public function testWriteNestedSelfList()
291+
{
292+
$dummy = new SelfReferencingDummyList();
293+
$dummy->items = [new SelfReferencingDummyList(), new SelfReferencingDummyList(), new SelfReferencingDummyList()];
294+
295+
$this->assertWritten(
296+
'{"items":[{"items":[]},{"items":[]},{"items":[]}]}',
297+
$dummy,
298+
Type::object(SelfReferencingDummyList::class)
299+
);
300+
301+
$dummy = new DummyWithNestedListDummies();
302+
$dummy->dummies = [new DummyWithNestedListDummies(), new DummyWithNestedListDummies(), new DummyWithNestedListDummies()];
303+
304+
$this->assertWritten(
305+
'{"dummies":[{"dummies":[]},{"dummies":[]},{"dummies":[]}]}',
306+
$dummy,
307+
Type::object(DummyWithNestedListDummies::class)
308+
);
309+
}
310+
311+
public function testWriteNestedSelfDict()
312+
{
313+
$dummy = new SelfReferencingDummyDict();
314+
$dummy->items = [
315+
'first' => new SelfReferencingDummyDict(),
316+
'second' => new SelfReferencingDummyDict(),
317+
'third' => new SelfReferencingDummyDict(),
318+
];
319+
320+
$this->assertWritten(
321+
'{"items":{"first":{"items":{}},"second":{"items":{}},"third":{"items":{}}}}',
322+
$dummy,
323+
Type::object(SelfReferencingDummyDict::class)
324+
);
325+
326+
$dummy = new DummyWithNestedDictDummies();
327+
$dummy->dummies = [
328+
'first' => new DummyWithNestedDictDummies(),
329+
'second' => new DummyWithNestedDictDummies(),
330+
'third' => new DummyWithNestedDictDummies(),
331+
];
332+
333+
$this->assertWritten(
334+
'{"dummies":{"first":{"dummies":{}},"second":{"dummies":{}},"third":{"dummies":{}}}}',
335+
$dummy,
336+
Type::object(DummyWithNestedDictDummies::class)
337+
);
338+
}
339+
286340
#[DataProvider('throwWhenMaxDepthIsReachedDataProvider')]
287341
public function testThrowWhenMaxDepthIsReached(Type $type, mixed $data)
288342
{

src/Symfony/Component/JsonStreamer/Tests/Write/StreamWriterGeneratorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@
2727
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithDollarNamedProperties;
2828
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNameAttributes;
2929
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNestedArray;
30+
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNestedDictDummies;
31+
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNestedListDummies;
3032
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithOtherDummies;
3133
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithSyntheticProperties;
3234
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithUnionProperties;
3335
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithValueTransformerAttributes;
3436
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\SelfReferencingDummy;
37+
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\SelfReferencingDummyDict;
38+
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\SelfReferencingDummyList;
3539
use Symfony\Component\JsonStreamer\Tests\Fixtures\ValueTransformer\BooleanToStringValueTransformer;
3640
use Symfony\Component\JsonStreamer\Tests\Fixtures\ValueTransformer\DoubleIntAndCastToStringValueTransformer;
3741
use Symfony\Component\JsonStreamer\Tests\ServiceContainer;
@@ -99,6 +103,8 @@ public static function generatedStreamWriterDataProvider(): iterable
99103
yield ['nullable_object_list', Type::nullable(Type::list(Type::object(DummyWithNameAttributes::class)))];
100104
yield ['nested_list', Type::list(Type::object(DummyWithArray::class))];
101105
yield ['double_nested_list', Type::list(Type::object(DummyWithNestedArray::class))];
106+
yield ['object_with_nested_list_self', Type::object(DummyWithNestedListDummies::class)];
107+
yield ['object_with_nested_dict_self', Type::object(DummyWithNestedDictDummies::class)];
102108

103109
yield ['dict', Type::dict()];
104110
yield ['object_dict', Type::dict(Type::object(DummyWithNameAttributes::class))];
@@ -112,6 +118,8 @@ public static function generatedStreamWriterDataProvider(): iterable
112118
yield ['object_in_object', Type::object(DummyWithOtherDummies::class)];
113119
yield ['object_with_value_transformer', Type::object(DummyWithValueTransformerAttributes::class)];
114120
yield ['self_referencing_object', Type::object(SelfReferencingDummy::class)];
121+
yield ['self_referencing_object_list', Type::object(SelfReferencingDummyList::class)];
122+
yield ['self_referencing_object_dict', Type::object(SelfReferencingDummyDict::class)];
115123
yield ['object_with_dollar_named_properties', Type::object(DummyWithDollarNamedProperties::class)];
116124
yield ['object_with_synthetic_properties', Type::object(DummyWithSyntheticProperties::class), new SyntheticPropertyMetadataLoader()];
117125

0 commit comments

Comments
 (0)