Skip to content

Commit 12e473a

Browse files
committed
bug #58889 [Serializer] Handle default context in Serializer (Valmonzo)
This PR was merged into the 6.4 branch. Discussion ---------- [Serializer] Handle default context in Serializer | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #58628 | License | MIT This fixes the default context not taken into account by the Serializer class. Commits ------- 4400674 [Serializer] fix default context in Serializer
2 parents 7e396bb + 4400674 commit 12e473a

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
$container->services()
6262
->set('serializer', Serializer::class)
63-
->args([[], []])
63+
->args([[], [], []])
6464

6565
->alias(SerializerInterface::class, 'serializer')
6666
->alias(NormalizerInterface::class, 'serializer')

src/Symfony/Component/Serializer/DependencyInjection/SerializerPass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function process(ContainerBuilder $container)
5656
}
5757

5858
$container->getParameterBag()->remove('serializer.default_context');
59+
$container->getDefinition('serializer')->setArgument('$defaultContext', $defaultContext);
5960
}
6061

6162
if ($container->getParameter('kernel.debug') && $container->hasDefinition('serializer.data_collector')) {

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,12 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface
8484
/**
8585
* @param array<NormalizerInterface|DenormalizerInterface> $normalizers
8686
* @param array<EncoderInterface|DecoderInterface> $encoders
87+
* @param array<string, mixed> $defaultContext
8788
*/
8889
public function __construct(
8990
private array $normalizers = [],
9091
array $encoders = [],
92+
private array $defaultContext = [],
9193
) {
9294
foreach ($normalizers as $normalizer) {
9395
if ($normalizer instanceof SerializerAwareInterface) {
@@ -163,12 +165,12 @@ public function normalize(mixed $data, ?string $format = null, array $context =
163165
return $data;
164166
}
165167

166-
if (\is_array($data) && !$data && ($context[self::EMPTY_ARRAY_AS_OBJECT] ?? false)) {
168+
if (\is_array($data) && !$data && ($context[self::EMPTY_ARRAY_AS_OBJECT] ?? $this->defaultContext[self::EMPTY_ARRAY_AS_OBJECT] ?? false)) {
167169
return new \ArrayObject();
168170
}
169171

170172
if (is_iterable($data)) {
171-
if ($data instanceof \Countable && ($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && !\count($data)) {
173+
if ($data instanceof \Countable && ($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? $this->defaultContext[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && !\count($data)) {
172174
return new \ArrayObject();
173175
}
174176

@@ -220,7 +222,7 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
220222
throw new NotNormalizableValueException(sprintf('Could not denormalize object of type "%s", no supporting normalizer found.', $type));
221223
}
222224

223-
if (isset($context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS])) {
225+
if (isset($context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS]) || isset($this->defaultContext[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS])) {
224226
unset($context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS]);
225227
$context['not_normalizable_value_exceptions'] = [];
226228
$errors = &$context['not_normalizable_value_exceptions'];

src/Symfony/Component/Serializer/Tests/DependencyInjection/SerializerPassTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,20 @@ public function testServicesAreOrderedAccordingToPriority()
7777

7878
public function testBindSerializerDefaultContext()
7979
{
80+
$context = ['enable_max_depth' => true];
81+
8082
$container = new ContainerBuilder();
8183
$container->setParameter('kernel.debug', false);
82-
$container->register('serializer')->setArguments([null, null]);
84+
$container->register('serializer')->setArguments([null, null, []]);
8385
$container->setParameter('serializer.default_context', ['enable_max_depth' => true]);
8486
$definition = $container->register('n1')->addTag('serializer.normalizer')->addTag('serializer.encoder');
8587

8688
$serializerPass = new SerializerPass();
8789
$serializerPass->process($container);
8890

8991
$bindings = $definition->getBindings();
90-
$this->assertEquals($bindings['array $defaultContext'], new BoundArgument(['enable_max_depth' => true], false));
92+
$this->assertEquals($bindings['array $defaultContext'], new BoundArgument($context, false));
93+
$this->assertEquals($context, $container->getDefinition('serializer')->getArgument('$defaultContext'));
9194
}
9295

9396
public function testNormalizersAndEncodersAreDecoredAndOrderedWhenCollectingData()

src/Symfony/Component/Serializer/Tests/SerializerTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,32 @@ public function testPartialDenormalizationWithInvalidVariadicParameter()
16521652
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
16531653
]);
16541654
}
1655+
1656+
public function testEmptyArrayAsObjectDefaultContext()
1657+
{
1658+
$serializer = new Serializer(
1659+
defaultContext: [Serializer::EMPTY_ARRAY_AS_OBJECT => true],
1660+
);
1661+
$this->assertEquals(new \ArrayObject(), $serializer->normalize([]));
1662+
}
1663+
1664+
public function testPreserveEmptyObjectsAsDefaultContext()
1665+
{
1666+
$serializer = new Serializer(
1667+
defaultContext: [AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true],
1668+
);
1669+
$this->assertEquals(new \ArrayObject(), $serializer->normalize(new \ArrayIterator()));
1670+
}
1671+
1672+
public function testCollectDenormalizationErrorsDefaultContext()
1673+
{
1674+
$data = ['variadic' => ['a random string']];
1675+
$serializer = new Serializer([new UidNormalizer(), new ObjectNormalizer()], [], [DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true]);
1676+
1677+
$this->expectException(PartialDenormalizationException::class);
1678+
1679+
$serializer->denormalize($data, DummyWithVariadicParameter::class);
1680+
}
16551681
}
16561682

16571683
class Model

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy