Skip to content

Commit dc93ca8

Browse files
committed
Merge pull request #38 from EvolverGroup/feature/json-encode-fails-for-objects
json_encode fails for streams Conflicts: composer.json
2 parents 00f7f01 + 8768adc commit dc93ca8

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
},
2929
"require": {
3030
"php": "^5.6 || ^7.0",
31+
"ext-json": "*",
3132
"zendframework/zend-eventmanager": "^2.6.3 || ^3.0.1",
3233
"zendframework/zend-http": "^2.5.4",
3334
"zendframework/zend-json": "^2.6.1 || ^3.0",

src/ApiProblemResponse.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ApiProblemResponse extends Response
2323
*
2424
* @var int
2525
*/
26-
protected $jsonFlags = 0;
26+
protected $jsonFlags;
2727

2828
/**
2929
* @param ApiProblem $apiProblem
@@ -34,9 +34,7 @@ public function __construct(ApiProblem $apiProblem)
3434
$this->setCustomStatusCode($apiProblem->status);
3535
$this->setReasonPhrase($apiProblem->title);
3636

37-
if (defined('JSON_UNESCAPED_SLASHES')) {
38-
$this->jsonFlags = constant('JSON_UNESCAPED_SLASHES');
39-
}
37+
$this->jsonFlags = JSON_UNESCAPED_SLASHES | JSON_PARTIAL_OUTPUT_ON_ERROR;
4038
}
4139

4240
/**

test/ApiProblemResponseTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,21 @@ public function testApiProblemResponseSetsStatusCodeAndReasonPhrase()
3232

3333
public function testApiProblemResponseBodyIsSerializedApiProblem()
3434
{
35-
$apiProblem = new ApiProblem(400, 'Random error');
35+
$additional = [
36+
'foo' => fopen('php://memory', 'r')
37+
];
38+
39+
$expected = [
40+
'foo' => null,
41+
'type' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html',
42+
'title' => 'Bad Request',
43+
'status' => 400,
44+
'detail' => 'Random error',
45+
];
46+
47+
$apiProblem = new ApiProblem(400, 'Random error', null, null, $additional);
3648
$response = new ApiProblemResponse($apiProblem);
37-
$this->assertEquals($apiProblem->toArray(), json_decode($response->getContent(), true));
49+
$this->assertEquals($expected, json_decode($response->getContent(), true));
3850
}
3951

4052
/**

0 commit comments

Comments
 (0)