symfony / ux

Symfony UX initiative: a JavaScript ecosystem for Symfony
https://ux.symfony.com/
MIT License
814 stars 298 forks source link

[LiveComponent] Uuid are not natively (de)hydrated #1846

Open Wait4Code opened 4 months ago

Wait4Code commented 4 months ago

I'm not sure if it is covered by the "Future H)" item of https://github.com/symfony/ux/issues/102 issue but I've noticed that LiveComponent crash when trying to (de)hydrate Uuid.

More precisely, in LiveComponentHydrator#dehydrateObjectValue() method, line 507, (new PropertyInfoExtractor([new ReflectionExtractor()]))->getProperties($classType) returns null which makes the foreach loop crash.

This is quite annoying when using Uuid in a DTO class that mixed entities and simple DTOs. I could use useSerializerForHydration option on LineProp but then I would need to manage serialization in my entity which can be a burden. I will try to get through this by creating an Hydration Extension for Uuid but I think this would be a nice addition if it could be managed by Live Component natively.

smnandre commented 3 months ago

Is this UUID writable ? If not, maybe you could not make it a live prop ?

Wait4Code commented 3 months ago

Well, as it is a dehydration of the Uuid object (from symfony/uid package), I don't think I would change anything. In fact, my UuidHydrationExtension workaround works pretty well :


use Symfony\Component\Uid\AbstractUid;
use Symfony\UX\LiveComponent\Hydration\HydrationExtensionInterface;

class UuidHydrationExtension implements HydrationExtensionInterface
{

    public function supports(string $className): bool
    {
        return is_a($className, AbstractUid::class, true);
    }

    public function hydrate(mixed $value, string $className): ?object
    {
        return new $className($value);
    }

    public function dehydrate(object $object): mixed
    {
        return $object->toRfc4122();
    }
}

I am not sure which package should have the responsibility to handle this compatibility problem between symfony/ux-live-component and symfony/uid packages ? Probably symfony/ux-live-component but for some reason it is not obvious to me :/

axel37 commented 1 month ago

I just encountered the same issue.

I recently moved from php8.0 to 8.3 and did a composer update. Before then, the issue did not appear. I'm on Symfony 5.4.41 and symfony/ux-live-component 2.18.1.

Indeed, the error occurs in LiveComponentHydrator, line 504 :

 foreach ((new PropertyInfoExtractor([new ReflectionExtractor()]))->getProperties($classType) as $property) {
           // ...
 }

Which throws :

Warning: foreach() argument must be of type array|object, null given

Here's a screenshot of the variables shown in the debugger :

a list of variables including $classType, $value and an Exception

smnandre commented 1 month ago

You only updated LiveComponent ? Do you know what version you used ?

axel37 commented 1 month ago

You only updated LiveComponent ?

No, but after downgrading to the previous version, the error went away.

Do you know what version you used ?

I went back to 2.7.1.

smnandre commented 1 month ago

2.7.1 is sadly a pretty old version so many many things may have changed since (LiveComponent is only 'stable' since 2.14 earlier this year)

The only that i can think of immediately is that symfony/serializer is not required anymore.... so you may have more success if you install it manually ?

I also can see you're using the DoctrineEntityHydration here, so maybe something to digg there ?