Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception when using Ramsey/Uuid ^4.0 #3473

Closed
BeyerJC opened this issue Mar 31, 2020 · 5 comments
Closed

Exception when using Ramsey/Uuid ^4.0 #3473

BeyerJC opened this issue Mar 31, 2020 · 5 comments

Comments

@BeyerJC
Copy link
Contributor

BeyerJC commented Mar 31, 2020

API Platform version(s) affected: x.y.z

Description
After updating to Ramsey/Uuid ^4.0 i get an exception when querying an item with identifier. For example api/books/3fa85f64-5717-4562-b3fc-2c963f66afa6

The exception:
Argument 1 passed to Ramsey\Uuid\Uuid::fromString() must be of the type string, object given, called in ..\vendor\api-platform\core\src\Bridge\RamseyUuid\Identifier\Normalizer\UuidNormalizer.php on line 35

$data contains an Uuid object instead of an string , the Uuid is my identifier.

It works fine with Ramsey/Uuid ^3.9 !

Possible Solution
Add a check if $data is a string or an instance of UuidInterface in the UuidNormalizer

@soyuka
Copy link
Member

soyuka commented Mar 31, 2020

You may not need this normalizer after all, could you trying removing it to see if everything works as expected?

@BeyerJC
Copy link
Contributor Author

BeyerJC commented Mar 31, 2020

You may not need this normalizer after all, could you trying removing it to see if everything works as expected?

All my GET-Item Operations are working without the Normalizer. My POST test is failing now

Uncaught PHP Exception Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException: "Cannot create an instance of "Ramsey\Uuid\Uuid" from serialized data because its constructor requires parameter "fields" to be present

@soyuka
Copy link
Member

soyuka commented Apr 15, 2020

My guess is that the denormalizer can't use the Uuid class as-is. Would you be able to add a denormalizer for the Uuid type (see https://symfony.com/doc/current/serializer/custom_normalizer.html#creating-a-new-normalizer) to see if it fixes it.

@BeyerJC
Copy link
Contributor Author

BeyerJC commented Apr 16, 2020

It works if i adjust the UuidNormalizer with the check i suggested in "Possible solution".
I changed


to
return ($data instanceof UuidInterface) ? $data : Uuid::fromString($data);

That fixes the problem and should not impact older versions of ramsey.

@soyuka Im not sure what denormalizer i should add. The existing one already supports the UuidInterface. Can you give me a hint ?

@soyuka
Copy link
Member

soyuka commented Apr 16, 2020

There is a difference between the Identifier normalizer and the denormalizer I suggested. In fact what bother's me is that we already have:

    public function supportsDenormalization($data, $type, $format = null)
    {
        return is_a($type, UuidInterface::class, true);
    }

The code should not go through the denormalize if this isn't true. I'll try some things and come back to you.

soyuka added a commit to soyuka/core that referenced this issue Apr 16, 2020
soyuka added a commit to soyuka/core that referenced this issue Apr 16, 2020
soyuka added a commit to soyuka/core that referenced this issue Apr 16, 2020
dunglas pushed a commit to soyuka/core that referenced this issue Apr 21, 2020
soyuka added a commit to soyuka/core that referenced this issue Apr 21, 2020
soyuka added a commit to soyuka/core that referenced this issue Apr 21, 2020
soyuka added a commit to soyuka/core that referenced this issue Apr 21, 2020
dunglas added a commit that referenced this issue Apr 21, 2020
@soyuka soyuka closed this as completed Apr 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants