Skip to content

[Serializer] Support readonly classes or classes that use readonly for constructor property promotion #49808

Closed as not planned
@ruudk

Description

@ruudk

Description

The Serializer is perfectly capable of deserializing the following object without configuration:

final class CreateUserCommand
{
    private function __construct(
        public string $firstName,
        public string $lastName,
    ) {}
}

But as soon as you make the class readonly (PHP 8.2 feature) or make the constructor properties readonly (PHP 8.1) you'll get an error:

Error : Cannot initialize readonly property CreateUserCommand::$firstName from scope Symfony\Component\PropertyAccess\PropertyAccessor
 /Volumes/CS/www/cosmos/vendor/symfony/property-access/PropertyAccessor.php:530
final readonly class CreateUserCommand
{
    private function __construct(
        public string $firstName,
        public string $lastName,
    ) {}
}
final class CreateUserCommand
{
    private function __construct(
        public readonly string $firstName,
        public readonly string $lastName,
    ) {}
}

Example

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