You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
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?
Besides that, your suggestion makes me wonder how /** @JMS\Serializer\Annotation\Type("array") */ works.
Because:
I'm just saying this field is an array, not specifying what is inside the array.
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.
Steps required to reproduce the problem
Expected Result
Actual Result
I see in
\JMS\Serializer\Metadata\Driver\TypedPropertiesDriver::reorderTypes
thatarray
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?
The text was updated successfully, but these errors were encountered: