Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

json_encode fails for streams #38

Merged
merged 2 commits into from
Jul 6, 2016
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 2 additions & 4 deletions src/ApiProblemResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ApiProblemResponse extends Response
*
* @var int
*/
protected $jsonFlags = 0;
protected $jsonFlags;

/**
* @param ApiProblem $apiProblem
Expand All @@ -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;
}

/**
Expand Down
16 changes: 14 additions & 2 deletions test/ApiProblemResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down