diff --git a/src/Response/Elasticsearch.php b/src/Response/Elasticsearch.php index 6fb4caef1..2cfd08d6f 100644 --- a/src/Response/Elasticsearch.php +++ b/src/Response/Elasticsearch.php @@ -66,6 +66,10 @@ public function setResponse(ResponseInterface $response, bool $throwException = $this->productCheck($response); // Check for Serverless response $this->serverless = $this->isServerlessResponse($response); + + unset($this->asArray, $this->asObject); + $this->asString = ''; + $this->response = $response; $status = $response->getStatusCode(); if ($throwException && $status > 399 && $status < 500) { diff --git a/tests/Response/ElasticsearchTest.php b/tests/Response/ElasticsearchTest.php index dd48f4acb..80ffc4c9e 100644 --- a/tests/Response/ElasticsearchTest.php +++ b/tests/Response/ElasticsearchTest.php @@ -317,4 +317,15 @@ public function testIsServerlessFalseIfNotServerlessResponse() $this->elasticsearch->setResponse($this->response200); $this->assertFalse($this->elasticsearch->isServerless()); } + + public function testCacheIsClearedOnSetResponse(): void + { + $firstBody = $this->psr17Factory->createStream(json_encode(['foo' => 'bar'])); + $this->elasticsearch->setResponse($this->response200->withBody($firstBody)); + $this->assertSame('bar', $this->elasticsearch->asArray()['foo']); + + $secondBody = $this->psr17Factory->createStream(json_encode(['foo' => 'baz'])); + $this->elasticsearch->setResponse($this->response200->withBody($secondBody)); + $this->assertSame('baz', $this->elasticsearch->asArray()['foo']); + } } \ No newline at end of file