The following error occurs during object deserialization:
Symfony\Component\Serializer\Exception\NotNormalizableValueException
The type of the "products" attribute for class "shop\manage\flexbe\objects\Lead" must be one of "shop\manage\flexbe\objects\Product[]" ("array" given).
Have JSON-object:
{
"id": "9757241",
"time": "1567105530",
// other params
"products": [
{
"title": "Product name",
"count": 1
}
]
}
In the Lead class that describes the object related methods to "products":
private $products = [];
/**
* @return Product[]
*/
public function getProducts()
{
return $this->products;
}
/**
* @param Product $product
*/
public function addProduct(Product $product): void
{
$this->products[] = $product;
}
Deserialization Code:
$normalizer = new ObjectNormalizer(null, null, new PropertyAccessor(), new ReflectionExtractor());
$serializer = new Serializer(array($normalizer), array(new JsonEncoder()));
$lead = $serializer->deserialize($data, Lead::class, 'json');
I can’t understand what the problem is. It is expected using the addProduct() method deserializer should bypass the array and add all objects to Product class like in this case.