Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialize an attribute of type array of an object without @Type #1583

Open
moto-billie opened this issue Dec 23, 2024 · 2 comments
Open

Deserialize an attribute of type array of an object without @Type #1583

moto-billie opened this issue Dec 23, 2024 · 2 comments

Comments

@moto-billie
Copy link

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no

Steps required to reproduce the problem

class A {
    private readonly string $name;
    private readonly array $parameters;

    public function __construct(
        string $name,
        array $parameters
    )
    {
        $this->name = $name;
        $this->parameters = $parameters;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getParameters(): array
    {
        return $this->parameters;
    }
}

$serializer = SerializerBuilder::create()->build();

$a = new A(
    "Foo",
    [
        "age" => 30,
        "city" => "Berlin",
    ]
);
$aSerialized = $serializer->serialize($a, 'json');
$aDeserialized = $serializer->deserialize($aSerialized, A::class, 'json');

Expected Result

  • I would expect to successfully deserialize my object again

Actual Result

  • Fatal error: Uncaught JMS\Serializer\Exception\RuntimeException: You must define a type for A::$parameters.

I see in \JMS\Serializer\Metadata\Driver\TypedPropertiesDriver::reorderTypes that array is added as a built-in type. But in \JMS\Serializer\Metadata\Driver\TypedPropertiesDriver::getDefaultWhiteList it is not whitelisted.

Is there a way to inject the allowed list to \JMS\Serializer\Metadata\Driver\TypedPropertiesDriver?

Or is there any way other than using @type annotation to be able to deserialize attributes of type array of objects?

@scyzoryck
Copy link
Collaborator

Hi!
Serializer need to know what it inside an array to produce the correct data. Using #[Type] attribute is the one way but it should also work with php docs. Could you try to add phpdoc type like string[] as a type to your property?

@moto-billie
Copy link
Author

Hey,
Thanks for the reply.

So, I tried with PhpDoc both:

  • /** @var array $parameters */
  • /** @var string[] $parameters */

But still faced the same error.

Besides that, your suggestion makes me wonder how /** @JMS\Serializer\Annotation\Type("array") */ works.
Because:

  1. I'm just saying this field is an array, not specifying what is inside the array.
  2. also the array has both string and integer which with using the @JMS\Serializer\Annotation\Type("array") is working just fine.

I think if there's a way to inject the allowed list to the constructor of \JMS\Serializer\Metadata\Driver\TypedPropertiesDriver the problem will be solved. I see it is in the constructor but couldn't find a straightforward way to just inject this value, and also couldn't find anything in the documentation about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants