-
-
Notifications
You must be signed in to change notification settings - Fork 587
Description
Hello,
I'm having a bit of an issue after migrating from annotations to attributes in the sense that I have a class such as
<?php
declare(strict_types=1);
namespace App\Tests\Services\SerializeObjects;
use Doctrine\Common\Collections\ArrayCollection;
use JMS\Serializer\Annotation as JMS;
class TestList
{
#[JMS\Type('integer')]
#[JMS\SerializedName('item')]
private int $item;
#[JMS\Type('Doctrine\Common\Collections\ArrayCollection<App\Tests\Services\SerializeObjects\TestObject>')]
#[JMS\SerializedName('list')]
private ArrayCollection $testObjects;
public function __construct()
{
$this->item = 2;
$this->testObjects = new ArrayCollection([
new TestObject(),
new TestObject(),
]);
}
/**
* Get the value of item
*/
public function getItem(): int
{
return $this->item;
}
/**
* Get the value of testObjects
*/
public function getTestObjects(): ArrayCollection
{
return $this->testObjects;
}
}
when I call serialize on this, list becomes a JSON object that contains an elements array which contains the actual TestObject representations. If I use attributes, it works as expected (eg: list is a JSON array of TestObject representations). It seems that the attributes behavior is different somehow in a way that's not documented.
How the serializer is created:
$cacheDir = '/tmp/jms';
$strategy =
new \JMS\Serializer\Naming\SerializedNameAnnotationStrategy(
new \JMS\Serializer\Naming\IdenticalPropertyNamingStrategy(
new \JMS\Serializer\Naming\CamelCaseNamingStrategy()
)
);
$builder = SerializerBuilder::create()
->setPropertyNamingStrategy($strategy)
->setExpressionEvaluator(new ExpressionEvaluator(new ExpressionLanguage()))
->setSerializationContextFactory(function () {
return SerializationContext::create()
->setSerializeNull(true)
;
})
->setDebug(true);
$builder->setCacheDir($cacheDir);
$jms = $builder->build();
Now, my problem seems to go away if I manually register the ArrayCollectionHandler with the handler registry, though I was under the impression that the built-in handlers don't need to be manually registered (the docs says that the custom ones do).
Is this something lacking in the docs ?