Skip to content

Commit 8ba54c1

Browse files
jderussenicolas-grekas
authored andcommitted
Fix nesteed stream in AsyncResponse
1 parent 62d40d5 commit 8ba54c1

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Symfony/Component/HttpClient/Response/AsyncResponse.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ final class AsyncResponse implements ResponseInterface, StreamableInterface
3434
private $response;
3535
private $info = ['canceled' => false];
3636
private $passthru;
37+
private $lastYielded = false;
3738

3839
/**
3940
* @param ?callable(ChunkInterface, AsyncContext): ?\Iterator $passthru
@@ -255,7 +256,10 @@ public static function stream(iterable $responses, float $timeout = null, string
255256
}
256257
}
257258

258-
if (null === $chunk->getError() && !$chunk->isLast() && $r->response === $response && null !== $r->client) {
259+
if (null === $chunk->getError() && $chunk->isLast()) {
260+
$r->lastYielded = true;
261+
}
262+
if (null === $chunk->getError() && !$r->lastYielded && $r->response === $response && null !== $r->client) {
259263
throw new \LogicException('A chunk passthru must yield an "isLast()" chunk before ending a stream.');
260264
}
261265

src/Symfony/Component/HttpClient/Tests/AsyncDecoratorTraitTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,27 @@ public function testRetryTimeout()
230230

231231
$this->assertSame(200, $response->getStatusCode());
232232
}
233+
234+
public function testRecurciveStream()
235+
{
236+
$client = new class(parent::getHttpClient(__FUNCTION__)) implements HttpClientInterface {
237+
use AsyncDecoratorTrait;
238+
239+
public function request(string $method, string $url, array $options = []): ResponseInterface
240+
{
241+
return new AsyncResponse($this->client, $method, $url, $options);
242+
}
243+
};
244+
245+
$response = $client->request('GET', 'http://localhost:8057/json');
246+
$content = '';
247+
foreach ($client->stream($response) as $chunk) {
248+
$content .= $chunk->getContent();
249+
foreach ($client->stream($response) as $chunk) {
250+
$content .= $chunk->getContent();
251+
}
252+
}
253+
254+
$this->assertSame('{"documents":[{"id":"\/json\/1"},{"id":"\/json\/2"},{"id":"\/json\/3"}]}', $content);
255+
}
233256
}

0 commit comments

Comments
 (0)