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

Skip to content

[Serializer] Different name conversion strategy is used during denormalization, depending on whether you use properties or a constructor #58583

Closed as not planned
@gremo

Description

@gremo

Symfony version(s) affected

7.1

Description

A different name conversion strategy is used during denormalization, depending on whether you use properties or a constructor.

How to reproduce

$normalizer = new ObjectNormalizer(
    nameConverter: new CamelCaseToSnakeCaseNameConverter(),
    propertyTypeExtractor: new ReflectionExtractor(),
);

Given this normalizer, with the following class:

class Course
{
    public int $totalLessons;
}
// both works!
dump($n->denormalize(['totalLessons' => 10], Course::class));
dd($n->denormalize(['total_lessons' => 10], Course::class));

image

Now let's use the constructor:

class Course
{
    public function __construct(
        public readonly int $totalLessons,
    ) {
    }
}
// works
dd($n->denormalize(['total_lessons' => 10], Course::class));

// not working! MissingConstructorArgumentsException:
dump($n->denormalize(['totalLessons' => 10], Course::class));

MissingConstructorArgumentsException: "Cannot create an instance of "App\Model\Course" from serialized data because its constructor requires the following parameters to be present : "$totalLessons"." at AbstractNormalizer.php line 417.

Possible Solution

The name converter is applied to the constructor argument, so totalLessons become total_lessons and the exception is thrown.

Additional Context

Is this correct? Do we apply different logic depending only on whether we are using properties or a constructor?

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