Skip to content

Commit

Permalink
move adding detailed JSON error messages to the validate phase
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Jul 23, 2024
1 parent c878a1b commit 35bab84
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ private function addSerializerSection(ArrayNodeDefinition $rootNode, callable $e
->arrayNode('default_context')
->normalizeKeys(false)
->useAttributeAsKey('name')
->beforeNormalization()
->validate()
->ifTrue(fn () => $this->debug && class_exists(JsonParser::class))
->then(fn (array $v) => $v + [JsonDecode::DETAILED_ERROR_MESSAGES => true])
->end()
Expand Down
58 changes: 58 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\DBAL\Connection;
use PHPUnit\Framework\TestCase;
use Seld\JsonLint\JsonParser;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Configuration;
use Symfony\Bundle\FullStack;
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
Expand Down Expand Up @@ -566,6 +567,63 @@ public function testEnabledLockNeedsResources()
]);
}

public function testSerializerJsonDetailedErrorMessagesEnabledWhenDefaultContextIsConfigured()
{
$processor = new Processor();
$config = $processor->processConfiguration(new Configuration(true), [
[
'serializer' => [
'default_context' => [
'foo' => 'bar',
],
],
],
]);

$this->assertSame(['foo' => 'bar', JsonDecode::DETAILED_ERROR_MESSAGES => true], $config['serializer']['default_context'] ?? []);
}

public function testSerializerJsonDetailedErrorMessagesInDefaultContextCanBeDisabled()
{
$processor = new Processor();
$config = $processor->processConfiguration(new Configuration(true), [
[
'serializer' => [
'default_context' => [
'foo' => 'bar',
JsonDecode::DETAILED_ERROR_MESSAGES => false,
],
],
],
]);

$this->assertSame(['foo' => 'bar', JsonDecode::DETAILED_ERROR_MESSAGES => false], $config['serializer']['default_context'] ?? []);
}

public function testSerializerJsonDetailedErrorMessagesInDefaultContextCanBeDisabledWithSeveralConfigsBeingMerged()
{
$processor = new Processor();
$config = $processor->processConfiguration(new Configuration(true), [
[
'serializer' => [
'default_context' => [
'foo' => 'bar',
JsonDecode::DETAILED_ERROR_MESSAGES => false,
],
],
],
[
'serializer' => [
'default_context' => [
'foobar' => 'baz',
],
],
],
]);

$this->assertSame(['foo' => 'bar', JsonDecode::DETAILED_ERROR_MESSAGES => false, 'foobar' => 'baz'], $config['serializer']['default_context'] ?? []);
}

protected static function getBundleDefaultConfig()
{
return [
Expand Down

0 comments on commit 35bab84

Please sign in to comment.