Skip to content

Uninitialized properties on readonly classes without constructor #53638

Closed as not planned
@repli2dev

Description

@repli2dev

Symfony version(s) affected

7.0.2

Description

In the below context I get object with uninitialized fields... If I remove the readonly from class definition it starts to work.

How to reproduce

<?php
declare(strict_types=1);

require_once __DIR__ . '/vendor/autoload.php';

use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

$encoders = [new JsonEncoder()];
$normalizers = [new ObjectNormalizer()];

$serializer = new Serializer($normalizers, $encoders);

readonly class FirstDTO {
    public int $id;
    public string $name;
}

$data = <<<EOF
{
    "id": 1,
    "name": "John Doe"
}
EOF;

try {
    $object = $serializer->deserialize($data, FirstDTO::class, 'json');
    var_dump($object);
} catch (\Symfony\Component\Serializer\Exception\ExceptionInterface $e) {
    echo $e->getMessage();
}

With

symfony/polyfill-ctype           v1.28.0 Symfony polyfill for ctype functions
symfony/polyfill-intl-grapheme   v1.28.0 Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-normalizer v1.28.0 Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-mbstring        v1.28.0 Symfony polyfill for the Mbstring extension
symfony/property-access          v7.0.0  Provides functions to read and write from/to an object or array using a simple string notation
symfony/property-info            v7.0.0  Extracts information about PHP class' properties using metadata of popular sources
symfony/serializer               v7.0.2  Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and JSON.
symfony/string                   v7.0.2  Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a unified way

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