Skip to content

[LiveComponent] Make LiveComponentHydrator work without Serializer #1341

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

Merged
merged 1 commit into from
Dec 17, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
tagged_iterator(LiveComponentBundle::HYDRATION_EXTENSION_TAG),
new Reference('property_accessor'),
new Reference('ux.live_component.metadata_factory'),
new Reference('serializer'),
new Reference('serializer', ContainerInterface::NULL_ON_INVALID_REFERENCE),
'%kernel.secret%',
])
;
Expand Down
20 changes: 16 additions & 4 deletions src/LiveComponent/src/LiveComponentHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public function __construct(
private iterable $hydrationExtensions,
private PropertyAccessorInterface $propertyAccessor,
private LiveComponentMetadataFactory $liveComponentMetadataFactory,
private NormalizerInterface|DenormalizerInterface $normalizer,
private string $secret
private NormalizerInterface|DenormalizerInterface|null $serializer,
private string $secret,
) {
}

Expand Down Expand Up @@ -357,8 +357,14 @@ private function dehydrateValue(mixed $value, LivePropMetadata $propMetadata, ob
if (!interface_exists(NormalizerInterface::class)) {
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" has "useSerializerForHydration: true", but the Serializer component is not installed. Try running "composer require symfony/serializer".', $propMetadata->getName(), $parentObject::class));
}
if (null === $this->serializer) {
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" has "useSerializerForHydration: true", but no serializer has been set.', $propMetadata->getName(), $parentObject::class));
}
if (!$this->serializer instanceof NormalizerInterface) {
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" has "useSerializerForHydration: true", but the given serializer does not implement NormalizerInterface.', $propMetadata->getName(), $parentObject::class));
}
Comment on lines +360 to +365
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need those two errors? How those two errors could happen?

Copy link
Member Author

Choose a reason for hiding this comment

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

The null test is required (it's not because symfony/serializer is installed that it is configured in Symfony).

The instance test is necessary because the constructor argument is NormalizerInterface|DenormalizerInterface .. so one could be normalizer without beeing denormalizer :|


return $this->normalizer->normalize($value, 'json', $propMetadata->serializationContext());
return $this->serializer->normalize($value, 'json', $propMetadata->serializationContext());
}

if (\is_bool($value) || null === $value || is_numeric($value) || \is_string($value)) {
Expand Down Expand Up @@ -438,8 +444,14 @@ private function hydrateValue(mixed $value, LivePropMetadata $propMetadata, obje
if (!interface_exists(DenormalizerInterface::class)) {
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" has "useSerializerForHydration: true", but the Serializer component is not installed. Try running "composer require symfony/serializer".', $propMetadata->getName(), $parentObject::class));
}
if (null === $this->serializer) {
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" has "useSerializerForHydration: true", but no serializer has been set.', $propMetadata->getName(), $parentObject::class));
}
if (!$this->serializer instanceof DenormalizerInterface) {
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" has "useSerializerForHydration: true", but the given serializer does not implement DenormalizerInterface.', $propMetadata->getName(), $parentObject::class));
}

return $this->normalizer->denormalize($value, $propMetadata->getType(), 'json', $propMetadata->serializationContext());
return $this->serializer->denormalize($value, $propMetadata->getType(), 'json', $propMetadata->serializationContext());
}

if ($propMetadata->collectionValueType() && Type::BUILTIN_TYPE_OBJECT === $propMetadata->collectionValueType()->getBuiltinType()) {
Expand Down