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

[Serializer] fix default context in Serializer #59552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Serializer] fix default context in Serializer
  • Loading branch information
Valmonzo committed Jan 18, 2025
commit 4d4668268333aa32cf400cd289fd7672b8a0783f
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function process(ContainerBuilder $container)
$definition->setBindings(['array $defaultContext' => new BoundArgument($defaultContext, false)] + $definition->getBindings());
}

$container->getDefinition('serializer')->addMethodCall('setDefaultContext', [$defaultContext]);
$container->getParameterBag()->remove('serializer.default_context');
}

Expand Down
21 changes: 18 additions & 3 deletions 21 src/Symfony/Component/Serializer/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
*/
private array $normalizerCache = [];

/**
* @var array<string, mixed>
*/
private array $defaultContext = [];

/**
* @param array<NormalizerInterface|DenormalizerInterface> $normalizers
* @param array<EncoderInterface|DecoderInterface> $encoders
Expand Down Expand Up @@ -152,7 +157,7 @@
return $this->denormalize($data, $type, $format, $context);
}

public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null

Check failure on line 160 in src/Symfony/Component/Serializer/Serializer.php

View workflow job for this annotation

GitHub Actions / Psalm

ParamNameMismatch

src/Symfony/Component/Serializer/Serializer.php:160:37: ParamNameMismatch: Argument 1 of Symfony\Component\Serializer\Serializer::normalize has wrong name $data, expecting $object as defined by Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize (see https://psalm.dev/230)

Check failure on line 160 in src/Symfony/Component/Serializer/Serializer.php

View workflow job for this annotation

GitHub Actions / Psalm

ParamNameMismatch

src/Symfony/Component/Serializer/Serializer.php:160:37: ParamNameMismatch: Argument 1 of Symfony\Component\Serializer\Serializer::normalize has wrong name $data, expecting $object as defined by Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize (see https://psalm.dev/230)
{
// If a normalizer supports the given data, use it
if ($normalizer = $this->getNormalizer($data, $format, $context)) {
Expand All @@ -163,12 +168,12 @@
return $data;
}

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

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

Expand Down Expand Up @@ -220,7 +225,7 @@
throw new NotNormalizableValueException(sprintf('Could not denormalize object of type "%s", no supporting normalizer found.', $type));
}

if (isset($context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS])) {
if (isset($context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS]) || isset($this->defaultContext[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS])) {
unset($context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS]);
$context['not_normalizable_value_exceptions'] = [];
$errors = &$context['not_normalizable_value_exceptions'];
Expand Down Expand Up @@ -430,4 +435,14 @@
{
return $this->decoder->supportsDecoding($format, $context);
}

/**
* @internal
*
* @param array<string, mixed> $defaultContext
*/
public function setDefaultContext(array $defaultContext): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be added as a constructor argument to me

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the approach in #58889.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks cleaner for me too. Should I add more details to say that it's a fix up to 7.3?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we merge this as a bugfix for branches up to 7.3?

{
$this->defaultContext = $defaultContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,19 @@ public function testNormalizersAndEncodersAreDecoredAndOrderedWhenCollectingData
$this->assertEquals(new Reference('e'), $traceableEncoderDefinition->getArgument(0));
$this->assertEquals(new Reference('serializer.data_collector'), $traceableEncoderDefinition->getArgument(1));
}

public function testSetDefaultContext()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('serializer.default_context', ['enable_max_depth' => true]);
$container->register('serializer')->setArguments([null, null]);
$container->register('n')->addTag('serializer.normalizer');
$container->register('e')->addTag('serializer.encoder');

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

$this->assertSame([['setDefaultContext', [['enable_max_depth' => true]]]], $container->getDefinition('serializer')->getMethodCalls());
}
}
23 changes: 23 additions & 0 deletions 23 src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,29 @@ public function testPartialDenormalizationWithInvalidVariadicParameter()
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
]);
}

public function testEmptyArrayAsObjectDefaultContext()
{
$serializer = new Serializer();
$serializer->setDefaultContext([Serializer::EMPTY_ARRAY_AS_OBJECT => true]);
$this->assertEquals(new \ArrayObject(), $serializer->normalize([]));
}

public function testPreserveEmptyObjectsAsDefaultContext()
{
$serializer = new Serializer();
$serializer->setDefaultContext([AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true]);
$this->assertEquals(new \ArrayObject(), $serializer->normalize(new \ArrayIterator()));
}

public function testCollectDenormalizationErrorsDefaultContext()
{
$data = ['variadic' => ['a random string']];
$serializer = new Serializer([new UidNormalizer(), new ObjectNormalizer()], []);
$serializer->setDefaultContext([DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true]);
$this->expectException(PartialDenormalizationException::class);
$serializer->denormalize($data, DummyWithVariadicParameter::class);
}
}

class Model
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.