Skip to content

Commit

Permalink
Fix ramsey uuid denormalization api-platform#3473
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Apr 16, 2020
1 parent bda66b7 commit cacef7f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
15 changes: 15 additions & 0 deletions features/main/uuid.feature
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,18 @@ 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 print last JSON response
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"
31 changes: 31 additions & 0 deletions src/Bridge/RamseyUuid/Serializer/UuidDenormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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\RamseyUuid\Serializer;

use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;

class UuidDenormalizer implements ContextAwareDenormalizerInterface
{
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);
}
}
4 changes: 4 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/ramsey_uuid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
<service id="api_platform.identifier.uuid_normalizer" class="ApiPlatform\Core\Bridge\RamseyUuid\Identifier\Normalizer\UuidNormalizer" public="false">
<tag name="api_platform.identifier.denormalizer" />
</service>

<service id="api_platform.serializer.uuid_denormalizer" class="ApiPlatform\Core\Bridge\RamseyUuid\Serializer\UuidDenormalizer" public="false">
<tag name="serializer.normalizer" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit cacef7f

Please sign in to comment.