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...
-
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
-
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)
-
Prepare your database:
bin/console doctrine:migrations:diff
bin/console doctrine:migrations:migrate
-
Run symfony's server or setup a vhost and go to the registration page http:///register in a browser. Fill out the form and click the register button
-
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
bug