Skip to content

Added the array support for text/plain #1220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Response/Elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Elastic\Transport\Serializer\CsvSerializer;
use Elastic\Transport\Serializer\JsonSerializer;
use Elastic\Transport\Serializer\NDJsonSerializer;
use Elastic\Transport\Serializer\TextSerializer;
use Elastic\Transport\Serializer\XmlSerializer;
use Psr\Http\Message\ResponseInterface;

Expand Down Expand Up @@ -113,6 +114,10 @@ public function asArray(): array
$this->asArray = CsvSerializer::unserialize($this->asString());
return $this->asArray;
}
if (strpos($contentType, 'text/plain') !== false) {
$this->asArray = [$this->asString()];
return $this->asArray;
}
throw new UnknownContentTypeException(sprintf(
"Cannot deserialize the reponse as array with Content-Type: %s",
$contentType
Expand Down
16 changes: 16 additions & 0 deletions tests/Response/ElasticsearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,20 @@ public function testAccessAsObject()

$this->assertEquals($array['foo'], $this->elasticsearch->foo);
}

/**
* @see https://github.com/elastic/elasticsearch-php/issues/1218
*/
public function testAccessAsArrayWithTextPlainResponse()
{
$msg = "This is a text/plain response";
$body = $this->psr17Factory->createStream($msg);
$this->elasticsearch->setResponse(
$this->response200
->withBody($body)
->withHeader('Content-Type', 'text/plain')
);

$this->assertEquals($msg, $this->elasticsearch[0]);
}
}