Skip to content

Commit 42bad65

Browse files
bug #62153 [HttpFoundation] Fix issue where ServerEvent with "0" data is not sent (santysisi)
This PR was merged into the 7.3 branch. Discussion ---------- [HttpFoundation] Fix issue where ServerEvent with "0" data is not sent | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #62151 | License | MIT Fixes an issue where data with a string value of '0' was not being sent correctly. This update ensures that the '0' string is handled properly when sending data. Commits ------- b81271e [HttpFoundation] Fix issue where ServerEvent with "0" data is not sent
2 parents eb65faf + b81271e commit 42bad65

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/Symfony/Component/HttpFoundation/ServerEvent.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,12 @@ public function getIterator(): \Traversable
132132
}
133133
yield $head;
134134

135-
if ($this->data) {
136-
if (is_iterable($this->data)) {
137-
foreach ($this->data as $data) {
138-
yield \sprintf('data: %s', $data)."\n";
139-
}
140-
} else {
141-
yield \sprintf('data: %s', $this->data)."\n";
135+
if (is_iterable($this->data)) {
136+
foreach ($this->data as $data) {
137+
yield \sprintf('data: %s', $data)."\n";
142138
}
139+
} elseif ('' !== $this->data) {
140+
yield \sprintf('data: %s', $this->data)."\n";
143141
}
144142

145143
yield "\n";

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,28 @@ public function testStreamEventWithSendMethod()
116116
$this->assertSameResponseContent("data: foo\n\n", $response);
117117
}
118118

119+
public function testStreamEventWith0Data()
120+
{
121+
$response = new EventStreamResponse(function () {
122+
yield new ServerEvent(
123+
data: '0',
124+
);
125+
});
126+
127+
$this->assertSameResponseContent("data: 0\n\n", $response);
128+
}
129+
130+
public function testStreamEventEmptyStringIgnored()
131+
{
132+
$response = new EventStreamResponse(function () {
133+
yield new ServerEvent(
134+
data: '',
135+
);
136+
});
137+
138+
$this->assertSameResponseContent("\n", $response);
139+
}
140+
119141
private function assertSameResponseContent(string $expected, EventStreamResponse $response): void
120142
{
121143
ob_start();

0 commit comments

Comments
 (0)