Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c97b1d

Browse files
committed
[JsonStreamer] Fix nested generated foreach loops
Fix #60984
1 parent d8a673a commit 6c97b1d

16 files changed

+168
-32
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ public function __construct(
2727
private DataAccessorInterface $accessor,
2828
private CollectionType $type,
2929
private DataModelNodeInterface $item,
30+
private DataModelNodeInterface $key,
3031
) {
3132
}
3233

3334
public function withAccessor(DataAccessorInterface $accessor): self
3435
{
35-
return new self($accessor, $this->type, $this->item);
36+
return new self($accessor, $this->type, $this->item, $this->key);
3637
}
3738

3839
public function getIdentifier(): string
@@ -54,4 +55,9 @@ public function getItemNode(): DataModelNodeInterface
5455
{
5556
return $this->item;
5657
}
58+
59+
public function getKeyNode(): DataModelNodeInterface
60+
{
61+
return $this->key;
62+
}
5763
}

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

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

5555
$this->assertSame([$collection, $object, $scalar], $composite->getNodes());
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 DummyWithArray
6+
{
7+
/** @var ClassicDummy[] */
8+
public array $dummies;
9+
10+
public string $customProperty;
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 DummyWithNestedArray
6+
{
7+
/** @var DummyWithArray[] */
8+
public array $dummies;
9+
10+
public string $stringProperty;
11+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
return static function (mixed $data, \Psr\Container\ContainerInterface $valueTransformers, array $options): \Traversable {
4+
try {
5+
yield '[';
6+
$prefix = '';
7+
foreach ($data as $value1) {
8+
yield $prefix;
9+
yield '{"dummies":[';
10+
$prefix = '';
11+
foreach ($value1->dummies as $value2) {
12+
yield $prefix;
13+
yield '{"dummies":[';
14+
$prefix = '';
15+
foreach ($value2->dummies as $value3) {
16+
yield $prefix;
17+
yield '{"id":';
18+
yield \json_encode($value3->id, \JSON_THROW_ON_ERROR, 506);
19+
yield ',"name":';
20+
yield \json_encode($value3->name, \JSON_THROW_ON_ERROR, 506);
21+
yield '}';
22+
$prefix = ',';
23+
}
24+
yield '],"customProperty":';
25+
yield \json_encode($value2->customProperty, \JSON_THROW_ON_ERROR, 508);
26+
yield '}';
27+
$prefix = ',';
28+
}
29+
yield '],"stringProperty":';
30+
yield \json_encode($value1->stringProperty, \JSON_THROW_ON_ERROR, 510);
31+
yield '}';
32+
$prefix = ',';
33+
}
34+
yield ']';
35+
} catch (\JsonException $e) {
36+
throw new \Symfony\Component\JsonStreamer\Exception\NotEncodableValueException($e->getMessage(), 0, $e);
37+
}
38+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
return static function (mixed $data, \Psr\Container\ContainerInterface $valueTransformers, array $options): \Traversable {
4+
try {
5+
yield '[';
6+
$prefix = '';
7+
foreach ($data as $value1) {
8+
yield $prefix;
9+
yield '{"dummies":[';
10+
$prefix = '';
11+
foreach ($value1->dummies as $value2) {
12+
yield $prefix;
13+
yield '{"id":';
14+
yield \json_encode($value2->id, \JSON_THROW_ON_ERROR, 508);
15+
yield ',"name":';
16+
yield \json_encode($value2->name, \JSON_THROW_ON_ERROR, 508);
17+
yield '}';
18+
$prefix = ',';
19+
}
20+
yield '],"customProperty":';
21+
yield \json_encode($value1->customProperty, \JSON_THROW_ON_ERROR, 510);
22+
yield '}';
23+
$prefix = ',';
24+
}
25+
yield ']';
26+
} catch (\JsonException $e) {
27+
throw new \Symfony\Component\JsonStreamer\Exception\NotEncodableValueException($e->getMessage(), 0, $e);
28+
}
29+
};

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
if (\is_array($data)) {
66
yield '{';
77
$prefix = '';
8-
foreach ($data as $key => $value) {
9-
$key = \substr(\json_encode($key), 1, -1);
10-
yield "{$prefix}\"{$key}\":";
8+
foreach ($data as $key1 => $value1) {
9+
$key1 = \substr(\json_encode($key1), 1, -1);
10+
yield "{$prefix}\"{$key1}\":";
1111
yield '{"@id":';
12-
yield \json_encode($value->id, \JSON_THROW_ON_ERROR, 510);
12+
yield \json_encode($value1->id, \JSON_THROW_ON_ERROR, 510);
1313
yield ',"name":';
14-
yield \json_encode($value->name, \JSON_THROW_ON_ERROR, 510);
14+
yield \json_encode($value1->name, \JSON_THROW_ON_ERROR, 510);
1515
yield '}';
1616
$prefix = ',';
1717
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
if (\is_array($data)) {
66
yield '[';
77
$prefix = '';
8-
foreach ($data as $value) {
8+
foreach ($data as $value1) {
99
yield $prefix;
1010
yield '{"@id":';
11-
yield \json_encode($value->id, \JSON_THROW_ON_ERROR, 510);
11+
yield \json_encode($value1->id, \JSON_THROW_ON_ERROR, 510);
1212
yield ',"name":';
13-
yield \json_encode($value->name, \JSON_THROW_ON_ERROR, 510);
13+
yield \json_encode($value1->name, \JSON_THROW_ON_ERROR, 510);
1414
yield '}';
1515
$prefix = ',';
1616
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
try {
55
yield '{';
66
$prefix = '';
7-
foreach ($data as $key => $value) {
8-
$key = \substr(\json_encode($key), 1, -1);
9-
yield "{$prefix}\"{$key}\":";
7+
foreach ($data as $key1 => $value1) {
8+
$key1 = \substr(\json_encode($key1), 1, -1);
9+
yield "{$prefix}\"{$key1}\":";
1010
yield '{"@id":';
11-
yield \json_encode($value->id, \JSON_THROW_ON_ERROR, 510);
11+
yield \json_encode($value1->id, \JSON_THROW_ON_ERROR, 510);
1212
yield ',"name":';
13-
yield \json_encode($value->name, \JSON_THROW_ON_ERROR, 510);
13+
yield \json_encode($value1->name, \JSON_THROW_ON_ERROR, 510);
1414
yield '}';
1515
$prefix = ',';
1616
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
try {
55
yield '{';
66
$prefix = '';
7-
foreach ($data as $key => $value) {
8-
$key = is_int($key) ? $key : \substr(\json_encode($key), 1, -1);
9-
yield "{$prefix}\"{$key}\":";
7+
foreach ($data as $key1 => $value1) {
8+
$key1 = is_int($key1) ? $key1 : \substr(\json_encode($key1), 1, -1);
9+
yield "{$prefix}\"{$key1}\":";
1010
yield '{"id":';
11-
yield \json_encode($value->id, \JSON_THROW_ON_ERROR, 510);
11+
yield \json_encode($value1->id, \JSON_THROW_ON_ERROR, 510);
1212
yield ',"name":';
13-
yield \json_encode($value->name, \JSON_THROW_ON_ERROR, 510);
13+
yield \json_encode($value1->name, \JSON_THROW_ON_ERROR, 510);
1414
yield '}';
1515
$prefix = ',';
1616
}

0 commit comments

Comments
 (0)