uuid: Could not convert database value "11ead97b-ffa1-7458-a..." to Doctrine Type uuid_binary_ordered_time. Expected format: UuidV1

Describe the bug

For the last couple of weeks I’ve noticed that in symfony UuidOrderedTimeGenerator is generating other than v1 Uuids intead of the expected v1, and that leads to throwing a converter error when validating the value

Exception message: Could not convert database value “xxxxxxxx-xxxx-xxxx-x…” to Doctrine Type uuid_binary_ordered_time. Expected format: UuidV1

To Reproduce…

  1. Setup a new symfony project with registration and authentication:

    composer create-project symfony/website-skeleton symf cd symf composer require ramsey/uuid-doctrine composer require symfonycasts/verify-email-bundle bin/console make:user bin/console make:registration-form during this process choose to login automatically on registration success bin/console make:auth

  2. Basic project configuration:

    Follow instructions given by make:auth to fix the return in onAuthenticationSuccess to a valid route (use bin/console make:controller DefaultController to create an empty controller to use here) change config/packages/ramsey._uuid_doctrine.yaml to be:

doctrine:
    dbal:
        types:
            uuid: 'Ramsey\Uuid\Doctrine\UuidType'
            uuid_binary: 'Ramsey\Uuid\Doctrine\UuidBinaryType'
            uuid_binary_ordered_time: 'Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType'
        mapping_types:
            uuid_binary: binary
            uuid_binary_ordered_time: binary
In src/Entity/User.php change the type of $id to uuid
use \Ramsey\Uuid\UuidInterface;
....
/**
 * @ORM\Id()
 * @ORM\Column(type="uuid_binary_ordered_time", unique=true)
 * @ORM\GeneratedValue(strategy="CUSTOM")
 * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator")
 */
private $id;
...
public function getId(): ?UuidInterface
{
    return $this->id;
}
change .env so that `MAILER_DSN` and `DATABASE_URL` are both set and valid according to your own mail and database servers (i'm using my ISP's mail server and mariadb-10.4.7 in my test)
  1. Prepare your database:

    bin/console doctrine:migrations:diff bin/console doctrine:migrations:migrate

  2. Run symfony’s server or setup a vhost and go to the registration page http://<someaddress>/register in a browser. Fill out the form and click the register button

  3. See output similar to the following:

    on registration success a new entry is added to the user table in the database but during the login process the following exception is thrown and unhandled

    Could not convert database value "11ead97b-ffa1-7458-a..." to Doctrine Type uuid_binary_ordered_time. Expected format: UuidV1
    

Examining the value of the id it is not in fact validated as version 1

Expected behavior

A new entry is added to the user table in the database using the information typed in the form, login suing those credentials and redirect to the route setup in onAuthenticationSuccess .

Screenshots or output

Symfony Exception
ConversionException
HTTP 500 Internal Server Error
Could not convert database value "11ead97b-ffa1-7458-a..." to Doctrine Type uuid_binary_ordered_time. Expected format: UuidV1
in vendor/ramsey/uuid-doctrine/src/UuidBinaryOrderedTimeType.php :: conversionFailedFormat (line 190)

        {
            if (1 !== $value->getVersion()) { 
                throw ConversionException::conversionFailedFormat(
                    $value->toString(),
                    self::NAME,
                    self::ASSERT_FORMAT
                );
            }
        }
        /**

in vendor/ramsey/uuid-doctrine/src/UuidBinaryOrderedTimeType.php -> assertUuidV1 (line 204)

         *
         * @throws ConversionException
         */
        private function encode(UuidInterface $uuid)
        {
            $this->assertUuidV1($uuid);
            return $this->getCodec()->encodeBinary($uuid);
        }
        /**

in vendor/ramsey/uuid-doctrine/src/UuidBinaryOrderedTimeType.php -> encode (line 108)

            if ($value === null || $value === '') {
                return null;
            }
            if ($value instanceof UuidInterface) {
                return $this->encode($value);  
            }
            try {
                if (is_string($value) || method_exists($value, '__toString')) {
                    $uuid = $this->getUuidFactory()->fromString((string) $value);


Environment details

  • OS: Linux (Ubuntu 20.04)
  • PHP version: 7.4.5
  • ramsey/uuid version: 4.1.0
  • ramsey/uuid-doctrine version: 1.6.0
  • ramsey/collection version: 1.0.1

Additional context

This started happenning a couple of weeks back when i updated ramsey/uuid using composer update - can’t remember what version i had before

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18

Commits related to this issue

Most upvoted comments

@eXtreme can you tell me where to do toString() and UuidV1::fromString

@ziarv in public function serialize() and public function unserialize($serialized) (respectively) of my security user class. The solution was not tu serialize Uuid object but just a string representation.

Just noted something: if you get the string representation of the UUID, the 13th character is the version indicator. However, the UUIDs being created have the 13th digit go off the range of valid versions.

Here’s some examples of generated UUIDs:

11ead97b-ffa1-7458-a66b-00155d000701    <-- Version 7?
11eadc33-e890-a6ca-ba10-00155d000701    <-- Version 10?
11eadd9d-e00a-bbba-8789-00155d000701    <-- Version 11?

That’s why getVersion ends up null - it doesn’t recognize the version identifier as valid Not sure why this is happening though - tested all generators and all work by itself

Uuid::uuid1();
4ca15652-decb-11ea-9c38-00155d000701   <-- Version 1

Uuid::uuid2(Uuid::DCE_DOMAIN_PERSON);
00000021-decb-21ea-8800-00155d000701   <-- Version 2

Uuid::uuid3(Uuid::NAMESPACE_URL, 'https://www.php.net');
3f703955-aaba-3e70-a3cb-baff6aa3b28f   <-- Version 3

Uuid::uuid4();
f3bf1ff6-393b-4ac8-80c4-ea8007eac922   <-- Version 4

Uuid::uuid5(Uuid::NAMESPACE_URL, 'https://www.php.net');
a8f6ae40-d8a7-58f0-be05-a22f94eca9ec   <-- Version 5

Uuid::uuid6();
1eadecb4-ca1c-601a-8e91-00155d000701   <-- Version 6

Uuid::fromDateTime($datetime);
4ca15652-decb-11ea-8312-00155d000701   <-- Version 1