diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8daf231..63b8035 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,17 +11,14 @@ jobs: strategy: matrix: php-version: - - "7.1" - - "7.2" - - "7.3" - - "7.4" - "8.0" - "8.1" - "8.2" + - "8.3" dependencies: - highest include: - - php-version: "8.2" + - php-version: "8.3" dependencies: lowest fail-fast: false @@ -38,13 +35,6 @@ jobs: MYSQL_PASSWORD: odm steps: - - name: Use legacy password hashing in MySQL - if: ${{ contains(fromJson('["7.1", "7.2", "7.3"]'), matrix.php-version) }} - env: - MYSQL_PWD: odmroot - run: | - mysql -h127.0.0.1 -uroot -e "ALTER USER odm IDENTIFIED WITH mysql_native_password BY 'odm'" - - name: Start PostgreSQL run: | sudo systemctl start postgresql.service diff --git a/README.md b/README.md index b239d65..896832d 100644 --- a/README.md +++ b/README.md @@ -41,13 +41,14 @@ use Dunglas\DoctrineJsonOdm\Type\JsonDocumentType; use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer; +use Symfony\Component\Serializer\Normalizer\UidNormalizer; use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; if (!Type::hasType('json_document')) { Type::addType('json_document', JsonDocumentType::class); Type::getType('json_document')->setSerializer( - new Serializer([new BackedEnumNormalizer(), new DateTimeNormalizer(), new ArrayDenormalizer(), new ObjectNormalizer()], [new JsonEncoder()]) + new Serializer([new BackedEnumNormalizer(), new UidNormalizer(), new DateTimeNormalizer(), new ArrayDenormalizer(), new ObjectNormalizer()], [new JsonEncoder()]) ); } @@ -301,7 +302,7 @@ As an example we inject a custom normalizer service. Be aware that the order of dunglas_doctrine_json_odm.serializer: class: Dunglas\DoctrineJsonOdm\Serializer arguments: - - ['@App\MyCustom\Normalizer', '@?dunglas_doctrine_json_odm.normalizer.backed_enum', '@dunglas_doctrine_json_odm.normalizer.datetime', '@dunglas_doctrine_json_odm.normalizer.array', '@dunglas_doctrine_json_odm.normalizer.object'] + - ['@App\MyCustom\Normalizer', '@?dunglas_doctrine_json_odm.normalizer.backed_enum', '@?dunglas_doctrine_json_odm.normalizer.uid', '@dunglas_doctrine_json_odm.normalizer.datetime', '@dunglas_doctrine_json_odm.normalizer.array', '@dunglas_doctrine_json_odm.normalizer.object'] - ['@serializer.encoder.json'] - '@?dunglas_doctrine_json_odm.type_mapper' public: true diff --git a/composer.json b/composer.json index 911c55c..67307aa 100644 --- a/composer.json +++ b/composer.json @@ -13,19 +13,23 @@ } ], "require": { - "php": ">=7.1", + "php": ">=8.0", "doctrine/orm": "^2.6.3", - "symfony/property-access": "^4.4 || ^5.4 || ^6.0 || ^7.0", - "symfony/property-info": "^4.4 || ^5.4 || ^6.0 || ^7.0", - "symfony/serializer": "^4.4 || ^5.4 || ^6.0 || ^7.0" + "symfony/property-access": "^4.4 || ^5.4 || ^6.0", + "symfony/property-info": "^4.4 || ^5.4 || ^6.0", + "symfony/serializer": "^4.4 || ^5.4 || ^6.0" }, "require-dev": { "doctrine/annotations": "^1.0", "doctrine/doctrine-bundle": "^1.12.13 || ^2.2", "doctrine/dbal": "^2.7 || ^3.3", - "symfony/finder": "^4.4 || ^5.4 || ^6.0 || ^7.0", - "symfony/framework-bundle": "^4.4 || ^5.4 || ^6.0 || ^7.0", - "symfony/phpunit-bridge": "^6.0 || ^7.0" + "symfony/finder": "^4.4 || ^5.4 || ^6.0", + "symfony/framework-bundle": "^4.4 || ^5.4 || ^6.0", + "symfony/phpunit-bridge": "^6.0", + "symfony/uid": "^5.4 || ^6.0" + }, + "conflict": { + "symfony/doctrine-bridge": ">=7.0.0" }, "suggest": { "symfony/framework-bundle": "To use the provided bundle.", diff --git a/src/Bundle/DependencyInjection/DunglasDoctrineJsonOdmExtension.php b/src/Bundle/DependencyInjection/DunglasDoctrineJsonOdmExtension.php index 3273ea8..a660bee 100644 --- a/src/Bundle/DependencyInjection/DunglasDoctrineJsonOdmExtension.php +++ b/src/Bundle/DependencyInjection/DunglasDoctrineJsonOdmExtension.php @@ -15,6 +15,7 @@ use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer; +use Symfony\Component\Serializer\Normalizer\UidNormalizer; /** * @author Kévin Dunglas @@ -51,6 +52,10 @@ public function load(array $configs, ContainerBuilder $container): void $container->removeDefinition('dunglas_doctrine_json_odm.normalizer.backed_enum'); } + if (!class_exists(UidNormalizer::class)) { + $container->removeDefinition('dunglas_doctrine_json_odm.normalizer.uid'); + } + $config = $this->processConfiguration(new Configuration(), $configs); if ($config['type_map'] ?? []) { diff --git a/src/Bundle/Resources/config/services.xml b/src/Bundle/Resources/config/services.xml index ad0eb25..5cbe5dc 100644 --- a/src/Bundle/Resources/config/services.xml +++ b/src/Bundle/Resources/config/services.xml @@ -8,6 +8,8 @@ + + @@ -25,6 +27,7 @@ + diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 8e8a92c..bad34ef 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -18,6 +18,7 @@ use Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Entity\Product; use Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Enum\InputMode; use Symfony\Component\Console\Input\StringInput; +use Symfony\Component\Uid\Uuid; /** * @author Kévin Dunglas @@ -218,4 +219,31 @@ public function testStoreAndRetrieveEnum(): void $this->assertSame(InputMode::EMAIL, $retrievedProduct->attributes[0]->value); } + + /** + * @requires function \Symfony\Component\Serializer\Normalizer\UidNormalizer::normalize + */ + public function testStoreAndRetrieveUid(): void + { + $uuid = '1a87e1f2-1569-4493-a4a8-bc1915ca5631'; + + $attribute = new Attribute(); + $attribute->key = 'uid'; + $attribute->value = Uuid::fromString($uuid); + + $product = new Product(); + $product->name = 'My product'; + $product->attributes = [$attribute]; + + $manager = self::$kernel->getContainer()->get('doctrine')->getManagerForClass(Product::class); + $manager->persist($product); + $manager->flush(); + + $manager->clear(); + + $retrievedProduct = $manager->find(Product::class, $product->id); + + $this->assertInstanceOf(Uuid::class, $retrievedProduct->attributes[0]->value); + $this->assertEquals($uuid, (string) $retrievedProduct->attributes[0]->value); + } } diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php index a3aa259..29d9792 100644 --- a/tests/SerializerTest.php +++ b/tests/SerializerTest.php @@ -19,6 +19,7 @@ use Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Document\WithMappedType; use Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Entity\Foo; use Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Enum\InputMode; +use Symfony\Component\Uid\UuidV4; /** * @author Kévin Dunglas @@ -223,4 +224,21 @@ public function testSerializeEnum(): void $this->assertEquals($value, $restoredValue); } + + /** + * @requires function \Symfony\Component\Serializer\Normalizer\UidNormalizer::normalize + */ + public function testSerializeUid(): void + { + $serializer = self::$kernel->getContainer()->get('dunglas_doctrine_json_odm.serializer'); + + $value = new UuidV4('4a7ba820-c806-4579-a907-193a2493863b'); + $data = $serializer->serialize($value, 'json'); + $decodeData = json_decode($data, true); + $this->assertArrayHasKey('#type', $decodeData); + $this->assertSame(UuidV4::class, $decodeData['#type']); + $restoredValue = $serializer->deserialize($data, '', 'json'); + + $this->assertEquals($value, $restoredValue); + } }