Skip to content

Commit acb2027

Browse files
committed
bug symfony#61580 [JsonStreamer] Fix encoding iterable lists (mtarld)
This PR was merged into the 7.3 branch. Discussion ---------- [JsonStreamer] Fix encoding iterable lists | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT Sibling of symfony#61561. Handle any iterable with `int` as key as a list during encoding (which mean being encoded with brackets). Commits ------- 2057bc8 [JsonStreamer] Fix encoding iterable lists
2 parents 5ff7e23 + 2057bc8 commit acb2027

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function testWriteCollection()
105105
);
106106

107107
$this->assertWritten(
108-
'{"0":{"id":1,"name":"dummy"},"1":{"id":1,"name":"dummy"}}',
108+
'[{"id":1,"name":"dummy"},{"id":1,"name":"dummy"}]',
109109
new \ArrayObject([new ClassicDummy(), new ClassicDummy()]),
110110
Type::iterable(Type::object(ClassicDummy::class), Type::int()),
111111
);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,9 @@ private function buildYieldStatements(DataModelNodeInterface $dataModelNode, arr
294294
if ($dataModelNode instanceof CollectionNode) {
295295
++$context['depth'];
296296

297-
if ($dataModelNode->getType()->isList()) {
297+
$collectionKeyType = $dataModelNode->getType()->getCollectionKeyType();
298+
299+
if ($collectionKeyType instanceof BuiltinType && TypeIdentifier::INT === $collectionKeyType->getTypeIdentifier()) {
298300
return [
299301
new Expression(new Yield_($this->builder->val('['))),
300302
new Expression(new Assign($this->builder->var('prefix'), $this->builder->val(''))),

0 commit comments

Comments
 (0)