mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-23 05:50:55 +01:00
In JSON DocumentEngine, preserve the distinction between "{}" and "[]"
Summary: Ref T13635. Currently, the JSON DocumentEngine uses "phutil_json_decode()", but this can confuse "{}" and "[]". Be more careful about how the JSON value is decoded, to preserve the distinction. Test Plan: {F8520479} Maniphest Tasks: T13635 Differential Revision: https://secure.phabricator.com/D21605
This commit is contained in:
parent
0815891e42
commit
4484946cfd
3 changed files with 24 additions and 0 deletions
|
@ -3209,6 +3209,7 @@ phutil_register_library_map(array(
|
||||||
'PhabricatorDocumentEngineBlock' => 'applications/files/diff/PhabricatorDocumentEngineBlock.php',
|
'PhabricatorDocumentEngineBlock' => 'applications/files/diff/PhabricatorDocumentEngineBlock.php',
|
||||||
'PhabricatorDocumentEngineBlockDiff' => 'applications/files/diff/PhabricatorDocumentEngineBlockDiff.php',
|
'PhabricatorDocumentEngineBlockDiff' => 'applications/files/diff/PhabricatorDocumentEngineBlockDiff.php',
|
||||||
'PhabricatorDocumentEngineBlocks' => 'applications/files/diff/PhabricatorDocumentEngineBlocks.php',
|
'PhabricatorDocumentEngineBlocks' => 'applications/files/diff/PhabricatorDocumentEngineBlocks.php',
|
||||||
|
'PhabricatorDocumentEngineParserException' => 'applications/files/document/exception/PhabricatorDocumentEngineParserException.php',
|
||||||
'PhabricatorDocumentRef' => 'applications/files/document/PhabricatorDocumentRef.php',
|
'PhabricatorDocumentRef' => 'applications/files/document/PhabricatorDocumentRef.php',
|
||||||
'PhabricatorDocumentRenderingEngine' => 'applications/files/document/render/PhabricatorDocumentRenderingEngine.php',
|
'PhabricatorDocumentRenderingEngine' => 'applications/files/document/render/PhabricatorDocumentRenderingEngine.php',
|
||||||
'PhabricatorDoorkeeperApplication' => 'applications/doorkeeper/application/PhabricatorDoorkeeperApplication.php',
|
'PhabricatorDoorkeeperApplication' => 'applications/doorkeeper/application/PhabricatorDoorkeeperApplication.php',
|
||||||
|
@ -9707,6 +9708,7 @@ phutil_register_library_map(array(
|
||||||
'PhabricatorDocumentEngineBlock' => 'Phobject',
|
'PhabricatorDocumentEngineBlock' => 'Phobject',
|
||||||
'PhabricatorDocumentEngineBlockDiff' => 'Phobject',
|
'PhabricatorDocumentEngineBlockDiff' => 'Phobject',
|
||||||
'PhabricatorDocumentEngineBlocks' => 'Phobject',
|
'PhabricatorDocumentEngineBlocks' => 'Phobject',
|
||||||
|
'PhabricatorDocumentEngineParserException' => 'Exception',
|
||||||
'PhabricatorDocumentRef' => 'Phobject',
|
'PhabricatorDocumentRef' => 'Phobject',
|
||||||
'PhabricatorDocumentRenderingEngine' => 'Phobject',
|
'PhabricatorDocumentRenderingEngine' => 'Phobject',
|
||||||
'PhabricatorDoorkeeperApplication' => 'PhabricatorApplication',
|
'PhabricatorDoorkeeperApplication' => 'PhabricatorApplication',
|
||||||
|
|
|
@ -31,6 +31,17 @@ final class PhabricatorJSONDocumentEngine
|
||||||
try {
|
try {
|
||||||
$data = phutil_json_decode($raw_data);
|
$data = phutil_json_decode($raw_data);
|
||||||
|
|
||||||
|
// See T13635. "phutil_json_decode()" always turns JSON into a PHP array,
|
||||||
|
// and we lose the distinction between "{}" and "[]". This distinction is
|
||||||
|
// important when rendering a document.
|
||||||
|
$data = json_decode($raw_data, false);
|
||||||
|
if (!$data) {
|
||||||
|
throw new PhabricatorDocumentEngineParserException(
|
||||||
|
pht(
|
||||||
|
'Failed to "json_decode(...)" JSON document after successfully '.
|
||||||
|
'decoding it with "phutil_json_decode(...).'));
|
||||||
|
}
|
||||||
|
|
||||||
if (preg_match('/^\s*\[/', $raw_data)) {
|
if (preg_match('/^\s*\[/', $raw_data)) {
|
||||||
$content = id(new PhutilJSON())->encodeAsList($data);
|
$content = id(new PhutilJSON())->encodeAsList($data);
|
||||||
} else {
|
} else {
|
||||||
|
@ -47,6 +58,13 @@ final class PhabricatorJSONDocumentEngine
|
||||||
'This document is not valid JSON: %s',
|
'This document is not valid JSON: %s',
|
||||||
$ex->getMessage()));
|
$ex->getMessage()));
|
||||||
|
|
||||||
|
$content = $raw_data;
|
||||||
|
} catch (PhabricatorDocumentEngineParserException $ex) {
|
||||||
|
$message = $this->newMessage(
|
||||||
|
pht(
|
||||||
|
'Unable to parse this document as JSON: %s',
|
||||||
|
$ex->getMessage()));
|
||||||
|
|
||||||
$content = $raw_data;
|
$content = $raw_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
final class PhabricatorDocumentEngineParserException
|
||||||
|
extends Exception {}
|
Loading…
Reference in a new issue