Skip to content

Commit 8d245bc

Browse files
authored
Added the array support for text/plain (#1220)
1 parent a8fb0e0 commit 8d245bc

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/Response/Elasticsearch.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Elastic\Transport\Serializer\CsvSerializer;
2525
use Elastic\Transport\Serializer\JsonSerializer;
2626
use Elastic\Transport\Serializer\NDJsonSerializer;
27+
use Elastic\Transport\Serializer\TextSerializer;
2728
use Elastic\Transport\Serializer\XmlSerializer;
2829
use Psr\Http\Message\ResponseInterface;
2930

@@ -113,6 +114,10 @@ public function asArray(): array
113114
$this->asArray = CsvSerializer::unserialize($this->asString());
114115
return $this->asArray;
115116
}
117+
if (strpos($contentType, 'text/plain') !== false) {
118+
$this->asArray = [$this->asString()];
119+
return $this->asArray;
120+
}
116121
throw new UnknownContentTypeException(sprintf(
117122
"Cannot deserialize the reponse as array with Content-Type: %s",
118123
$contentType

tests/Response/ElasticsearchTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,20 @@ public function testAccessAsObject()
200200

201201
$this->assertEquals($array['foo'], $this->elasticsearch->foo);
202202
}
203+
204+
/**
205+
* @see https://github.com/elastic/elasticsearch-php/issues/1218
206+
*/
207+
public function testAccessAsArrayWithTextPlainResponse()
208+
{
209+
$msg = "This is a text/plain response";
210+
$body = $this->psr17Factory->createStream($msg);
211+
$this->elasticsearch->setResponse(
212+
$this->response200
213+
->withBody($body)
214+
->withHeader('Content-Type', 'text/plain')
215+
);
216+
217+
$this->assertEquals($msg, $this->elasticsearch[0]);
218+
}
203219
}

0 commit comments

Comments
 (0)