Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] hide pretty response behind the flag #507

Merged
merged 1 commit into from
Nov 4, 2022
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
12 changes: 10 additions & 2 deletions Classes/Json/JsonEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
use JsonException;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Configuration\Features;
use TYPO3\CMS\Core\Utility\GeneralUtility;

use function json_encode;

Expand All @@ -24,13 +25,20 @@ class JsonEncoder implements JsonEncoderInterface, LoggerAwareInterface
{
use LoggerAwareTrait;

private Features $features;

public function __construct()
{
$this->features = GeneralUtility::makeInstance(Features::class);
}

/**
* @inheritDoc
*/
public function encode($data, int $options = 0): string
{
try {
if (Environment::getContext()->isDevelopment() && !($options & JSON_PRETTY_PRINT)) {
if ($this->features->isFeatureEnabled('headless.prettyPrint') && !($options & JSON_PRETTY_PRINT)) {
$options |= JSON_PRETTY_PRINT;
}

Expand Down
13 changes: 13 additions & 0 deletions Tests/Unit/Json/JsonEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

use function json_encode;

class JsonEncoderTest extends UnitTestCase
{
protected function setUp(): void
Expand All @@ -35,10 +37,21 @@ public function testEncoding($testValue, $expectedValue): void
self::assertSame($expectedValue, $encoder->encode($testValue));
}

public function testPrettyEncoding(): void
{
$GLOBALS['TYPO3_CONF_VARS']['SYS']['features']['headless.prettyPrint'] = true;
$encoder = GeneralUtility::makeInstance(JsonEncoder::class);

$encodeValue = ['nested' => ['test' => 1]];

self::assertSame(json_encode($encodeValue, JSON_PRETTY_PRINT), $encoder->encode($encodeValue));
}

public function jsonProvider(): array
{
return [
[[], '[]'],
[['test'=>1], '{"test":1}'],
[new \stdClass(), '{}'],
["\xB1\x31", '[]'], // exception caught, return empty array instead
];
Expand Down