Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.6.5

* OpenAPI: Fix notice/warning for `response` without `content` in the `openapi_context` (#4210)
* Serializer: Convert internal error to HTTP 400 in Ramsey uuid denormalization from invalid body string (#4200)

## 2.6.4

Expand Down
58 changes: 54 additions & 4 deletions features/main/uuid.feature
Original file line number Diff line number Diff line change
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/ld+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/ld+json; charset=utf-8"
8 changes: 7 additions & 1 deletion src/Bridge/RamseyUuid/Serializer/UuidDenormalizer.php
Original file line number Diff line number Diff line change
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);
Comment thread
soyuka marked this conversation as resolved.
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
53 changes: 53 additions & 0 deletions tests/Bridge/RamseyUuid/Serializer/UuidDenormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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(): void
{
$uuid = Uuid::uuid4();
$normalizer = new UuidDenormalizer();
self::assertTrue($normalizer->supportsDenormalization($uuid->toString(), Uuid::class));
self::assertEquals($uuid, $normalizer->denormalize($uuid->toString(), Uuid::class));
}

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

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

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

public function testDoNotSupportNotString(): void
{
$uuid = Uuid::uuid4();
$normalizer = new UuidDenormalizer();
self::assertFalse($normalizer->supportsDenormalization($uuid, Uuid::class));
}
}
21 changes: 19 additions & 2 deletions tests/Fixtures/TestBundle/Entity/RamseyUuidDummy.php
Original file line number Diff line number Diff line change
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;
}
}