1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-10 00:42:41 +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:
epriestley 2021-03-11 11:48:55 -08:00
parent 0815891e42
commit 4484946cfd
3 changed files with 24 additions and 0 deletions

View file

@ -3209,6 +3209,7 @@ phutil_register_library_map(array(
'PhabricatorDocumentEngineBlock' => 'applications/files/diff/PhabricatorDocumentEngineBlock.php',
'PhabricatorDocumentEngineBlockDiff' => 'applications/files/diff/PhabricatorDocumentEngineBlockDiff.php',
'PhabricatorDocumentEngineBlocks' => 'applications/files/diff/PhabricatorDocumentEngineBlocks.php',
'PhabricatorDocumentEngineParserException' => 'applications/files/document/exception/PhabricatorDocumentEngineParserException.php',
'PhabricatorDocumentRef' => 'applications/files/document/PhabricatorDocumentRef.php',
'PhabricatorDocumentRenderingEngine' => 'applications/files/document/render/PhabricatorDocumentRenderingEngine.php',
'PhabricatorDoorkeeperApplication' => 'applications/doorkeeper/application/PhabricatorDoorkeeperApplication.php',
@ -9707,6 +9708,7 @@ phutil_register_library_map(array(
'PhabricatorDocumentEngineBlock' => 'Phobject',
'PhabricatorDocumentEngineBlockDiff' => 'Phobject',
'PhabricatorDocumentEngineBlocks' => 'Phobject',
'PhabricatorDocumentEngineParserException' => 'Exception',
'PhabricatorDocumentRef' => 'Phobject',
'PhabricatorDocumentRenderingEngine' => 'Phobject',
'PhabricatorDoorkeeperApplication' => 'PhabricatorApplication',

View file

@ -31,6 +31,17 @@ final class PhabricatorJSONDocumentEngine
try {
$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)) {
$content = id(new PhutilJSON())->encodeAsList($data);
} else {
@ -47,6 +58,13 @@ final class PhabricatorJSONDocumentEngine
'This document is not valid JSON: %s',
$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;
}

View file

@ -0,0 +1,4 @@
<?php
final class PhabricatorDocumentEngineParserException
extends Exception {}