Skip to content

Commit 8a5de03

Browse files
minor symfony#61052 [JsonStreamer] Upmerge symfony#61001 (mtarld)
This PR was merged into the 7.4 branch. Discussion ---------- [JsonStreamer] Upmerge symfony#61001 | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT Finish symfony#61001 upmerge. Commits ------- 67c06e3 [JsonStreamer] Upmerge symfony#61001
2 parents b8cce26 + 67c06e3 commit 8a5de03

File tree

8 files changed

+63
-8
lines changed

8 files changed

+63
-8
lines changed

src/Symfony/Component/JsonStreamer/DataModel/Write/CollectionNode.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ public function __construct(
2626
private string $accessor,
2727
private CollectionType $type,
2828
private DataModelNodeInterface $item,
29+
private DataModelNodeInterface $key,
2930
) {
3031
}
3132

3233
public function withAccessor(string $accessor): self
3334
{
34-
return new self($accessor, $this->type, $this->item);
35+
return new self($accessor, $this->type, $this->item, $this->key);
3536
}
3637

3738
public function getIdentifier(): string
@@ -53,4 +54,9 @@ public function getItemNode(): DataModelNodeInterface
5354
{
5455
return $this->item;
5556
}
57+
58+
public function getKeyNode(): DataModelNodeInterface
59+
{
60+
return $this->key;
61+
}
5662
}

src/Symfony/Component/JsonStreamer/Tests/DataModel/Write/CompositeNodeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function testSortNodesOnCreation()
4848
$composite = new CompositeNode('$data', [
4949
$scalar = new ScalarNode('$data', Type::int()),
5050
$object = new ObjectNode('$data', Type::object(self::class), []),
51-
$collection = new CollectionNode('$data', Type::list(), new ScalarNode('$data', Type::int())),
51+
$collection = new CollectionNode('$data', Type::list(), new ScalarNode('$data', Type::int()), new ScalarNode('$key', Type::string())),
5252
]);
5353

