ux icon indicating copy to clipboard operation
ux copied to clipboard

[LiveComponent] Anonymous component rendering problem ?

Open anatoly-pashin-axon21 opened this issue 4 months ago • 3 comments

Hi. I'm not able to present a minimal reproducer, but we have a problem with an anonymous component rendering.

The problem is happening only for the event handling render (\Symfony\UX\LiveComponent\Attribute\PreReRender -> \Symfony\UX\LiveComponent\ComponentToolsTrait::emit -> POST https://redacted/en/_components/sylius_shop:product:summary/eventNameRedacted)

Image

I have reviewed the code and I see an issue with the anonymous components metadata handling.

\Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory::getMetadata:

public function getMetadata(string $name): LiveComponentMetadata
{
    // some not related code removed

    $componentMetadata = $this->componentFactory->metadataFor($name);

    // here the getClass() is called:
    $reflectionClass = new \ReflectionClass($componentMetadata->getClass());

    // some not related code removed
}

\Symfony\UX\TwigComponent\ComponentFactory::metadataFor:

public function metadataFor(string $name): ComponentMetadata
{
    // some not related code removed

    if ($template = $this->componentTemplateFinder->findAnonymousComponentTemplate($name)) {
        $this->config[$name] = [
            'key' => $name,
            'template' => $template,
        ];

        // here we return the metadata with only two keys in the config (key, template):
        return new ComponentMetadata($this->config[$name]);
    }

As you can see, it tries to read "class", but there is no class defined for the anonymous components. There are only key and template defined for them. I believe the code is missing for the AnonymousComponent special case handling.

We use the latest versions of symfony/ux-live-component symfony/ux-twig-component libs.

anatoly-pashin-axon21 avatar Aug 18 '25 12:08 anatoly-pashin-axon21

Anonymous component cannot be LiveComponent so not sure what is going on here 🤔

smnandre avatar Aug 20 '25 19:08 smnandre

You can see from the trace that there happens a twig component creation (ux-twig/ComponentRenderer). It dispatches an event, that is handled by ux-live/InterceptChildComponentRender that tries to get metadata.class. So it's just an error in the code.

I was able to fix this is our project by adding a missing check for the AnonymousComponent in the method I mentioned earlier.

anatoly-pashin-axon21 avatar Aug 21 '25 06:08 anatoly-pashin-axon21

@anatoly-pashin-axon21

I am stating - again - that I don't get how and why a LiveComponentMetadataFactory would try to create metadata for a non-live component. Not saying it does not happen. But this is not a scenario I understand right now.

Without a simple reproducer (not just an exception screenshot, where we can see lots of other things coming in play).. we cannot investigate nor find a solution if there is a bug we missed (this is very possible).

I'm sure you can understand that without observing your problem in simple and isolated conditions, we cannot apply the code change you suggest.

smnandre avatar Aug 21 '25 21:08 smnandre