@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator

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

+24
+2
src/__phutil_library_map__.php
··· 3209 3209 'PhabricatorDocumentEngineBlock' => 'applications/files/diff/PhabricatorDocumentEngineBlock.php', 3210 3210 'PhabricatorDocumentEngineBlockDiff' => 'applications/files/diff/PhabricatorDocumentEngineBlockDiff.php', 3211 3211 'PhabricatorDocumentEngineBlocks' => 'applications/files/diff/PhabricatorDocumentEngineBlocks.php', 3212 + 'PhabricatorDocumentEngineParserException' => 'applications/files/document/exception/PhabricatorDocumentEngineParserException.php', 3212 3213 'PhabricatorDocumentRef' => 'applications/files/document/PhabricatorDocumentRef.php', 3213 3214 'PhabricatorDocumentRenderingEngine' => 'applications/files/document/render/PhabricatorDocumentRenderingEngine.php', 3214 3215 'PhabricatorDoorkeeperApplication' => 'applications/doorkeeper/application/PhabricatorDoorkeeperApplication.php', ··· 9707 9708 'PhabricatorDocumentEngineBlock' => 'Phobject', 9708 9709 'PhabricatorDocumentEngineBlockDiff' => 'Phobject', 9709 9710 'PhabricatorDocumentEngineBlocks' => 'Phobject', 9711 + 'PhabricatorDocumentEngineParserException' => 'Exception', 9710 9712 'PhabricatorDocumentRef' => 'Phobject', 9711 9713 'PhabricatorDocumentRenderingEngine' => 'Phobject', 9712 9714 'PhabricatorDoorkeeperApplication' => 'PhabricatorApplication',
+18
src/applications/files/document/PhabricatorJSONDocumentEngine.php
··· 31 31 try { 32 32 $data = phutil_json_decode($raw_data); 33 33 34 + // See T13635. "phutil_json_decode()" always turns JSON into a PHP array, 35 + // and we lose the distinction between "{}" and "[]". This distinction is 36 + // important when rendering a document. 37 + $data = json_decode($raw_data, false); 38 + if (!$data) { 39 + throw new PhabricatorDocumentEngineParserException( 40 + pht( 41 + 'Failed to "json_decode(...)" JSON document after successfully '. 42 + 'decoding it with "phutil_json_decode(...).')); 43 + } 44 + 34 45 if (preg_match('/^\s*\[/', $raw_data)) { 35 46 $content = id(new PhutilJSON())->encodeAsList($data); 36 47 } else { ··· 45 56 $message = $this->newMessage( 46 57 pht( 47 58 'This document is not valid JSON: %s', 59 + $ex->getMessage())); 60 + 61 + $content = $raw_data; 62 + } catch (PhabricatorDocumentEngineParserException $ex) { 63 + $message = $this->newMessage( 64 + pht( 65 + 'Unable to parse this document as JSON: %s', 48 66 $ex->getMessage())); 49 67 50 68 $content = $raw_data;
+4
src/applications/files/document/exception/PhabricatorDocumentEngineParserException.php
··· 1 + <?php 2 + 3 + final class PhabricatorDocumentEngineParserException 4 + extends Exception {}