Skip to content

Commit 7ba124a

Browse files
committed
Add Symfony Uid support
1 parent 1e62c99 commit 7ba124a

File tree

10 files changed

+288
-1
lines changed

10 files changed

+288
-1
lines changed

.github/workflows/ci.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ jobs:
113113
run: |
114114
composer global require --prefer-dist --no-progress --no-suggest --ansi \
115115
symfony/flex
116+
- name: Require Symfony Uid
117+
run: composer require symfony/uid --dev --no-progress --ansi
116118
- name: Update project dependencies
117119
run: |
118120
mkdir -p /tmp/api-platform/core/vendor
@@ -235,6 +237,9 @@ jobs:
235237
composer remove --dev --no-progress --no-update --ansi \
236238
doctrine/mongodb-odm \
237239
doctrine/mongodb-odm-bundle \
240+
- name: Require Symfony Uid
241+
if: (!startsWith(matrix.php, '7.1'))
242+
run: composer require symfony/uid --dev --no-progress --ansi
238243
- name: Update project dependencies
239244
run: |
240245
mkdir -p /tmp/api-platform/core/vendor
@@ -377,6 +382,9 @@ jobs:
377382
composer remove --dev --no-progress --no-update --ansi \
378383
doctrine/mongodb-odm \
379384
doctrine/mongodb-odm-bundle \
385+
- name: Require Symfony Uid
386+
if: (!startsWith(matrix.php, '7.1'))
387+
run: composer require symfony/uid --dev --no-progress --ansi
380388
- name: Update project dependencies
381389
run: |
382390
mkdir -p /tmp/api-platform/core/vendor
@@ -521,6 +529,8 @@ jobs:
521529
run: |
522530
composer global require --prefer-dist --no-progress --no-suggest --ansi \
523531
symfony/flex
532+
- name: Require Symfony Uid
533+
run: composer require symfony/uid --dev --no-progress --ansi
524534
- name: Update project dependencies
525535
run: |
526536
mkdir -p /tmp/api-platform/core/vendor
@@ -613,6 +623,8 @@ jobs:
613623
mkdir -p /tmp/api-platform/core/vendor
614624
ln -s /tmp/api-platform/core/vendor vendor
615625
composer update --no-progress --no-suggest --prefer-stable --prefer-lowest --ansi
626+
- name: Require Symfony Uid
627+
run: composer require symfony/uid --dev --no-progress --ansi
616628
- name: Install PHPUnit
617629
run: vendor/bin/simple-phpunit --version
618630
- name: Clear test app cache
@@ -753,6 +765,9 @@ jobs:
753765
composer remove --dev --no-progress --no-update --ansi \
754766
doctrine/mongodb-odm \
755767
doctrine/mongodb-odm-bundle \
768+
- name: Require Symfony Uid
769+
if: (!startsWith(matrix.php, '7.1'))
770+
run: composer require symfony/uid --dev --no-progress --ansi
756771
- name: Update project dependencies
757772
run: |
758773
mkdir -p /tmp/api-platform/core/vendor
@@ -876,6 +891,8 @@ jobs:
876891
run: |
877892
composer global require --prefer-dist --no-progress --no-suggest --ansi \
878893
symfony/flex
894+
- name: Require Symfony Uid
895+
run: composer require symfony/uid --dev --no-progress --ansi
879896
- name: Update project dependencies
880897
run: |
881898
mkdir -p /tmp/api-platform/core/vendor
@@ -987,6 +1004,8 @@ jobs:
9871004
run: |
9881005
composer global require --prefer-dist --no-progress --no-suggest --ansi \
9891006
symfony/flex
1007+
- name: Require Symfony Uid
1008+
run: composer require symfony/uid --dev --no-progress --ansi
9901009
- name: Update project dependencies
9911010
run: |
9921011
mkdir -p /tmp/api-platform/core/vendor
@@ -1098,6 +1117,8 @@ jobs:
10981117
run: |
10991118
composer global require --prefer-dist --no-progress --no-suggest --ansi \
11001119
symfony/flex
1120+
- name: Require Symfony Uid
1121+
run: composer require symfony/uid --dev --no-progress --ansi
11011122
- name: Update project dependencies
11021123
run: |
11031124
mkdir -p /tmp/api-platform/core/vendor
@@ -1233,6 +1254,8 @@ jobs:
12331254
run: |
12341255
composer global require --prefer-dist --no-progress --no-suggest --ansi \
12351256
symfony/flex
1257+
- name: Require Symfony Uid
1258+
run: composer require symfony/uid --dev --no-progress --ansi
12361259
- name: Update project dependencies
12371260
run: |
12381261
mkdir -p /tmp/api-platform/core/vendor
@@ -1344,6 +1367,8 @@ jobs:
13441367
run: |
13451368
composer global require --prefer-dist --no-progress --no-suggest --ansi \
13461369
symfony/flex
1370+
- name: Require Symfony Uid
1371+
run: composer require symfony/uid --dev --no-progress --ansi
13471372
- name: Update project dependencies
13481373
run: |
13491374
mkdir -p /tmp/api-platform/core/vendor
@@ -1492,6 +1517,8 @@ jobs:
14921517
mkdir -p /tmp/api-platform/core/vendor
14931518
ln -s /tmp/api-platform/core/vendor vendor
14941519
composer update --no-progress --no-suggest --ansi
1520+
- name: Require Symfony Uid
1521+
run: composer require symfony/uid --dev --no-progress --ansi
14951522
- name: Install PHPUnit
14961523
run: vendor/bin/simple-phpunit --version
14971524
- name: Clear test app cache
@@ -1630,6 +1657,8 @@ jobs:
16301657
run: |
16311658
composer global require --prefer-dist --no-progress --no-suggest --ansi \
16321659
symfony/flex
1660+
- name: Require Symfony Uid
1661+
run: composer require symfony/uid --dev --no-progress --ansi
16331662
- name: Update project dependencies
16341663
run: |
16351664
mkdir -p /tmp/api-platform/core/vendor
@@ -1806,6 +1835,8 @@ jobs:
18061835
run: |
18071836
composer global require --prefer-dist --no-progress --no-suggest --ansi \
18081837
symfony/flex
1838+
- name: Require Symfony Uid
1839+
run: composer require symfony/uid --dev --no-progress --ansi
18091840
- name: Update project dependencies
18101841
run: |
18111842
mkdir -p /tmp/api-platform/core/vendor
@@ -1951,6 +1982,8 @@ jobs:
19511982
run: |
19521983
composer global require --prefer-dist --no-progress --no-suggest --ansi \
19531984
symfony/flex
1985+
- name: Require Symfony Uid
1986+
run: composer require symfony/uid --dev --no-progress --ansi
19541987
- name: Update project dependencies
19551988
run: |
19561989
mkdir -p /tmp/api-platform/core/vendor
@@ -2094,6 +2127,8 @@ jobs:
20942127
run: |
20952128
composer global require --prefer-dist --no-progress --no-suggest --ansi \
20962129
symfony/flex
2130+
- name: Require Symfony Uid
2131+
run: composer require symfony/uid --dev --no-progress --ansi
20972132
- name: Update project dependencies
20982133
run: |
20992134
mkdir -p /tmp/api-platform/core/vendor
@@ -2185,6 +2220,8 @@ jobs:
21852220
run: |
21862221
composer global require --prefer-dist --no-progress --no-suggest --ansi \
21872222
symfony/flex
2223+
- name: Require Symfony Uid
2224+
run: composer require symfony/uid --dev --no-progress --ansi
21882225
- name: Allow unstable project dependencies
21892226
run: |
21902227
jq '. + {"minimum-stability": "dev"}' composer.json | sponge composer.json
@@ -2293,6 +2330,8 @@ jobs:
22932330
run: |
22942331
composer global require --prefer-dist --no-progress --no-suggest --ansi \
22952332
symfony/flex
2333+
- name: Require Symfony Uid
2334+
run: composer require symfony/uid --dev --no-progress --ansi
22962335
- name: Allow unstable project dependencies
22972336
run: |
22982337
jq '. + {"minimum-stability": "dev"}' composer.json | sponge composer.json

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
"symfony/expression-language": "To use authorization features.",
105105
"symfony/security": "To use authorization features.",
106106
"symfony/twig-bundle": "To use the Swagger UI integration.",
107+
"symfony/uid": "To support Symfony UUID/ULID identifiers.",
107108
"symfony/web-profiler-bundle": "To use the data collector.",
108109
"webonyx/graphql-php": "To support GraphQL."
109110
},

src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
use Symfony\Component\Finder\Finder;
5454
use Symfony\Component\HttpClient\HttpClientTrait;
5555
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
56+
use Symfony\Component\Uid\AbstractUid;
5657
use Symfony\Component\Validator\Validator\ValidatorInterface;
5758
use Symfony\Component\Yaml\Yaml;
5859

@@ -167,6 +168,10 @@ private function registerCommonConfiguration(ContainerBuilder $container, array
167168
$loader->load('ramsey_uuid.xml');
168169
}
169170

171+
if (class_exists(AbstractUid::class)) {
172+
$loader->load('symfony_uid.xml');
173+
}
174+
170175
$container->setParameter('api_platform.enable_entrypoint', $config['enable_entrypoint']);
171176
$container->setParameter('api_platform.enable_docs', $config['enable_docs']);
172177
$container->setParameter('api_platform.title', $config['title']);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<services>
8+
<service id="api_platform.identifier.symfony_ulid_normalizer" class="ApiPlatform\Core\Bridge\Symfony\Identifier\Normalizer\UlidNormalizer" public="false">
9+
<tag name="api_platform.identifier.denormalizer" />
10+
</service>
11+
12+
<service id="api_platform.identifier.symfony_uuid_normalizer" class="ApiPlatform\Core\Bridge\Symfony\Identifier\Normalizer\UuidNormalizer" public="false">
13+
<tag name="api_platform.identifier.denormalizer" />
14+
</service>
15+
</services>
16+
</container>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Bridge\Symfony\Identifier\Normalizer;
15+
16+
use ApiPlatform\Core\Exception\InvalidIdentifierException;
17+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
18+
use Symfony\Component\Uid\Ulid;
19+
20+
/**
21+
* Denormalizes an ULID string to an instance of Symfony\Component\Uid\Ulid.
22+
*/
23+
final class UlidNormalizer implements DenormalizerInterface
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function denormalize($data, $class, $format = null, array $context = [])
29+
{
30+
try {
31+
return Ulid::fromString($data);
32+
} catch (\InvalidArgumentException $e) {
33+
throw new InvalidIdentifierException($e->getMessage(), $e->getCode(), $e);
34+
}
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function supportsDenormalization($data, $type, $format = null)
41+
{
42+
return \is_string($data) && is_a($type, Ulid::class, true);
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Bridge\Symfony\Identifier\Normalizer;
15+
16+
use ApiPlatform\Core\Exception\InvalidIdentifierException;
17+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
18+
use Symfony\Component\Uid\Uuid;
19+
20+
/**
21+
* Denormalizes an UUID string to an instance of Symfony\Component\Uid\Uuid.
22+
*/
23+
final class UuidNormalizer implements DenormalizerInterface
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function denormalize($data, $class, $format = null, array $context = [])
29+
{
30+
try {
31+
return Uuid::fromString($data);
32+
} catch (\InvalidArgumentException $e) {
33+
throw new InvalidIdentifierException($e->getMessage(), $e->getCode(), $e);
34+
}
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function supportsDenormalization($data, $type, $format = null)
41+
{
42+
return \is_string($data) && is_a($type, Uuid::class, true);
43+
}
44+
}

src/JsonSchema/TypeFactory.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use ApiPlatform\Core\Util\ResourceClassInfoTrait;
1818
use Ramsey\Uuid\UuidInterface;
1919
use Symfony\Component\PropertyInfo\Type;
20+
use Symfony\Component\Uid\Ulid;
21+
use Symfony\Component\Uid\Uuid;
2022

2123
/**
2224
* {@inheritdoc}
@@ -106,12 +108,18 @@ private function getClassType(?string $className, string $format, ?bool $readabl
106108
'format' => 'duration',
107109
];
108110
}
109-
if (is_a($className, UuidInterface::class, true)) {
111+
if (is_a($className, UuidInterface::class, true) || is_a($className, Uuid::class, true)) {
110112
return [
111113
'type' => 'string',
112114
'format' => 'uuid',
113115
];
114116
}
117+
if (is_a($className, Ulid::class, true)) {
118+
return [
119+
'type' => 'string',
120+
'format' => 'ulid',
121+
];
122+
}
115123

116124
// Skip if $schema is null (filters only support basic types)
117125
if (null === $schema) {

tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
use Symfony\Component\DependencyInjection\Reference;
114114
use Symfony\Component\HttpFoundation\Response;
115115
use Symfony\Component\Serializer\Exception\ExceptionInterface;
116+
use Symfony\Component\Uid\AbstractUid;
116117

117118
/**
118119
* @group resource-hog
@@ -986,6 +987,11 @@ private function getPartialContainerBuilderProphecy($configuration = null)
986987
'api_platform.subresource_operation_factory.cached',
987988
];
988989

990+
if (class_exists(AbstractUid::class)) {
991+
$definitions[] = 'api_platform.identifier.symfony_ulid_normalizer';
992+
$definitions[] = 'api_platform.identifier.symfony_uuid_normalizer';
993+
}
994+
989995
foreach ($definitions as $definition) {
990996
$containerBuilderProphecy->setDefinition($definition, Argument::type(Definition::class))->shouldBeCalled();
991997
}

0 commit comments

Comments
 (0)