Skip to content

Commit 41934a4

Browse files
committed
Fixed bug where any response object that could not be type cast as a string would throw an exception. Fixes dingo#277.
Signed-off-by: Jason Lewis <[email protected]>
1 parent c2ae6ae commit 41934a4

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

changes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
- Default throttles for rate limiting have been removed.
66

7+
#### Fixed
8+
9+
- Fixed bug where any response object that could not be type cast as a string would throw an `UnexpectedValueException`.
10+
711
### v0.7.2
812

913
#### Fixed

src/Http/Response.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Dingo\Api\Http;
44

55
use ArrayObject;
6+
use UnexpectedValueException;
67
use Dingo\Api\Transformer\TransformerFactory;
78
use Illuminate\Http\Response as IlluminateResponse;
89
use Illuminate\Support\Contracts\ArrayableInterface;
@@ -72,6 +73,24 @@ public function morph($format = 'json')
7273
return $this;
7374
}
7475

76+
/**
77+
* {@inheritDoc}
78+
*/
79+
public function setContent($content)
80+
{
81+
// Attempt to set the content string, if we encounter an unexpected value
82+
// then we most likely have an object that cannot be type cast. In that
83+
// case we'll simply leave the content as null and set the original
84+
// content value the continue.
85+
try {
86+
return parent::setContent($content);
87+
} catch (UnexpectedValueException $exception) {
88+
$this->original = $content;
89+
90+
return $this;
91+
}
92+
}
93+
7594
/**
7695
* Get the formatter based on the requested format type.
7796
*

tests/Http/ResponseTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Dingo\Api\Tests\Http;
44

5+
use stdClass;
56
use Dingo\Api\Http\Response;
67
use PHPUnit_Framework_TestCase;
78

@@ -15,4 +16,16 @@ public function testGettingInvalidFormatterThrowsException()
1516
{
1617
Response::getFormatter('json');
1718
}
19+
20+
21+
public function testNonCastableObjectsSetAsOriginalContent()
22+
{
23+
$object = new stdClass;
24+
$object->id = 'test';
25+
26+
$response = new Response($object);
27+
28+
$this->assertNull($response->getContent());
29+
$this->assertSame($object, $response->getOriginalContent());
30+
}
1831
}

0 commit comments

Comments
 (0)