From 35bab8428616546cec9166c685be151677b80894 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Tue, 23 Jul 2024 08:10:24 +0200 Subject: [PATCH] move adding detailed JSON error messages to the validate phase --- DependencyInjection/Configuration.php | 2 +- .../DependencyInjection/ConfigurationTest.php | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index ad3521608..4f23d4396 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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() diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index ca34ce109..1e4ee15a6 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -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; @@ -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 [