symfony / symfony

The Symfony PHP framework
https://symfony.com
MIT License
29.78k stars 9.47k forks source link

[Serializer] Setting COLLECT_DENORMALIZATION_ERRORS in default_context of framework config has no effect #58628

Open freddytqg opened 2 weeks ago

freddytqg commented 2 weeks ago

Symfony version(s) affected

7.1.*

Description

Adding COLLECT_DENORMALIZATION_ERRORS to the framework.default_context has no effect if you want to collect denormalization errors for all ->denormalize calls. It only works, if it is added to each ->denormalize call as an extra argument.

How to reproduce

Framework Config

# config/packages/framework.yaml
framework:
    # ...
    serializer:
        default_context:
            !php/const Symfony\Component\Serializer\Normalizer\DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS: true

Object

final readonly class Foo
{
    public function __construct(
        public string $bar,
    ) {}
}

Denormalize call:

public function __construct(
        private DenormalizerInterface $denormalizer,
    ) {
    }

    public function __invoke(): void
    {
        $this->denormalizer->denormalize(
            data: [],
            type: Foo::class,
        );
    }

Expected: \Symfony\Component\Serializer\Exception\PartialDenormalizationException is thrown Actual: \Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException is thrown

Possible Solution

If this is intended behaviour, I would suggest to add this to the documentation here, that it cannot be set as a default.

If this is not an intended behaviour, maybe there is a way to get the context of the used $normalizer at this line in the Serializer Class and use it as another check if it is not set in $context.

Additional Context

No response

mtarld commented 1 week ago

Hey @freddytqg, thanks for the issue!

Indeed, this is not an intended behavior, would you like to create a bug fix PR targeting 5.4? :slightly_smiling_face: