API Platform version(s) affected: 2.6.3, 2.6.4
- Symfony 4.4
ramsey/uuid 3.9.3
Description
(see title) when using the UuidInterface type declaration for automatic denormalization
How to reproduce
On PHP 7.4
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Ramsey\Uuid\UuidInterface;
/** @ApiResource */
class Foo
{
public int $id = 0;
public ?UuidInterface $uuid = null;
public ?\DateTimeInterface $datetime = null;
}
Request: POST /foos with body (Content-Type: application/ld+json):
Expected: 400 Bad Request
Actual: 500 Internal Server Error
(message: "Invalid UUID string: foo")
Possible Solution
In ApiPlatform\Core\Bridge\RamseyUuid\Serializer\UuidDenormalizer, wrap return Uuid::fromString($data); with a try-catch (like in ApiPlatform\Core\Bridge\RamseyUuid\Identifier\Normalizer\UuidNormalizer) and convert the Ramsey\Uuid\Exception\InvalidUuidStringException to an appropriate exception (a subclass of Symfony\Component\Serializer\Exception\ExceptionInterface)?
Additional Context
For this body:
I get a 400 Bad Request, as expected
(message: "DateTimeImmutable::__construct(): Failed to parse time string (foo) at position 0 (f): The timezone could not be found in the database")
API Platform version(s) affected: 2.6.3, 2.6.4
ramsey/uuid3.9.3Description
(see title) when using the
UuidInterfacetype declaration for automatic denormalizationHow to reproduce
On PHP 7.4
Request:
POST /fooswith body (Content-Type: application/ld+json):{"uuid": "foo"}Expected: 400 Bad Request
Actual: 500 Internal Server Error
(message: "Invalid UUID string: foo")
Possible Solution
In
ApiPlatform\Core\Bridge\RamseyUuid\Serializer\UuidDenormalizer, wrapreturn Uuid::fromString($data);with a try-catch (like inApiPlatform\Core\Bridge\RamseyUuid\Identifier\Normalizer\UuidNormalizer) and convert theRamsey\Uuid\Exception\InvalidUuidStringExceptionto an appropriate exception (a subclass ofSymfony\Component\Serializer\Exception\ExceptionInterface)?Additional Context
For this body:
{"datetime": "foo"}I get a 400 Bad Request, as expected
(message: "DateTimeImmutable::__construct(): Failed to parse time string (foo) at position 0 (f): The timezone could not be found in the database")