Skip to content

Navigation Menu

Sign in
Appearance settings

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 c3a3c23

Browse filesBrowse files
committed
Merge branch '7.2' into 7.3
* 7.2: [Serializer] fix default context in Serializer
2 parents 0deaa1e + d3d1a78 commit c3a3c23
Copy full SHA for c3a3c23

File tree

Expand file treeCollapse file tree

5 files changed

+38
-6
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+38
-6
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-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/DependencyInjection/SerializerPass.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function process(ContainerBuilder $container): void
5858
$defaultContext = $container->getParameter('serializer.default_context');
5959
$this->bindDefaultContext($container, array_merge($normalizers, $encoders), $defaultContext);
6060
$container->getParameterBag()->remove('serializer.default_context');
61+
$container->getDefinition('serializer')->setArgument('$defaultContext', $defaultContext);
6162
}
6263

6364
$this->configureSerializer($container, 'serializer', $normalizers, $encoders, 'default');

‎src/Symfony/Component/Serializer/Serializer.php

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/DependencyInjection/SerializerPassTest.php
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,20 @@ public function testServicesAreOrderedAccordingToPriority()
8383

8484
public function testBindSerializerDefaultContext()
8585
{
86+
$context = ['enable_max_depth' => true];
87+
8688
$container = new ContainerBuilder();
8789
$container->setParameter('kernel.debug', false);
88-
$container->register('serializer')->setArguments([null, null]);
90+
$container->register('serializer')->setArguments([null, null, []]);
8991
$container->setParameter('serializer.default_context', ['enable_max_depth' => true]);
9092
$definition = $container->register('n1')->addTag('serializer.normalizer')->addTag('serializer.encoder');
9193

9294
$serializerPass = new SerializerPass();
9395
$serializerPass->process($container);
9496

9597
$bindings = $definition->getBindings();
96-
$this->assertEquals($bindings['array $defaultContext'], new BoundArgument(['enable_max_depth' => true], false));
98+
$this->assertEquals($bindings['array $defaultContext'], new BoundArgument($context, false));
99+
$this->assertEquals($context, $container->getDefinition('serializer')->getArgument('$defaultContext'));
97100
}
98101

99102
public function testNormalizersAndEncodersAreDecoratedAndOrderedWhenCollectingData()

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

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

16791705
class Model

0 commit comments

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