Skip to content

Add Symfony Uid support #3715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ jobs:
run: |
composer global require --prefer-dist --no-progress --no-suggest --ansi \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
composer global require --prefer-dist --no-progress --no-suggest --ansi \
composer global require --prefer-dist --no-progress --no-suggest --ansi \

symfony/flex
- name: Require Symfony Uid
run: composer require symfony/uid --dev --no-progress --ansi
- name: Update project dependencies
run: |
mkdir -p /tmp/api-platform/core/vendor
Expand Down Expand Up @@ -235,6 +237,9 @@ jobs:
composer remove --dev --no-progress --no-update --ansi \
doctrine/mongodb-odm \
doctrine/mongodb-odm-bundle \
- name: Require Symfony Uid
if: (!startsWith(matrix.php, '7.1'))
run: composer require symfony/uid --dev --no-progress --ansi
- name: Update project dependencies
run: |
mkdir -p /tmp/api-platform/core/vendor
Expand Down Expand Up @@ -377,6 +382,9 @@ jobs:
composer remove --dev --no-progress --no-update --ansi \
doctrine/mongodb-odm \
doctrine/mongodb-odm-bundle \
- name: Require Symfony Uid
if: (!startsWith(matrix.php, '7.1'))
run: composer require symfony/uid --dev --no-progress --ansi
- name: Update project dependencies
run: |
mkdir -p /tmp/api-platform/core/vendor
Expand Down Expand Up @@ -521,6 +529,8 @@ jobs:
run: |
composer global require --prefer-dist --no-progress --no-suggest --ansi \
symfony/flex
- name: Require Symfony Uid
run: composer require symfony/uid --dev --no-progress --ansi
- name: Update project dependencies
run: |
mkdir -p /tmp/api-platform/core/vendor
Expand Down Expand Up @@ -613,6 +623,8 @@ jobs:
mkdir -p /tmp/api-platform/core/vendor
ln -s /tmp/api-platform/core/vendor vendor
composer update --no-progress --no-suggest --prefer-stable --prefer-lowest --ansi
- name: Require Symfony Uid
run: composer require symfony/uid --dev --no-progress --ansi
- name: Install PHPUnit
run: vendor/bin/simple-phpunit --version
- name: Clear test app cache
Expand Down Expand Up @@ -753,6 +765,9 @@ jobs:
composer remove --dev --no-progress --no-update --ansi \
doctrine/mongodb-odm \
doctrine/mongodb-odm-bundle \
- name: Require Symfony Uid
if: (!startsWith(matrix.php, '7.1'))
run: composer require symfony/uid --dev --no-progress --ansi
- name: Update project dependencies
run: |
mkdir -p /tmp/api-platform/core/vendor
Expand Down Expand Up @@ -876,6 +891,8 @@ jobs:
run: |
composer global require --prefer-dist --no-progress --no-suggest --ansi \
symfony/flex
- name: Require Symfony Uid
run: composer require symfony/uid --dev --no-progress --ansi
- name: Update project dependencies
run: |
mkdir -p /tmp/api-platform/core/vendor
Expand Down Expand Up @@ -987,6 +1004,8 @@ jobs:
run: |
composer global require --prefer-dist --no-progress --no-suggest --ansi \
symfony/flex
- name: Require Symfony Uid
run: composer require symfony/uid --dev --no-progress --ansi
- name: Update project dependencies
run: |
mkdir -p /tmp/api-platform/core/vendor
Expand Down Expand Up @@ -1098,6 +1117,8 @@ jobs:
run: |
composer global require --prefer-dist --no-progress --no-suggest --ansi \
symfony/flex
- name: Require Symfony Uid
run: composer require symfony/uid --dev --no-progress --ansi
- name: Update project dependencies
run: |
mkdir -p /tmp/api-platform/core/vendor
Expand Down Expand Up @@ -1233,6 +1254,8 @@ jobs:
run: |
composer global require --prefer-dist --no-progress --no-suggest --ansi \
symfony/flex
- name: Require Symfony Uid
run: composer require symfony/uid --dev --no-progress --ansi
- name: Update project dependencies
run: |
mkdir -p /tmp/api-platform/core/vendor
Expand Down Expand Up @@ -1344,6 +1367,8 @@ jobs:
run: |
composer global require --prefer-dist --no-progress --no-suggest --ansi \
symfony/flex
- name: Require Symfony Uid
run: composer require symfony/uid --dev --no-progress --ansi
- name: Update project dependencies
run: |
mkdir -p /tmp/api-platform/core/vendor
Expand Down Expand Up @@ -1492,6 +1517,8 @@ jobs:
mkdir -p /tmp/api-platform/core/vendor
ln -s /tmp/api-platform/core/vendor vendor
composer update --no-progress --no-suggest --ansi
- name: Require Symfony Uid
run: composer require symfony/uid --dev --no-progress --ansi
- name: Install PHPUnit
run: vendor/bin/simple-phpunit --version
- name: Clear test app cache
Expand Down Expand Up @@ -1630,6 +1657,8 @@ jobs:
run: |
composer global require --prefer-dist --no-progress --no-suggest --ansi \
symfony/flex
- name: Require Symfony Uid
run: composer require symfony/uid --dev --no-progress --ansi
- name: Update project dependencies
run: |
mkdir -p /tmp/api-platform/core/vendor
Expand Down Expand Up @@ -1806,6 +1835,8 @@ jobs:
run: |
composer global require --prefer-dist --no-progress --no-suggest --ansi \
symfony/flex
- name: Require Symfony Uid
run: composer require symfony/uid --dev --no-progress --ansi
- name: Update project dependencies
run: |
mkdir -p /tmp/api-platform/core/vendor
Expand Down Expand Up @@ -1951,6 +1982,8 @@ jobs:
run: |
composer global require --prefer-dist --no-progress --no-suggest --ansi \
symfony/flex
- name: Require Symfony Uid
run: composer require symfony/uid --dev --no-progress --ansi
- name: Update project dependencies
run: |
mkdir -p /tmp/api-platform/core/vendor
Expand Down Expand Up @@ -2094,6 +2127,8 @@ jobs:
run: |
composer global require --prefer-dist --no-progress --no-suggest --ansi \
symfony/flex
- name: Require Symfony Uid
run: composer require symfony/uid --dev --no-progress --ansi
- name: Update project dependencies
run: |
mkdir -p /tmp/api-platform/core/vendor
Expand Down Expand Up @@ -2185,6 +2220,8 @@ jobs:
run: |
composer global require --prefer-dist --no-progress --no-suggest --ansi \
symfony/flex
- name: Require Symfony Uid
run: composer require symfony/uid --dev --no-progress --ansi
- name: Allow unstable project dependencies
run: |
jq '. + {"minimum-stability": "dev"}' composer.json | sponge composer.json
Expand Down Expand Up @@ -2293,6 +2330,8 @@ jobs:
run: |
composer global require --prefer-dist --no-progress --no-suggest --ansi \
symfony/flex
- name: Require Symfony Uid
run: composer require symfony/uid --dev --no-progress --ansi
- name: Allow unstable project dependencies
run: |
jq '. + {"minimum-stability": "dev"}' composer.json | sponge composer.json
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"symfony/expression-language": "To use authorization features.",
"symfony/security": "To use authorization features.",
"symfony/twig-bundle": "To use the Swagger UI integration.",
"symfony/uid": "To support Symfony UUID/ULID identifiers.",
"symfony/web-profiler-bundle": "To use the data collector.",
"webonyx/graphql-php": "To support GraphQL."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use Symfony\Component\Finder\Finder;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\Finder;

