Skip to content

[Serializer] \TypeError returned rather than a PartialDenormalizationException by PropertyNormalizer embedded into a Serializer #60405

@delamotte-pierrick

Description

@delamotte-pierrick

Symfony version(s) affected

7.2.X

Description

When attempting to denormalize an array of data into a readonly object using the PropertyNormalizer within a Serializer, a \TypeError exception is thrown.
Given that the following flags are set in the context:

DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true,

I would have expected a PartialDenormalizationException instead. Is this the intended behavior?

How to reproduce

I made a small boilerplate to reproduce the bug HERE

Possible Solution

To address the issue, we should wrap the code in a try/catch block that catches the \TypeError and rethrows it as an InvalidTypeException.

In my REPRODUCER, I added two tests to demonstrate the fix: one is a temporary workaround that I'm currently using, and the other represents the solution I believe should be the definitive fix.

Additional Context

I'm currently using

  • PHP: 8.4.4;
  • symfony/normalizer: 7.2;

I use the flags

  • DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true
  • AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true

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