When trying to serialize a model that uses traits, JMSSerializer does not serialize properties included by that trait. I am using yaml to configure the serializer but it seems that it's not working.
trait IdentityTrait
{
protected $id;
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getId()
{
return $this->id;
}
}
class OurClass {
use IdentityTrait;
protected $test;
public function getTest() {
$this->test;
}
}
JMSSerializerBundle is used and the following yaml is located in Resources/config/serializer/Model.Traits.IdentityTrait.yml
MyProject\Component\Core\Model\Traits\IdentityTrait:
exclusion_policy: NONE
properties:
id:
expose: true
And the OurClass
configuration is located in Resources/config/serializer/Model.OurClass.yml
MyProject\Component\Core\Model\OurClass:
exclusion_policy: NONE
properties:
test:
expose: true
Some code has been ignored to focus on the problem
PHP traits are introduced since PHP 5.4.0, the latest JMSSerializer code supports PHP 5.3.2. Note
"require": {"php": ">=5.3.2",
Looking around the code, this feature is not supported (yet). This problem is very related to this issue on JMSSerializer github.