Skip to content

Commit 5a7c2e4

Browse files
authored
Merge pull request #3511 from soyuka/fix/ramsey-uuid-denormalization-2
Fix ramsey uuid denormalization #3473
2 parents fa9d8ca + b522a52 commit 5a7c2e4

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

features/main/uuid.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,17 @@ Feature: Using uuid identifier on resource
130130
Then the response status code should be 404
131131
And the response should be in JSON
132132
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
133+
134+
@!mongodb
135+
@createSchema
136+
Scenario: Create a resource identified by Ramsey\Uuid\Uuid
137+
When I add "Content-Type" header equal to "application/ld+json"
138+
And I send a "POST" request to "/ramsey_uuid_dummies" with body:
139+
"""
140+
{
141+
"id": "41b29566-144b-11e6-a148-3e1d05defe78"
142+
}
143+
"""
144+
Then the response status code should be 201
145+
And the response should be in JSON
146+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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\RamseyUuid\Serializer;
15+
16+
use Ramsey\Uuid\Uuid;
17+
use Ramsey\Uuid\UuidInterface;
18+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
19+
20+
final class UuidDenormalizer implements DenormalizerInterface
21+
{
22+
public function denormalize($data, $type, $format = null, array $context = [])
23+
{
24+
return Uuid::fromString($data);
25+
}
26+
27+
public function supportsDenormalization($data, $type, $format = null)
28+
{
29+
return \is_string($data) && is_a($type, UuidInterface::class, true);
30+
}
31+
}

src/Bridge/Symfony/Bundle/Resources/config/ramsey_uuid.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@
88
<service id="api_platform.identifier.uuid_normalizer" class="ApiPlatform\Core\Bridge\RamseyUuid\Identifier\Normalizer\UuidNormalizer" public="false">
99
<tag name="api_platform.identifier.denormalizer" />
1010
</service>
11+
12+
<service id="api_platform.serializer.uuid_denormalizer" class="ApiPlatform\Core\Bridge\RamseyUuid\Serializer\UuidDenormalizer" public="false">
13+
<tag name="serializer.normalizer" />
14+
</service>
1115
</services>
1216
</container>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,7 @@ private function getPartialContainerBuilderProphecy($configuration = null)
949949
'api_platform.serializer.group_filter',
950950
'api_platform.serializer.normalizer.item',
951951
'api_platform.serializer.property_filter',
952+
'api_platform.serializer.uuid_denormalizer',
952953
'api_platform.serializer_locator',
953954
'api_platform.subresource_data_provider',
954955
'api_platform.subresource_operation_factory',

0 commit comments

Comments
 (0)