From 9f3fe6d3b1b0baf0fb098d7f0f879053096c4e70 Mon Sep 17 00:00:00 2001 From: soyuka Date: Thu, 16 Apr 2020 09:55:03 +0200 Subject: [PATCH] Fix ramsey uuid denormalization #3473 --- features/main/uuid.feature | 14 +++++++++ .../Serializer/UuidDenormalizer.php | 31 +++++++++++++++++++ .../Bundle/Resources/config/ramsey_uuid.xml | 4 +++ .../ApiPlatformExtensionTest.php | 1 + 4 files changed, 50 insertions(+) create mode 100644 src/Bridge/RamseyUuid/Serializer/UuidDenormalizer.php diff --git a/features/main/uuid.feature b/features/main/uuid.feature index ca5bc9ec9fc..9278cb15ee5 100644 --- a/features/main/uuid.feature +++ b/features/main/uuid.feature @@ -130,3 +130,17 @@ Feature: Using uuid identifier on resource Then the response status code should be 404 And the response should be in JSON And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" + + @!mongodb + @createSchema + Scenario: Create a resource identified by Ramsey\Uuid\Uuid + When I add "Content-Type" header equal to "application/ld+json" + And I send a "POST" request to "/ramsey_uuid_dummies" with body: + """ + { + "id": "41b29566-144b-11e6-a148-3e1d05defe78" + } + """ + Then the response status code should be 201 + And the response should be in JSON + And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" diff --git a/src/Bridge/RamseyUuid/Serializer/UuidDenormalizer.php b/src/Bridge/RamseyUuid/Serializer/UuidDenormalizer.php new file mode 100644 index 00000000000..c86fc47b5f1 --- /dev/null +++ b/src/Bridge/RamseyUuid/Serializer/UuidDenormalizer.php @@ -0,0 +1,31 @@ + + * + * 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\RamseyUuid\Serializer; + +use Ramsey\Uuid\Uuid; +use Ramsey\Uuid\UuidInterface; +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; + +class UuidDenormalizer implements DenormalizerInterface +{ + public function denormalize($data, string $type, string $format = null, array $context = []) + { + return Uuid::fromString($data); + } + + public function supportsDenormalization($data, string $type, string $format = null, array $context = []) + { + return \is_string($data) && is_a($type, UuidInterface::class, true); + } +} diff --git a/src/Bridge/Symfony/Bundle/Resources/config/ramsey_uuid.xml b/src/Bridge/Symfony/Bundle/Resources/config/ramsey_uuid.xml index a5d0626ab06..bbf81f232fa 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/ramsey_uuid.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/ramsey_uuid.xml @@ -8,5 +8,9 @@ + + + + diff --git a/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php b/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php index c378d7897d7..026f8ce9c7a 100644 --- a/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php +++ b/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php @@ -949,6 +949,7 @@ private function getPartialContainerBuilderProphecy($configuration = null) 'api_platform.serializer.group_filter', 'api_platform.serializer.normalizer.item', 'api_platform.serializer.property_filter', + 'api_platform.serializer.uuid_denormalizer', 'api_platform.serializer_locator', 'api_platform.subresource_data_provider', 'api_platform.subresource_operation_factory',