Skip to content

Commit a01d3c5

Browse files
committed
Merge branch '6.4' into 7.1
* 6.4: [Serializer] fix default context in Serializer
2 parents 0920bb5 + 12e473a commit a01d3c5

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
@@ -59,7 +59,7 @@
5959

6060
$container->services()
6161
->set('serializer', Serializer::class)
62-
->args([[], []])
62+
->args([[], [], []])
6363

6464
->alias(SerializerInterface::class, 'serializer')
6565
->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
@@ -53,6 +53,7 @@ public function process(ContainerBuilder $container): void
5353
}
5454

5555
$container->getParameterBag()->remove('serializer.default_context');
56+
$container->getDefinition('serializer')->setArgument('$defaultContext', $defaultContext);
5657
}
5758

5859
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
@@ -75,10 +75,12 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
7575
/**
7676
* @param array<NormalizerInterface|DenormalizerInterface> $normalizers
7777
* @param array<EncoderInterface|DecoderInterface> $encoders
78+
* @param array<string, mixed> $defaultContext
7879
*/
7980
public function __construct(
8081
private array $normalizers = [],
8182
array $encoders = [],
83+
private array $defaultContext = [],
8284
) {
8385
foreach ($normalizers as $normalizer) {
8486
if ($normalizer instanceof SerializerAwareInterface) {
@@ -154,12 +156,12 @@ public function normalize(mixed $data, ?string $format = null, array $context =
154156
return $data;
155157
}
156158

157-
if (\is_array($data) && !$data && ($context[self::EMPTY_ARRAY_AS_OBJECT] ?? false)) {
159+
if (\is_array($data) && !$data && ($context[self::EMPTY_ARRAY_AS_OBJECT] ?? $this->defaultContext[self::EMPTY_ARRAY_AS_OBJECT] ?? false)) {
158160
return new \ArrayObject();
159161
}
160162

161163
if (is_iterable($data)) {
162-
if ($data instanceof \Countable && ($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && !\count($data)) {
164+
if ($data instanceof \Countable && ($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? $this->defaultContext[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && !\count($data)) {
163165
return new \ArrayObject();
164166
}
165167

@@ -211,7 +213,7 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
211213
throw new NotNormalizableValueException(sprintf('Could not denormalize object of type "%s", no supporting normalizer found.', $type));
212214
}
213215

214-
if (isset($context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS])) {
216+
if (isset($context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS]) || isset($this->defaultContext[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS])) {
215217
unset($context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS]);
216218
$context['not_normalizable_value_exceptions'] = [];
217219
$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
@@ -81,17 +81,20 @@ public function testServicesAreOrderedAccordingToPriority()
8181

8282
public function testBindSerializerDefaultContext()
8383
{
84+
$context = ['enable_max_depth' => true];
85+
8486
$container = new ContainerBuilder();
8587
$container->setParameter('kernel.debug', false);
86-
$container->register('serializer')->setArguments([null, null]);
88+
$container->register('serializer')->setArguments([null, null, []]);
8789
$container->setParameter('serializer.default_context', ['enable_max_depth' => true]);
8890
$definition = $container->register('n1')->addTag('serializer.normalizer')->addTag('serializer.encoder');
8991

9092
$serializerPass = new SerializerPass();
9193
$serializerPass->process($container);
9294

9395
$bindings = $definition->getBindings();
94-
$this->assertEquals($bindings['array $defaultContext'], new BoundArgument(['enable_max_depth' => true], false));
96+
$this->assertEquals($bindings['array $defaultContext'], new BoundArgument($context, false));
97+
$this->assertEquals($context, $container->getDefinition('serializer')->getArgument('$defaultContext'));
9598
}
9699

97100
public function testNormalizersAndEncodersAreDecoredAndOrderedWhenCollectingData()

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,32 @@ public function testPartialDenormalizationWithInvalidVariadicParameter()
16751675
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
16761676
]);
16771677
}
1678+
1679+
public function testEmptyArrayAsObjectDefaultContext()
1680+
{
1681+
$serializer = new Serializer(
1682+
defaultContext: [Serializer::EMPTY_ARRAY_AS_OBJECT => true],
1683+
);
1684+
$this->assertEquals(new \ArrayObject(), $serializer->normalize([]));
1685+
}
1686+
1687+
public function testPreserveEmptyObjectsAsDefaultContext()
1688+
{
1689+
$serializer = new Serializer(
1690+
defaultContext: [AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true],
1691+
);
1692+
$this->assertEquals(new \ArrayObject(), $serializer->normalize(new \ArrayIterator()));
1693+
}
1694+
1695+
public function testCollectDenormalizationErrorsDefaultContext()
1696+
{
1697+
$data = ['variadic' => ['a random string']];
1698+
$serializer = new Serializer([new UidNormalizer(), new ObjectNormalizer()], [], [DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true]);
1699+
1700+
$this->expectException(PartialDenormalizationException::class);
1701+
1702+
$serializer->denormalize($data, DummyWithVariadicParameter::class);
1703+
}
16781704
}
16791705

16801706
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