Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] PartialDenormalizationException not thrown or not collecting all Exceptions if other Denormalizer are called during denormalizing (e.g. DateTimeNormalizer) #59721

Open
@freddytqg

Description

@freddytqg

Symfony version(s) affected

7.2.3

Description

Denormalizing an erroneous object that has one ore more non-scalar types in it's class definition can cause losing any or all Exception caught in a PartialDenormalizationException or the entire exception itself.

How to reproduce

COLLECT_DENORMALIZATION_ERRORS is activated.

# 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,
        public DateTimeInterface $createdAt,
    ) {}
}

Denormalize call:

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

    public function __invoke(): void
    {
        $this->denormalizer->denormalize(
            data: ['createdAt' => '2024-06-13T01:32:28+00:00'],
            type: Foo::class,
        );
    }

Expected: \Symfony\Component\Serializer\Exception\PartialDenormalizationException is thrown
Actual: Returns an empty object of type Foo.

Calling the denormalizer like this:

        $this->denormalizer->denormalize(
            data: ['createdAt' => []],
            type: Foo::class,
        );

results in a PartialDenormalizationException but with only 1 error instead of 2.

This can also be the case if you use an enum in your DTO.
Removing $createdAt from the constructor and denormalize [] as data would result in a PartialDenormalizationException with the error NotNormalizeableValueException, which is correct.

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions