Skip to content

Conversation

squrious
Copy link
Contributor

@squrious squrious commented Mar 5, 2024

Q A
Bug fix? yes
New feature? no
Issues N/A
License MIT

Hello!

I encountered issues when trying to hydrate a LiveProp as a collection of objects, using the Symfony Serializer.

Given phpdocumentor/reflection-dockblock is installed in the project, and I have the following component:

#[AsLiveComponent]
class MyComponent
{
    /**
     * @var MyDto[] 
     */
    #[LiveProp(useSerializerForHydration: true)]
    public array $prop = [];
}

Then I should be able to hydrate $prop as a collection of MyDto.

However, the LiveComponentHydrator doesn't consider the collection type when useSerializerForHydration is enabled, and tries to deserialize to array type, throwing the following exception:

Symfony\Component\Serializer\Exception\NotNormalizableValueException : Could not denormalize object of type "array", no supporting normalizer found.

So here is a proposal to fix the issue.

Cheers!

@carsonbot carsonbot added the Status: Needs Review Needs to be reviewed label Mar 5, 2024
@squrious squrious force-pushed the live-component/use-collection-type-serializer branch from 05959a5 to ec70907 Compare March 5, 2024 13:59
@@ -259,7 +259,9 @@ public function hydrateValue(mixed $value, LivePropMetadata $propMetadata, objec
throw new \LogicException(sprintf('The "%s::%s" object should be hydrated with the Serializer, but no type could be guessed.', $parentObject::class, $propMetadata->getName()));
}

return $this->serializer->denormalize($value, $propMetadata->getType(), 'json', $propMetadata->serializationContext());
$type = $propMetadata->collectionValueType() ? $propMetadata->collectionValueType()->getClassName().'[]' : $propMetadata->getType();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to also check for Type::BUILTIN_TYPE_OBJECT === $propMetadata->collectionValueType()->getBuiltinType() like we do on line 267? Basically, what if this is typed as string[]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes absolutely, I missed those cases. Next commit fixes that, and also ensures the built-in collection type is scalar (if used). Otherwise the "not guessable type" exception is thrown

@carsonbot carsonbot added Status: Needs Work Additional work is needed Status: Needs Review Needs to be reviewed and removed Status: Needs Review Needs to be reviewed Status: Needs Work Additional work is needed labels Mar 5, 2024
@carsonbot carsonbot added Status: Needs Work Additional work is needed and removed Status: Needs Review Needs to be reviewed labels Mar 5, 2024
@squrious squrious force-pushed the live-component/use-collection-type-serializer branch from 9a5d6cc to 881c643 Compare March 5, 2024 17:12
@carsonbot carsonbot added Status: Needs Review Needs to be reviewed and removed Status: Needs Work Additional work is needed labels Mar 5, 2024
@weaverryan weaverryan force-pushed the live-component/use-collection-type-serializer branch from 881c643 to 7a8bc4f Compare March 5, 2024 17:46
@weaverryan
Copy link
Member

Thanks Nicolas!

@weaverryan weaverryan merged commit 8e3cabe into symfony:2.x Mar 5, 2024
@smnandre
Copy link
Member

smnandre commented Mar 9, 2024

Oh i missed this one ! So cool 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Needs Review Needs to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants