Skip to content

Commit

Permalink
Merge 3383345 into a773ce6
Browse files Browse the repository at this point in the history
  • Loading branch information
guilliamxavier committed Apr 12, 2021
2 parents a773ce6 + 3383345 commit ad802d0
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 13 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Expand Up @@ -4,17 +4,17 @@

* Serializer: Fix denormalization of basic property-types in XML and CSV (#3191)
* Serializer: Fix denormalization of collection with one element in XML (#4154)
* Serializer: Convert internal error to HTTP 400 in Ramsey uuid denormalization from invalid body string (#4200)
* JSON Schema: Manage Sequentially and AtLeastOneOf constraints when generating property metadata (#4139 and #4147)
* Doctrine: Fix purging HTTP cache for unreadable relations (#3441)
* Doctrine: Revert #3774 support for binary UUID in search filter (#4134)
* Doctrine: Fix order filter when using embedded and nulls comparison (#4151)
* GraphQL: Partial pagination support (#3223)
* GraphQL: Manage `pagination_use_output_walkers` and `pagination_fetch_join_collection` for operations (#3311)
* Swagger UI: Remove Google fonts (#4112)
* Doctrine: Revert #3774 support for binary UUID in search filter (#4134)
* Do not override Vary headers already set in the Response
* Do not override Vary headers already set in the Response (#4146)
* GraphQL: Make sure the order of order filters is preserved if nested resources are used (#4171)
* Validation: properties regex pattern is now correctly anchored (#4176)
* Validation: properties regex pattern is now correctly anchored (#4176 and #4198)

## 2.6.3

Expand Down
58 changes: 54 additions & 4 deletions features/main/uuid.feature
Expand Up @@ -111,8 +111,7 @@ Feature: Using uuid identifier on resource
@createSchema
Scenario: Retrieve a resource identified by Ramsey\Uuid\Uuid
Given there is a ramsey identified resource with uuid "41B29566-144B-11E6-A148-3E1D05DEFE78"
When I add "Content-Type" header equal to "application/ld+json"
And I send a "GET" request to "/ramsey_uuid_dummies/41B29566-144B-11E6-A148-3E1D05DEFE78"
When I send a "GET" request to "/ramsey_uuid_dummies/41B29566-144B-11E6-A148-3E1D05DEFE78"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
Expand All @@ -125,8 +124,7 @@ Feature: Using uuid identifier on resource

@!mongodb
Scenario: Retrieve a resource identified by a bad Ramsey\Uuid\Uuid
When I add "Content-Type" header equal to "application/ld+json"
And I send a "GET" request to "/ramsey_uuid_dummies/41B29566-144B-E1D05DEFE78"
When I send a "GET" request to "/ramsey_uuid_dummies/41B29566-144B-E1D05DEFE78"
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"
Expand All @@ -144,3 +142,55 @@ Feature: Using uuid identifier on resource
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"

@!mongodb
Scenario: Create a resource with a Ramsey\Uuid\Uuid non-id field
When I add "Content-Type" header equal to "application/ld+json"
And I send a "POST" request to "/ramsey_uuid_dummies" with body:
"""
{
"other": "51b29566-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"

@!mongodb
Scenario: Update a resource with a Ramsey\Uuid\Uuid non-id field
When I add "Content-Type" header equal to "application/ld+json"
And I send a "PUT" request to "/ramsey_uuid_dummies/41b29566-144b-11e6-a148-3e1d05defe78" with body:
"""
{
"other": "61b29566-144b-11e6-a148-3e1d05defe78"
}
"""
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"

@!mongodb
Scenario: Create a resource identified by a bad 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-e1d05defe78"
}
"""
Then the response status code should be 400
And the response should be in JSON
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"

@!mongodb
Scenario: Update a resource with a bad Ramsey\Uuid\Uuid non-id field
When I add "Content-Type" header equal to "application/ld+json"
And I send a "PUT" request to "/ramsey_uuid_dummies/41b29566-144b-11e6-a148-3e1d05defe78" with body:
"""
{
"other": "61b29566-144b-e1d05defe78"
}
"""
Then the response status code should be 400
And the response should be in JSON
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
8 changes: 7 additions & 1 deletion src/Bridge/RamseyUuid/Serializer/UuidDenormalizer.php
Expand Up @@ -13,15 +13,21 @@

namespace ApiPlatform\Core\Bridge\RamseyUuid\Serializer;

use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

final class UuidDenormalizer implements DenormalizerInterface
{
public function denormalize($data, $type, $format = null, array $context = [])
{
return Uuid::fromString($data);
try {
return Uuid::fromString($data);
} catch (InvalidUuidStringException $e) {
throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e);
}
}

public function supportsDenormalization($data, $type, $format = null)
Expand Down
4 changes: 2 additions & 2 deletions tests/Behat/DoctrineContext.php
Expand Up @@ -167,6 +167,7 @@
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

/**
Expand Down Expand Up @@ -1365,8 +1366,7 @@ public function thereAreDummyWithDifferentGraphQlSerializationGroupsObjects(int
*/
public function thereIsARamseyIdentifiedResource(string $uuid)
{
$dummy = new RamseyUuidDummy();
$dummy->setId($uuid);
$dummy = new RamseyUuidDummy(Uuid::fromString($uuid));

$this->manager->persist($dummy);
$this->manager->flush();
Expand Down
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Bridge\RamseyUuid\Normalizer;
namespace ApiPlatform\Core\Tests\Bridge\RamseyUuid\Identifier\Normalizer;

use ApiPlatform\Core\Bridge\RamseyUuid\Identifier\Normalizer\UuidNormalizer;
use ApiPlatform\Core\Exception\InvalidIdentifierException;
Expand Down
54 changes: 54 additions & 0 deletions tests/Bridge/RamseyUuid/Serializer/UuidDenormalizerTest.php
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Tests\Bridge\RamseyUuid\Serializer;

use ApiPlatform\Core\Bridge\RamseyUuid\Serializer\UuidDenormalizer;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;

class UuidDenormalizerTest extends TestCase
{
public function testDenormalizeUuid()
{
$uuid = Uuid::uuid4();
$normalizer = new UuidDenormalizer();
$this->assertTrue($normalizer->supportsDenormalization($uuid->toString(), Uuid::class));
$this->assertEquals($uuid, $normalizer->denormalize($uuid->toString(), Uuid::class));
}

public function testNoSupportDenormalizeUuid()
{
$uuid = 'notanuuid';
$normalizer = new UuidDenormalizer();
$this->assertFalse($normalizer->supportsDenormalization($uuid, ''));
}

public function testFailDenormalizeUuid()
{
$this->expectException(NotNormalizableValueException::class);

$uuid = 'notanuuid';
$normalizer = new UuidDenormalizer();
$this->assertTrue($normalizer->supportsDenormalization($uuid, Uuid::class));
$normalizer->denormalize($uuid, Uuid::class);
}

public function testDoNotSupportNotString()
{
$uuid = Uuid::uuid4();
$normalizer = new UuidDenormalizer();
$this->assertFalse($normalizer->supportsDenormalization($uuid, Uuid::class));
}
}
21 changes: 19 additions & 2 deletions tests/Fixtures/TestBundle/Entity/RamseyUuidDummy.php
Expand Up @@ -32,13 +32,30 @@ class RamseyUuidDummy
*/
private $id;

/**
* @var \Ramsey\Uuid\UuidInterface|null
*
* @ORM\Column(type="uuid", nullable=true)
*/
private $other;

public function __construct(?UuidInterface $id = null)
{
$this->id = $id ?? Uuid::uuid4();
}

public function getId(): UuidInterface
{
return $this->id;
}

public function setId(string $uuid)
public function getOther(): ?UuidInterface
{
return $this->other;
}

public function setOther(UuidInterface $other)
{
$this->id = Uuid::fromString($uuid);
$this->other = $other;
}
}

0 comments on commit ad802d0

Please sign in to comment.