Skip to content

Commit 3453946

Browse files
Fix usage of class public properties object in structure
1 parent 7d8700f commit 3453946

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/Symfony/Component/HttpFoundation/StreamedJsonResponse.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ private function stream(): void
7474
if (self::PLACEHOLDER === $key) {
7575
// if the placeholder is already in the structure it should be replaced with a new one that explode
7676
// works like expected for the structure
77-
$generators[] = $item;
77+
$generators[] = $key;
7878
}
7979

80-
// generators should be used but for better DX all kind of Traversable are supported
81-
if ($item instanceof \Traversable || $item instanceof \JsonSerializable) {
80+
// generators should be used but for better DX all kind of Traversable and objects are supported
81+
if (is_object($item)) {
8282
$generators[] = $item;
8383
$item = self::PLACEHOLDER;
8484
} elseif (self::PLACEHOLDER === $item) {
@@ -97,7 +97,7 @@ private function stream(): void
9797
// send first and between parts of the structure
9898
echo $jsonParts[$index];
9999

100-
if (self::PLACEHOLDER === $generator || $generator instanceof \JsonSerializable) {
100+
if ($generator instanceof \JsonSerializable || !$generator instanceof \Traversable) {
101101
// the placeholders and JsonSerializable items in the structure are rendered here
102102
echo json_encode($generator, $jsonEncodingOptions);
103103

@@ -106,6 +106,7 @@ private function stream(): void
106106

107107
$isFirstItem = true;
108108
$startTag = '[';
109+
109110
foreach ($generator as $key => $item) {
110111
if ($isFirstItem) {
111112
$isFirstItem = false;

src/Symfony/Component/HttpFoundation/Tests/StreamedJsonResponseTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,25 @@ public function testResponseStatusCode()
155155
$this->assertSame(201, $response->getStatusCode());
156156
}
157157

158+
public function testPlaceholderAsObjectStructure()
159+
{
160+
$object = new class() {
161+
public $__symfony_json__ = 'foo';
162+
public $bar = '__symfony_json__';
163+
};
164+
165+
$content = $this->createSendResponse(
166+
[
167+
'object' => $object,
168+
// add a Generator to make sure it still work in combination with other object holding placeholders
169+
'articles' => $this->generatorArray('Article'),
170+
],
171+
);
172+
173+
$this->assertSame('{"object":{"__symfony_json__":"foo","bar":"__symfony_json__"},"articles":[{"title":"Article 1"},{"title":"Article 2"},{"title":"Article 3"}]}', $content);
174+
}
175+
176+
158177
public function testResponseHeaders()
159178
{
160179
$response = new StreamedJsonResponse([], 200, ['X-Test' => 'Test']);

0 commit comments

Comments
 (0)