symfony/http-kernel · error · LogicException

The "symfony/serializer" component is required to use the "#

Error message

The "symfony/serializer" component is required to use the "#[%s]" attribute. Try running "composer require symfony/serializer".

What it means

SerializeControllerResultAttributeListener handles the #[Serialize] attribute by serializing the controller result, which requires a serializer service. If none was injected, it throws a LogicException instructing the developer to install symfony/serializer.

Solutions

  1. Run "composer require symfony/serializer".
  2. Enable/configure the serializer in the framework (framework.serializer or installing symfony/serializer-pack).
  3. Remove the #[Serialize] attribute and handle rendering manually if serialization is not wanted.

Example fix

// before
$listener = new SerializeControllerResultAttributeListener(null);
// after
composer require symfony/serializer
$listener = new SerializeControllerResultAttributeListener($serializer);
Defensive patterns

Strategy: validation

Validate before calling

if (!interface_exists(\Symfony\Component\Serializer\SerializerInterface::class)) { throw new \RuntimeException('symfony/serializer required for #[Serialize]'); }

Try / catch

try { $kernel->handle($request); } catch (\LogicException $e) { if (str_contains($e->getMessage(), 'symfony/serializer')) { /* install component or remove #[Serialize] */ } throw $e; }

Prevention

When it happens

Trigger: A controller action annotated with #[Serialize] returning a result on a kernel.view event while the listener was constructed with $serializer = null (component not installed/wired).

Common situations: Using #[Serialize] in a project without symfony/serializer installed; the serializer alias not autowired because the framework.serializer config is absent; testing the listener without providing a serializer.

Understand the failure class

Background: "X is not installed. Please install it with pip install Y": missing optional dependency errors — ImportError/ValueError raised when a library's optional extra was never installed — this error's family across 22 libraries.

Related errors


AI-assisted analysis of symfony/http-kernel@aa3a39d728 (2026-09-13). Data as JSON: /api/errors/753a44eb089cc199. Report an issue: GitHub.

Appendix: source

Thrown at EventListener/SerializeControllerResultAttributeListener.php:46

final class SerializeControllerResultAttributeListener implements EventSubscriberInterface
{
    public function __construct(private readonly ?SerializerInterface $serializer)
    {
    }

    /**
     * @param ControllerAttributeEvent<Serialize, ViewEvent> $event
     */
    public function onView(ControllerAttributeEvent $event): void
    {
        $kernelEvent = $event->kernelEvent;

        if (!$kernelEvent instanceof ViewEvent) {
            return;
        }

        if (!$this->serializer) {
            throw new \LogicException(\sprintf('The "symfony/serializer" component is required to use the "#[%s]" attribute. Try running "composer require symfony/serializer".', Serialize::class));
        }

        $request = $kernelEvent->getRequest();
        $controllerResult = $kernelEvent->getControllerResult();
        $format = $request->getRequestFormat('json');

        try {
            $data = $this->serializer->serialize($controllerResult, $format, $event->attribute->context);
        } catch (UnsupportedFormatException $exception) {
            throw new UnsupportedMediaTypeHttpException(\sprintf('Unsupported format "%s".', $format), $exception->getPrevious());
        }

        $headers = $this->mergeHeaders($event->attribute, $request, $format);
        $response = new Response($data, $event->attribute->code, $headers);

        $kernelEvent->setResponse($response);
    }

View on GitHub (pinned to aa3a39d728)