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

Commit 1291649

Browse files
committed
Merge branch 'weierophinney-hotfix/json-default-flags' into develop
2 parents 986a6d7 + 7af31d3 commit 1291649

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ All notable changes to this project will be documented in this file, in reverse
4848
- [#96](https://github.com/zendframework/zend-diactoros/pull/96) updates
4949
`withPort()` to allow `null` port values (indicating usage of default for
5050
the given scheme).
51+
- [#98](https://github.com/zendframework/zend-diactoros/pull/98) updates
52+
the default JSON flags used by `JsonResponse` to include
53+
`JSON_UNESCAPED_SLASHES`, which still conforms with RFC 4627, and is a more
54+
sane default.
5155

5256
## 1.1.3 - 2015-08-10
5357

src/Response/JsonResponse.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ class JsonResponse extends Response
2424
{
2525
use InjectContentTypeTrait;
2626

27+
/**
28+
* Default flags for json_encode; value of:
29+
*
30+
* <code>
31+
* JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES
32+
* </code>
33+
*
34+
* @const int
35+
*/
36+
const DEFAULT_JSON_FLAGS = 79;
37+
2738
/**
2839
* Create a JSON response with the given data.
2940
*
@@ -34,15 +45,20 @@ class JsonResponse extends Response
3445
* - JSON_HEX_APOS
3546
* - JSON_HEX_AMP
3647
* - JSON_HEX_QUOT
48+
* - JSON_UNESCAPED_SLASHES
3749
*
3850
* @param mixed $data Data to convert to JSON.
3951
* @param int $status Integer status code for the response; 200 by default.
4052
* @param array $headers Array of headers to use at initialization.
4153
* @param int $encodingOptions JSON encoding options to use.
4254
* @throws InvalidArgumentException if unable to encode the $data to JSON.
4355
*/
44-
public function __construct($data, $status = 200, array $headers = [], $encodingOptions = 15)
45-
{
56+
public function __construct(
57+
$data,
58+
$status = 200,
59+
array $headers = [],
60+
$encodingOptions = self::DEFAULT_JSON_FLAGS
61+
) {
4662
$body = new Stream('php://temp', 'wb+');
4763
$body->write($this->jsonEncode($data, $encodingOptions));
4864

test/Response/JsonResponseTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,32 @@ public function testJsonErrorHandlingOfBadEmbeddedData()
9898
$this->setExpectedException('InvalidArgumentException', 'Unable to encode');
9999
new JsonResponse($data);
100100
}
101+
102+
public function valuesToJsonEncode()
103+
{
104+
return [
105+
'uri' => ['https://example.com/foo?bar=baz&baz=bat', 'uri'],
106+
'html' => ['<p class="test">content</p>', 'html'],
107+
'string' => ["Don't quote!", 'string'],
108+
];
109+
}
110+
111+
/**
112+
* @dataProvider valuesToJsonEncode
113+
*/
114+
public function testUsesSaneDefaultJsonEncodingFlags($value, $key)
115+
{
116+
$defaultFlags = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_SLASHES;
117+
118+
$response = new JsonResponse([$key => $value]);
119+
$stream = $response->getBody();
120+
$contents = (string) $stream;
121+
122+
$expected = json_encode($value, $defaultFlags);
123+
$this->assertContains(
124+
$expected,
125+
$contents,
126+
sprintf('Did not encode %s properly; expected (%s), received (%s)', $key, $expected, $contents)
127+
);
128+
}
101129
}

0 commit comments

Comments
 (0)