8000 [Serializer] Handle default context in Serializer · symfony/symfony@5f339f4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f339f4

Browse files
committed
[Serializer] Handle default context in Serializer
1 parent 383883a commit 5f339f4

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function process(ContainerBuilder $container)
5454
$definition = $container->getDefinition($service);
5555
$definition->setBindings(['array $defaultContext' => new BoundArgument($defaultContext, false)] + $definition->getBindings());
5656
}
57-
57+
$container->getDefinition('serializer')->replaceArgument(2, $defaultContext);
5858
$container->getParameterBag()->remove('serializer.default_context');
5959
}
6060

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 5 additions & 4 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) {
@@ -158,17 +160,16 @@ public function normalize(mixed $data, ?string $format = null, array $context =
158160
if ($normalizer = $this->getNormalizer($data, $format, $context)) {
159161
return $normalizer->normalize($data, $format, $context);
160162
}
161-
162163
if (null === $data || \is_scalar($data)) {
163164
return $data;
164165
}
165166

166-
if (\is_array($data) && !$data && ($context[self::EMPTY_ARRAY_AS_OBJECT] ?? false)) {
167+
if (\is_array($data) && !$data && ($context[self::EMPTY_ARRAY_AS_OBJECT] ?? $this->defaultContext[self::EMPTY_ARRAY_AS_OBJECT] ?? false)) {
167168
return new \ArrayObject();
168169
}
169170

170171
if (is_iterable($data)) {
171-
if ($data instanceof \Countable && ($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && !\count($data)) {
172+
if ($data instanceof \Countable && ($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? $this->defaultContext[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && !\count($data)) {
172173
return new \ArrayObject();
173174
}
174175

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

223-
if (isset($context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS])) {
224+
if (isset($context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS]) || isset($this->defaultContext[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS])) {
224225
unset($context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS]);
225226
$context['not_normalizable_value_exceptions'] = [];
226227
$errors = &$context['not_normalizable_value_exceptions'];

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

Lines changed: 6 additions & 3 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]);
83-
$container->setParameter('serializer.default_context', ['enable_max_depth' => true]);
84+
$container->register('serializer')->setArguments([null, null, null]);
85+
$container->setParameter('serializer.default_context', $context);
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(2));
9194
}
9295

9396
public function testNormalizersAndEncodersAreDecoredAndOrderedWhenCollectingData()

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
4242
use Symfony\Component\Serializer\Normalizer\DateTimeZoneNormalizer;
4343
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
44+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface as DenormalizerInterfaceAlias;
4445
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
4546
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
4647
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
@@ -1652,6 +1653,33 @@ public function testPartialDenormalizationWithInvalidVariadicParameter()
16521653
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
16531654
]);
16541655
}
1656+
1657+
public function testEmptyArrayAsObjectDefaultContext()
1658+
{
1659+
$serializer = new Serializer(
1660+
defaultContext: [Serializer::EMPTY_ARRAY_AS_OBJECT => true],
1661+
);
1662+
1663+
$this->assertEquals(new \ArrayObject(), $serializer->normalize([]));
1664+
}
1665+
1666+
public function testPreserveEmptyObjectsAsDefaultContext()
1667+
{
1668+
$serializer = new Serializer(
1669+
defaultContext: [AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true],
1670+
);
1671+
1672+
$this->assertEquals(new \ArrayObject(), $serializer->normalize(new \ArrayIterator()));
1673+
}
1674+
public function testCollectDenormalizationErrorsDefaultContext()
1675+
{
1676+
$data = ['variadic' => ['a random string']];
1677+
$serializer = new Serializer([new UidNormalizer(), new ObjectNormalizer()], [], [DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true]);
1678+
1679+
$this->expectException(PartialDenormalizationException::class);
1680+
1681+
$serializer->denormalize($data, DummyWithVariadicParameter::class);
1682+
}
16551683
}
16561684

16571685
class Model

0 commit comments

Comments
 (0)
< 2AFA /div>
0