Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 35627e0

Browse filesBrowse files
committed
[Serializer] Handle default context in Serializer
1 parent 383883a commit 35627e0
Copy full SHA for 35627e0

File tree

5 files changed

+42
-9
lines changed
Filter options

5 files changed

+42
-9
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php
+1-1
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/DependencyInjection/SerializerPass.php
+1-1
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Serializer.php
+5-4
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/DependencyInjection/SerializerPassTest.php
+6-3
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/SerializerTest.php
+29
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,34 @@ 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+
1675+
public function testCollectDenormalizationErrorsDefaultContext()
1676+
{
1677+
$data = ['variadic' => ['a random string']];
1678+
$serializer = new Serializer([new UidNormalizer(), new ObjectNormalizer()], [], [DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true]);
1679+
1680+
$this->expectException(PartialDenormalizationException::class);
1681+
1682+
$serializer->denormalize($data, DummyWithVariadicParameter::class);
1683+
}
16551684
}
16561685

16571686
class Model

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.