Symfony bundle : Jms serializer and Entity implementing ArrayTransformerInterface

Hi everybody,

This is my very first post here :).

I’m working on a project using Oro Platform (https://oroinc.com/oroplatform/), which is based on Symfony 3.4. I use Algolia bundle 3.4.0 and I have a problem I don’t know to resolve :

  • I have no choice but use jms_serializer.
  • I want to customize the normalization of my entities to index.

So I tried to make my entity implements JMS\Serializer\ArrayTransformerInterface and write my custom toArray method :

class PrcSkuAttribute implements ArrayTransformerInterface
{
...
    /**
     * @inheritdoc
     */
    public function toArray($data, SerializationContext $context = null)
    {
        return [
            'sku' => $data->getSku()->getName(),
            'brand' => $data->getBrand()->getValue(),
            ...
        ];
    }
}

Unfortunately, this does not work and the toArray method of the JMS\Serializer\Serializer is used instead of my entity’s method :confused:.

I found this code in the SearchableEntity class which seems to be the “problem”, as it uses the Serializer::toArray method :

public function getSearchableArray()
    {
        $context = [
            'fieldsMapping' => $this->entityMetadata->fieldMappings,
        ];

        if ($this->useSerializerGroups) {
            $context['groups'] = [Searchable::NORMALIZATION_GROUP];
        }

        if ($this->normalizer instanceof NormalizerInterface) {
            return $this->normalizer->normalize($this->entity, Searchable::NORMALIZATION_FORMAT, $context);
        } elseif ($this->normalizer instanceof ArrayTransformerInterface) {
            return $this->normalizer->toArray($this->entity);
        }
    }

So is there a way to “force” the use of my toArray method ? Or maybe I missed something in the jms config, as I not used to it :confused: .