Skip to content

Commit 18a4d61

Browse files
amenophisJérémy Leherpeur
andauthored
feat: Uid normalizer (#131)
* Add Uid normalizer * Install symfony/uid + Fix tests * Add php 7.2 limit on UUID tests + add test * Remove service if class doesn't exist * Remove php < 8 support + support php 8.3 * Revert symfony 7 support * Conflict with symfony/doctrine-bridge >= 7.0.0 --------- Co-authored-by: Jérémy Leherpeur <[email protected]>
1 parent 6a1f189 commit 18a4d61

File tree

7 files changed

+70
-21
lines changed

7 files changed

+70
-21
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@ jobs:
1111
strategy:
1212
matrix:
1313
php-version:
14-
- "7.1"
15-
- "7.2"
16-
- "7.3"
17-
- "7.4"
1814
- "8.0"
1915
- "8.1"
2016
- "8.2"
17+
- "8.3"
2118
dependencies:
2219
- highest
2320
include:
24-
- php-version: "8.2"
21+
- php-version: "8.3"
2522
dependencies: lowest
2623
fail-fast: false
2724

@@ -38,13 +35,6 @@ jobs:
3835
MYSQL_PASSWORD: odm
3936

4037
steps:
41-
- name: Use legacy password hashing in MySQL
42-
if: ${{ contains(fromJson('["7.1", "7.2", "7.3"]'), matrix.php-version) }}
43-
env:
44-
MYSQL_PWD: odmroot
45-
run: |
46-
mysql -h127.0.0.1 -uroot -e "ALTER USER odm IDENTIFIED WITH mysql_native_password BY 'odm'"
47-
4838
- name: Start PostgreSQL
4939
run: |
5040
sudo systemctl start postgresql.service

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ use Dunglas\DoctrineJsonOdm\Type\JsonDocumentType;
4141
use Symfony\Component\Serializer\Encoder\JsonEncoder;
4242
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
4343
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
44+
use Symfony\Component\Serializer\Normalizer\UidNormalizer;
4445
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
4546
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
4647

4748
if (!Type::hasType('json_document')) {
4849
Type::addType('json_document', JsonDocumentType::class);
4950
Type::getType('json_document')->setSerializer(
50-
new Serializer([new BackedEnumNormalizer(), new DateTimeNormalizer(), new ArrayDenormalizer(), new ObjectNormalizer()], [new JsonEncoder()])
51+
new Serializer([new BackedEnumNormalizer(), new UidNormalizer(), new DateTimeNormalizer(), new ArrayDenormalizer(), new ObjectNormalizer()], [new JsonEncoder()])
5152
);
5253
}
5354

@@ -301,7 +302,7 @@ As an example we inject a custom normalizer service. Be aware that the order of
301302
dunglas_doctrine_json_odm.serializer:
302303
class: Dunglas\DoctrineJsonOdm\Serializer
303304
arguments:
304-
- ['@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']
305+
- ['@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']
305306
- ['@serializer.encoder.json']
306307
- '@?dunglas_doctrine_json_odm.type_mapper'
307308
public: true

composer.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,23 @@
1313
}
1414
],
1515
"require": {
16-
"php": ">=7.1",
16+
"php": ">=8.0",
1717
"doctrine/orm": "^2.6.3",
18-
"symfony/property-access": "^4.4 || ^5.4 || ^6.0 || ^7.0",
19-
"symfony/property-info": "^4.4 || ^5.4 || ^6.0 || ^7.0",
20-
"symfony/serializer": "^4.4 || ^5.4 || ^6.0 || ^7.0"
18+
"symfony/property-access": "^4.4 || ^5.4 || ^6.0",
19+
"symfony/property-info": "^4.4 || ^5.4 || ^6.0",
20+
"symfony/serializer": "^4.4 || ^5.4 || ^6.0"
2121
},
2222
"require-dev": {
2323
"doctrine/annotations": "^1.0",
2424
"doctrine/doctrine-bundle": "^1.12.13 || ^2.2",
2525
"doctrine/dbal": "^2.7 || ^3.3",
26-
"symfony/finder": "^4.4 || ^5.4 || ^6.0 || ^7.0",
27-
"symfony/framework-bundle": "^4.4 || ^5.4 || ^6.0 || ^7.0",
28-
"symfony/phpunit-bridge": "^6.0 || ^7.0"
26+
"symfony/finder": "^4.4 || ^5.4 || ^6.0",
27+
"symfony/framework-bundle": "^4.4 || ^5.4 || ^6.0",
28+
"symfony/phpunit-bridge": "^6.0",
29+
"symfony/uid": "^5.4 || ^6.0"
30+
},
31+
"conflict": {
32+
"symfony/doctrine-bridge": ">=7.0.0"
2933
},
3034
"suggest": {
3135
"symfony/framework-bundle": "To use the provided bundle.",

src/Bundle/DependencyInjection/DunglasDoctrineJsonOdmExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1616
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1717
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
18+
use Symfony\Component\Serializer\Normalizer\UidNormalizer;
1819

1920
/**
2021
* @author Kévin Dunglas <[email protected]>
@@ -51,6 +52,10 @@ public function load(array $configs, ContainerBuilder $container): void
5152
$container->removeDefinition('dunglas_doctrine_json_odm.normalizer.backed_enum');
5253
}
5354

55+
if (!class_exists(UidNormalizer::class)) {
56+
$container->removeDefinition('dunglas_doctrine_json_odm.normalizer.uid');
57+
}
58+
5459
$config = $this->processConfiguration(new Configuration(), $configs);
5560

5661
if ($config['type_map'] ?? []) {

src/Bundle/Resources/config/services.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
<service id="dunglas_doctrine_json_odm.normalizer.backed_enum" class="Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer" public="false" />
1010

11+
<service id="dunglas_doctrine_json_odm.normalizer.uid" class="Symfony\Component\Serializer\Normalizer\UidNormalizer" public="false" />
12+
1113
<service id="dunglas_doctrine_json_odm.normalizer.datetime" class="Symfony\Component\Serializer\Normalizer\DateTimeNormalizer" public="false" />
1214

1315
<service id="dunglas_doctrine_json_odm.normalizer.array" class="Symfony\Component\Serializer\Normalizer\ArrayDenormalizer" public="false" />
@@ -25,6 +27,7 @@
2527
<service id="dunglas_doctrine_json_odm.serializer" class="Dunglas\DoctrineJsonOdm\Serializer" public="true">
2628
<argument type="collection">
2729
<argument type="service" id="dunglas_doctrine_json_odm.normalizer.backed_enum" on-invalid="ignore" />
30+
<argument type="service" id="dunglas_doctrine_json_odm.normalizer.uid" on-invalid="ignore" />
2831
<argument type="service" id="dunglas_doctrine_json_odm.normalizer.datetime" />
2932
<argument type="service" id="dunglas_doctrine_json_odm.normalizer.array" />
3033
<argument type="service" id="dunglas_doctrine_json_odm.normalizer.object" />

tests/FunctionalTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Entity\Product;
1919
use Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Enum\InputMode;
2020
use Symfony\Component\Console\Input\StringInput;
21+
use Symfony\Component\Uid\Uuid;
2122

2223
/**
2324
* @author Kévin Dunglas <[email protected]>
@@ -218,4 +219,31 @@ public function testStoreAndRetrieveEnum(): void
218219

219220
$this->assertSame(InputMode::EMAIL, $retrievedProduct->attributes[0]->value);
220221
}
222+
223+
/**
224+
* @requires function \Symfony\Component\Serializer\Normalizer\UidNormalizer::normalize
225+
*/
226+
public function testStoreAndRetrieveUid(): void
227+
{
228+
$uuid = '1a87e1f2-1569-4493-a4a8-bc1915ca5631';
229+
230+
$attribute = new Attribute();
231+
$attribute->key = 'uid';
232+
$attribute->value = Uuid::fromString($uuid);
233+
234+
$product = new Product();
235+
$product->name = 'My product';
236+
$product->attributes = [$attribute];
237+
238+
$manager = self::$kernel->getContainer()->get('doctrine')->getManagerForClass(Product::class);
239+
$manager->persist($product);
240+
$manager->flush();
241+
242+
$manager->clear();
243+
244+
$retrievedProduct = $manager->find(Product::class, $product->id);
245+
246+
$this->assertInstanceOf(Uuid::class, $retrievedProduct->attributes[0]->value);
247+
$this->assertEquals($uuid, (string) $retrievedProduct->attributes[0]->value);
248+
}
221249
}

tests/SerializerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Document\WithMappedType;
2020
use Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Entity\Foo;
2121
use Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Enum\InputMode;
22+
use Symfony\Component\Uid\UuidV4;
2223

2324
/**
2425
* @author Kévin Dunglas <[email protected]>
@@ -223,4 +224,21 @@ public function testSerializeEnum(): void
223224

224225
$this->assertEquals($value, $restoredValue);
225226
}
227+
228+
/**
229+
* @requires function \Symfony\Component\Serializer\Normalizer\UidNormalizer::normalize
230+
*/
231+
public function testSerializeUid(): void
232+
{
233+
$serializer = self::$kernel->getContainer()->get('dunglas_doctrine_json_odm.serializer');
234+
235+
$value = new UuidV4('4a7ba820-c806-4579-a907-193a2493863b');
236+
$data = $serializer->serialize($value, 'json');
237+
$decodeData = json_decode($data, true);
238+
$this->assertArrayHasKey('#type', $decodeData);
239+
$this->assertSame(UuidV4::class, $decodeData['#type']);
240+
$restoredValue = $serializer->deserialize($data, '', 'json');
241+
242+
$this->assertEquals($value, $restoredValue);
243+
}
226244
}

0 commit comments

Comments
 (0)