5454
$this->assertSame([$collection, $object, $scalar], $composite->getNodes());

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
/**
4+
* @param array<int,Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNestedArray> $data
5+
*/
36
return static function (mixed $data, \Psr\Container\ContainerInterface $valueTransformers, array $options): \Traversable {
47
try {
58
yield '[';

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
/**
4+
* @param array<int,Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithArray> $data
5+
*/
36
return static function (mixed $data, \Psr\Container\ContainerInterface $valueTransformers, array $options): \Traversable {
47
try {
58
yield '[';

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
use Symfony\Component\JsonStreamer\JsonStreamWriter;
1717
use Symfony\Component\JsonStreamer\Tests\Fixtures\Enum\DummyBackedEnum;
1818
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\ClassicDummy;
19+
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithArray;
1920
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithDateTimes;
2021
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithGenerics;
2122
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNameAttributes;
23+
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNestedArray;
2224
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNullableProperties;
2325
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithPhpDoc;
2426
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithUnionProperties;
@@ -109,6 +111,37 @@ public function testWriteCollection()
109111
);
110112
}
111113

114+
public function testWriteNestedCollection()
115+
{
116+
$dummyWithArray1 = new DummyWithArray();
117+
$dummyWithArray1->dummies = [new ClassicDummy()];
118+
$dummyWithArray1->customProperty = 'customProperty1';
119+
120+
$dummyWithArray2 = new DummyWithArray();
121+
$dummyWithArray2->dummies = [new ClassicDummy()];
122+
$dummyWithArray2->customProperty = 'customProperty2';
123+
124+
$this->assertWritten(
125+
'[{"dummies":[{"id":1,"name":"dummy"}],"customProperty":"customProperty1"},{"dummies":[{"id":1,"name":"dummy"}],"customProperty":"customProperty2"}]',
126+
[$dummyWithArray1, $dummyWithArray2],
127+
Type::list(Type::object(DummyWithArray::class)),
128+
);
129+
130+
$dummyWithNestedArray1 = new DummyWithNestedArray();
131+
$dummyWithNestedArray1->dummies = [$dummyWithArray1];
132+
$dummyWithNestedArray1->stringProperty = 'stringProperty1';
133+
134+
$dummyWithNestedArray2 = new DummyWithNestedArray();
135+
$dummyWithNestedArray2->dummies = [$dummyWithArray2];
136+
$dummyWithNestedArray2->stringProperty = 'stringProperty2';
137+
138+
$this->assertWritten(
139+
'[{"dummies":[{"dummies":[{"id":1,"name":"dummy"}],"customProperty":"customProperty1"}],"stringProperty":"stringProperty1"},{"dummies":[{"dummies":[{"id":1,"name":"dummy"}],"customProperty":"customProperty2"}],"stringProperty":"stringProperty2"}]',
140+
[$dummyWithNestedArray1, $dummyWithNestedArray2],
141+
Type::list(Type::object(DummyWithNestedArray::class)),
142+
);
143+
}
144+
112145
public function testWriteObject()
113146
{
114147
$dummy = new ClassicDummy();

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
use Symfony\Component\JsonStreamer\Tests\Fixtures\Enum\DummyBackedEnum;
2222
use Symfony\Component\JsonStreamer\Tests\Fixtures\Enum\DummyEnum;
2323
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\ClassicDummy;
24+
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithArray;
2425
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNameAttributes;
26+
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNestedArray;
2527
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithOtherDummies;
2628
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithUnionProperties;
2729
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithValueTransformerAttributes;
@@ -93,6 +95,8 @@ public static function generatedStreamWriterDataProvider(): iterable
9395
yield ['null_list', Type::list(Type::null())];
9496
yield ['object_list', Type::list(Type::object(DummyWithNameAttributes::class))];
9597
yield ['nullable_object_list', Type::nullable(Type::list(Type::object(DummyWithNameAttributes::class)))];
98+
yield ['nested_list', Type::list(Type::object(DummyWithArray::class))];
99+
yield ['double_nested_list', Type::list(Type::object(DummyWithNestedArray::class))];
96100

97101
yield ['dict', Type::dict()];
98102
yield ['object_dict', Type::dict(Type::object(DummyWithNameAttributes::class))];
@@ -141,6 +145,7 @@ public function testCallPropertyMetadataLoaderWithProperContext()
141145
->with(self::class, [], [
142146
'original_type' => $type,
143147
'generated_classes' => [self::class => true],
148+
'depth' => 0,
144149
])
145150
->willReturn([]);
146151

src/Symfony/Component/JsonStreamer/Write/PhpGenerator.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,18 +209,20 @@ private function generateYields(DataModelNodeInterface $dataModelNode, array $op
209209
.$this->yieldString(']', $context);
210210
}
211211

212+
$keyAccessor = $dataModelNode->getKeyNode()->getAccessor();
213+
212214
$escapedKey = $dataModelNode->getType()->getCollectionKeyType()->isIdentifiedBy(TypeIdentifier::INT)
213-
? '$key = is_int($key) ? $key : \substr(\json_encode($key), 1, -1);'
214-
: '$key = \substr(\json_encode($key), 1, -1);';
215+
? "$keyAccessor = is_int($keyAccessor) ? $keyAccessor : \substr(\json_encode($keyAccessor), 1, -1);"
216+
: "$keyAccessor = \substr(\json_encode($keyAccessor), 1, -1);";
215217

216218
$php = $this->yieldString('{', $context)
217219
.$this->flushYieldBuffer($context)
218220
.$this->line('$prefix = \'\';', $context)
219-
.$this->line("foreach ($accessor as \$key => ".$dataModelNode->getItemNode()->getAccessor().') {', $context);
221+
.$this->line("foreach ($accessor as $keyAccessor => ".$dataModelNode->getItemNode()->getAccessor().') {', $context);
220222

221223
++$context['indentation_level'];
222224
$php .= $this->line($escapedKey, $context)
223-
.$this->yield('"{$prefix}\"{$key}\":"', $context)
225+
.$this->yield('"{$prefix}\"{'.$keyAccessor.'}\":"', $context)
224226
.$this->generateYields($dataModelNode->getItemNode(), $options, $context)
225227
.$this->flushYieldBuffer($context)
226228
.$this->line('$prefix = \',\';', $context);

src/Symfony/Component/JsonStreamer/Write/StreamWriterGenerator.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function generate(Type $type, array $options = []): string
6464
$this->phpGenerator ??= new PhpGenerator();
6565
$this->fs ??= new Filesystem();
6666

67-
$dataModel = $this->createDataModel($type, '$data', $options);
67+
$dataModel = $this->createDataModel($type, '$data', $options, ['depth' => 0]);
6868
$php = $this->phpGenerator->generate($dataModel, $options);
6969

7070
if (!$this->fs->exists($this->streamWritersDir)) {
@@ -164,10 +164,13 @@ private function createDataModel(Type $type, string $accessor, array $options =
164164
}
165165

166166
if ($type instanceof CollectionType) {
167+
++$context['depth'];
168+
167169
return new CollectionNode(
168170
$accessor,
169171
$type,
170-
$this->createDataModel($type->getCollectionValueType(), '$value', $options, $context),
172+
$this->createDataModel($type->getCollectionValueType(), '$value'.$context['depth'], $options, $context),
173+
$this->createDataModel($type->getCollectionKeyType(), '$key'.$context['depth'], $options, $context),
171174
);
172175
}
173176

0 commit comments

Comments
 (0)