Skip to content

[Serializer] @var tag is no longer supported when deserializing array of objects #62712

@qconer

Description

@qconer

Symfony version(s) affected

7.4.2

Description

After upgrading Symfony to 7.4, the Serializer no longer respects @var PHPDoc annotations placed directly on constructor properties.

Previously, the following code worked correctly and the $data property was deserialized as an array of objects:

public function __construct(
    /** @var SomeInnerClass[]|null */
    public ?array $data,
) {
}

After the upgrade, using this form results in $data being deserialized as an array of arrays instead of an array of SomeInnerClass objects.

Now deserialization works correctly only if the type information is moved to a constructor-level @param PHPDoc:

/**
 * @param SomeInnerClass[]|null $data
 */
public function __construct(
    public ?array $data,
) {
}

How to reproduce

class SomeInnerClass
{
    public function __construct(
        public string $foo,
    ) {
    }
}

class SomeClass
{
    public function __construct(
        /** @var SomeInnerClass[]|null */
        public ?array $data,
    ) {
    }
}


$object = $serializer->deserialize(
  '{"data":[{"foo":"bar"}]}', 
  SomeClass::class, 
  'json'
); 

// $object->data contains array of arrays instead of array of objects

Possible Solution

#62713
#62717

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