use Symfony\Component\HttpClient\HttpClientTrait;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Validator\Validator\ValidatorInterface;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

use Symfony\Component\Yaml\Yaml;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Yaml;


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

if (class_exists(AbstractUid::class)) {
$loader->load('symfony_uid.xml');
}

$container->setParameter('api_platform.enable_entrypoint', $config['enable_entrypoint']);
$container->setParameter('api_platform.enable_docs', $config['enable_docs']);
$container->setParameter('api_platform.title', $config['title']);
Expand Down
16 changes: 16 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/symfony_uid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="api_platform.identifier.symfony_ulid_normalizer" class="ApiPlatform\Core\Bridge\Symfony\Identifier\Normalizer\UlidNormalizer" public="false">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<tag name="api_platform.identifier.denormalizer" />
</service>

<service id="api_platform.identifier.symfony_uuid_normalizer" class="ApiPlatform\Core\Bridge\Symfony\Identifier\Normalizer\UuidNormalizer" public="false">
<tag name="api_platform.identifier.denormalizer" />
</service>
</services>
</container>
44 changes: 44 additions & 0 deletions src/Bridge/Symfony/Identifier/Normalizer/UlidNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Bridge\Symfony\Identifier\Normalizer;

use ApiPlatform\Core\Exception\InvalidIdentifierException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Uid\Ulid;

/**
* Denormalizes an ULID string to an instance of Symfony\Component\Uid\Ulid.
*/
final class UlidNormalizer implements DenormalizerInterface
{
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = [])
{
try {
return Ulid::fromString($data);
} catch (\InvalidArgumentException $e) {
throw new InvalidIdentifierException($e->getMessage(), $e->getCode(), $e);
}
}

/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return \is_string($data) && is_a($type, Ulid::class, true);
}
}
44 changes: 44 additions & 0 deletions src/Bridge/Symfony/Identifier/Normalizer/UuidNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Bridge\Symfony\Identifier\Normalizer;

use ApiPlatform\Core\Exception\InvalidIdentifierException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Uid\Uuid;

/**
* Denormalizes an UUID string to an instance of Symfony\Component\Uid\Uuid.
*/
final class UuidNormalizer implements DenormalizerInterface
{
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = [])
{
try {
return Uuid::fromString($data);
} catch (\InvalidArgumentException $e) {
throw new InvalidIdentifierException($e->getMessage(), $e->getCode(), $e);
}
}

/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return \is_string($data) && is_a($type, Uuid::class, true);
}
}
10 changes: 9 additions & 1 deletion src/JsonSchema/TypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use ApiPlatform\Core\Util\ResourceClassInfoTrait;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -106,12 +108,18 @@ private function getClassType(?string $className, string $format, ?bool $readabl
'format' => 'duration',
];
}
if (is_a($className, UuidInterface::class, true)) {
if (is_a($className, UuidInterface::class, true) || is_a($className, Uuid::class, true)) {
return [
'type' => 'string',
'format' => 'uuid',
];
}
if (is_a($className, Ulid::class, true)) {
return [
'type' => 'string',
'format' => 'ulid',
];
}

// Skip if $schema is null (filters only support basic types)
if (null === $schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Uid\AbstractUid;

/**
* @group resource-hog
Expand Down Expand Up @@ -986,6 +987,11 @@ private function getPartialContainerBuilderProphecy($configuration = null)
'api_platform.subresource_operation_factory.cached',
];

if (class_exists(AbstractUid::class)) {
$definitions[] = 'api_platform.identifier.symfony_ulid_normalizer';
$definitions[] = 'api_platform.identifier.symfony_uuid_normalizer';
}

foreach ($definitions as $definition) {
$containerBuilderProphecy->setDefinition($definition, Argument::type(Definition::class))->shouldBeCalled();
}
Expand Down
Loading