From 5f7043e8a8e9009d855f7560e22c3360b2d322f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20K=C3=BChn?= Date: Fri, 16 Oct 2015 15:52:38 +0200 Subject: [PATCH 1/2] use JSON_PARTIAL_OUTPUT_ON_ERROR for json_decode() ApiProblem was not able to render exceptions when the option ApiProblem::detailIncludesStackTrace was true and the exception contained PHP streams in the arguments of the traces ("args" key in Exception::getTraces()). For example: GuzzleHttp works internally with streams which meant that some exceptions raised from GuzzleHttp didn't result in a ApiProblem response but in an empty 500 response because json_encode() failed. --- src/ApiProblemResponse.php | 4 ++-- test/ApiProblemResponseTest.php | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/ApiProblemResponse.php b/src/ApiProblemResponse.php index d6a04c9..fc68eb2 100644 --- a/src/ApiProblemResponse.php +++ b/src/ApiProblemResponse.php @@ -23,7 +23,7 @@ class ApiProblemResponse extends Response * * @var int */ - protected $jsonFlags = 0; + protected $jsonFlags = JSON_PARTIAL_OUTPUT_ON_ERROR; /** * @param ApiProblem $apiProblem @@ -35,7 +35,7 @@ public function __construct(ApiProblem $apiProblem) $this->setReasonPhrase($apiProblem->title); if (defined('JSON_UNESCAPED_SLASHES')) { - $this->jsonFlags = constant('JSON_UNESCAPED_SLASHES'); + $this->jsonFlags |= constant('JSON_UNESCAPED_SLASHES'); } } diff --git a/test/ApiProblemResponseTest.php b/test/ApiProblemResponseTest.php index b587fb3..bdc1293 100644 --- a/test/ApiProblemResponseTest.php +++ b/test/ApiProblemResponseTest.php @@ -32,9 +32,21 @@ public function testApiProblemResponseSetsStatusCodeAndReasonPhrase() public function testApiProblemResponseBodyIsSerializedApiProblem() { - $apiProblem = new ApiProblem(400, 'Random error'); + $additional = [ + 'foo' => fopen('php://memory', 'r') + ]; + + $expected = [ + 'foo' => null, + 'type' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html', + 'title' => 'Bad Request', + 'status' => 400, + 'detail' => 'Random error', + ]; + + $apiProblem = new ApiProblem(400, 'Random error', null, null, $additional); $response = new ApiProblemResponse($apiProblem); - $this->assertEquals($apiProblem->toArray(), json_decode($response->getContent(), true)); + $this->assertEquals($expected, json_decode($response->getContent(), true)); } /** From 8768adc29b3a2e2587194946fafec009c82eaf58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20K=C3=BChn?= Date: Fri, 16 Oct 2015 16:28:05 +0200 Subject: [PATCH 2/2] removed check for availability of json constants The check for the availability of JSON_UNESCAPED_SLASHES is redundant because we already require php >= 5.5 in composer.json and the constant was introduced in PHP 5.4. Also an check for the availability of ext-json was added to ensure functions like json_encode() and the json constants are available. --- composer.json | 1 + src/ApiProblemResponse.php | 6 ++---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 32669cb..430748b 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ }, "require": { "php": ">=5.5", + "ext-json": "*", "zendframework/zend-eventmanager": "~2.3", "zendframework/zend-http": "~2.3", "zendframework/zend-json": "~2.3", diff --git a/src/ApiProblemResponse.php b/src/ApiProblemResponse.php index fc68eb2..64a2d68 100644 --- a/src/ApiProblemResponse.php +++ b/src/ApiProblemResponse.php @@ -23,7 +23,7 @@ class ApiProblemResponse extends Response * * @var int */ - protected $jsonFlags = JSON_PARTIAL_OUTPUT_ON_ERROR; + protected $jsonFlags; /** * @param ApiProblem $apiProblem @@ -34,9 +34,7 @@ public function __construct(ApiProblem $apiProblem) $this->setCustomStatusCode($apiProblem->status); $this->setReasonPhrase($apiProblem->title); - if (defined('JSON_UNESCAPED_SLASHES')) { - $this->jsonFlags |= constant('JSON_UNESCAPED_SLASHES'); - } + $this->jsonFlags = JSON_UNESCAPED_SLASHES | JSON_PARTIAL_OUTPUT_ON_ERROR; } /**