Closed as not planned
Description
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));
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?