core: Can't serialize Ramsey\Uuid\Uuid identifier

When api-platform try to serialize my entity, I get the Resource "Ramsey\Uuid\Uuid" not found. error. It seems to only happen on deep serialization. In the following stack trace, I’m exposing AppBundle\Entity\Theme entities which have a bidirectional ManyToOne to a AppBundle\Entity\Category.

Also, I tried to expose my AppBundle\Entity\User entities which use the same identifier configuration and serialization works well when using application/json format but not application/ld+json format.

Stack trace:

[1] ApiPlatform\Core\Exception\ResourceClassNotFoundException: Resource "Ramsey\Uuid\Uuid" not found.
    at n/a
        in /var/www/vendor/api-platform/core/src/Metadata/Resource/Factory/ExtractorResourceMetadataFactory.php line 72

    at ApiPlatform\Core\Metadata\Resource\Factory\ExtractorResourceMetadataFactory->handleNotFound(null, 'Ramsey\\Uuid\\Uuid')
        in /var/www/vendor/api-platform/core/src/Metadata/Resource/Factory/ExtractorResourceMetadataFactory.php line 50

    at ApiPlatform\Core\Metadata\Resource\Factory\ExtractorResourceMetadataFactory->create('Ramsey\\Uuid\\Uuid')
        in /var/www/vendor/api-platform/core/src/Metadata/Resource/Factory/ShortNameResourceMetadataFactory.php line 35

    at ApiPlatform\Core\Metadata\Resource\Factory\ShortNameResourceMetadataFactory->create('Ramsey\\Uuid\\Uuid')
        in /var/www/vendor/api-platform/core/src/Metadata/Resource/Factory/OperationResourceMetadataFactory.php line 35

    at ApiPlatform\Core\Metadata\Resource\Factory\OperationResourceMetadataFactory->create('Ramsey\\Uuid\\Uuid')
        in /var/www/vendor/api-platform/core/src/Metadata/Resource/Factory/CachedResourceMetadataFactory.php line 53

    at ApiPlatform\Core\Metadata\Resource\Factory\CachedResourceMetadataFactory->create('Ramsey\\Uuid\\Uuid')
        in /var/www/vendor/api-platform/core/src/Metadata/Property/Factory/SerializerPropertyMetadataFactory.php line 156

    at ApiPlatform\Core\Metadata\Property\Factory\SerializerPropertyMetadataFactory->getEffectiveSerializerGroups(array(), 'Ramsey\\Uuid\\Uuid')
        in /var/www/vendor/api-platform/core/src/Metadata/Property/Factory/SerializerPropertyMetadataFactory.php line 51

    at ApiPlatform\Core\Metadata\Property\Factory\SerializerPropertyMetadataFactory->create('Ramsey\\Uuid\\Uuid', 'bytes', array())
        in /var/www/vendor/api-platform/core/src/Bridge/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactory.php line 48

    at ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\ValidatorPropertyMetadataFactory->create('Ramsey\\Uuid\\Uuid', 'bytes', array())
        in /var/www/vendor/api-platform/core/src/Metadata/Property/Factory/CachedPropertyMetadataFactory.php line 53

    at ApiPlatform\Core\Metadata\Property\Factory\CachedPropertyMetadataFactory->create('Ramsey\\Uuid\\Uuid', 'bytes')
        in /var/www/vendor/api-platform/core/src/Bridge/Symfony/Routing/IriConverter.php line 134

    at ApiPlatform\Core\Bridge\Symfony\Routing\IriConverter->getIdentifiersFromItem(object(Category))
        in /var/www/vendor/api-platform/core/src/Bridge/Symfony/Routing/IriConverter.php line 83

    at ApiPlatform\Core\Bridge\Symfony\Routing\IriConverter->getIriFromItem(object(Category))
        in /var/www/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php line 420

    at ApiPlatform\Core\Serializer\AbstractItemNormalizer->normalizeRelation(object(PropertyMetadata), object(Category), 'AppBundle\\Entity\\Category', 'json', array('collection_operation_name' => 'get', 'resource_class' => 'AppBundle\\Entity\\Theme', 'request_uri' => '/api/themes.json', 'api_sub_level' => true, 'api_normalize' => true, 'cache_key' => '9db9240ee742c4b9577d52a00843bee3', 'circular_reference_limit' => array('00000000040ec8b30000000028ca7688' => 1)))
        in /var/www/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php line 397

    at ApiPlatform\Core\Serializer\AbstractItemNormalizer->getAttributeValue(object(Theme), 'categorie', 'json', array('collection_operation_name' => 'get', 'resource_class' => 'AppBundle\\Entity\\Theme', 'request_uri' => '/api/themes.json', 'api_sub_level' => true, 'api_normalize' => true, 'cache_key' => '9db9240ee742c4b9577d52a00843bee3', 'circular_reference_limit' => array('00000000040ec8b30000000028ca7688' => 1)))
        in /var/www/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php line 78

    at Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->normalize(object(Theme), 'json', array('collection_operation_name' => 'get', 'resource_class' => 'AppBundle\\Entity\\Theme', 'request_uri' => '/api/themes.json', 'api_sub_level' => true, 'api_normalize' => true, 'cache_key' => '9db9240ee742c4b9577d52a00843bee3', 'circular_reference_limit' => array('00000000040ec8b30000000028ca7688' => 1)))
        in /var/www/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php line 84

    at ApiPlatform\Core\Serializer\AbstractItemNormalizer->normalize(object(Theme), 'json', array('collection_operation_name' => 'get', 'resource_class' => 'AppBundle\\Entity\\Theme', 'request_uri' => '/api/themes.json', 'api_sub_level' => true, 'api_normalize' => true))
        in /var/www/vendor/symfony/serializer/Serializer.php line 142

    at Symfony\Component\Serializer\Serializer->normalize(object(Theme), 'json', array('collection_operation_name' => 'get', 'resource_class' => 'AppBundle\\Entity\\Theme', 'request_uri' => '/api/themes.json'))
        in /var/www/vendor/symfony/serializer/Serializer.php line 152

    at Symfony\Component\Serializer\Serializer->normalize(object(Paginator), 'json', array('collection_operation_name' => 'get', 'resource_class' => 'AppBundle\\Entity\\Theme', 'request_uri' => '/api/themes.json'))
        in /var/www/vendor/symfony/serializer/Serializer.php line 115

    at Symfony\Component\Serializer\Serializer->serialize(object(Paginator), 'json', array('collection_operation_name' => 'get', 'resource_class' => 'AppBundle\\Entity\\Theme', 'request_uri' => '/api/themes.json'))
        in /var/www/vendor/api-platform/core/src/EventListener/SerializeListener.php line 64

    at ApiPlatform\Core\EventListener\SerializeListener->onKernelView(object(GetResponseForControllerResultEvent), 'kernel.view', object(TraceableEventDispatcher))
        in  line 

    at call_user_func(array(object(SerializeListener), 'onKernelView'), object(GetResponseForControllerResultEvent), 'kernel.view', object(TraceableEventDispatcher))
        in /var/www/vendor/symfony/event-dispatcher/Debug/WrappedListener.php line 106

    at Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke(object(GetResponseForControllerResultEvent), 'kernel.view', object(ContainerAwareEventDispatcher))
        in  line 

    at call_user_func(object(WrappedListener), object(GetResponseForControllerResultEvent), 'kernel.view', object(ContainerAwareEventDispatcher))
        in /var/www/vendor/symfony/event-dispatcher/EventDispatcher.php line 174

    at Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(array(object(WrappedListener), array(object(ValidateListener), 'onKernelView'), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.view', object(GetResponseForControllerResultEvent))
        in /var/www/vendor/symfony/event-dispatcher/EventDispatcher.php line 43

    at Symfony\Component\EventDispatcher\EventDispatcher->dispatch('kernel.view', object(GetResponseForControllerResultEvent))
        in /var/www/vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php line 136

    at Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch('kernel.view', object(GetResponseForControllerResultEvent))
        in /var/www/vendor/symfony/http-kernel/HttpKernel.php line 158

    at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
        in /var/www/vendor/symfony/http-kernel/HttpKernel.php line 68

    at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
        in /var/www/vendor/symfony/http-kernel/Kernel.php line 168

    at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
        in /var/www/web/app_dev.php line 13

Identifier configuration:

    /**
     * @ORM\Id
     * @ORM\Column(type="uuid")
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     *
     * @var Uuid
     */
    protected $id;

    public function getId()
    {
        return $this->id;
    }

    public function setId(Uuid $id)
    {
        $this->id = $id;
    }

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (14 by maintainers)

Most upvoted comments

@fbourigault OK I’ve found a solution.

According to your comment, it’s not recommended to use objects as identifiers. Now I use a string identifier, but still use Ramsey as generator (overridden):

// src/AppBundle/Entity/Foo.php

class Foo
{
    /**
     * @var string
     *
     * @ORM\Id
     * @ORM\Column(type="string")
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="AppBundle\Doctrine\Generator\UuidGenerator")
     */
    private $id;

And the UuidGenerator overridden:

// src/AppBundle/Doctrine/Generator/UuidGenerator.php

namespace AppBundle\Doctrine\Generator;

use Doctrine\ORM\EntityManager;
use Ramsey\Uuid\Doctrine\UuidGenerator as BaseUuidGenerator;
use Ramsey\Uuid\Uuid;

class UuidGenerator extends BaseUuidGenerator
{
    /**
     * {@inheritdoc}
     */
    public function generate(EntityManager $em, $entity)
    {
        /** @var Uuid $uuid */
        $uuid = parent::generate($em, $entity);

        return $uuid->toString();
    }
}

Hope this solution could help if other people have this issue 😃

I start working on that.

This is related to #916.

@api-platform/core-team I think we need to make fixing this a priority.

We can add __toString support in getIdentifiersFromItem.

This can be closed @fbourigault @dunglas ?