{"record":{"id":"753a44eb089cc199","repo":"symfony/http-kernel","slug":"the-symfony-serializer-component-is-required-to-use-the-s","errorCode":null,"errorMessage":"The \"symfony/serializer\" component is required to use the \"#[%s]\" attribute. Try running \"composer require symfony/serializer\".","messagePattern":"The \"symfony/serializer\" component is required to use the \"#\\[(.+?)\\]\" attribute\\. Try running \"composer require symfony/serializer\"\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"EventListener/SerializeControllerResultAttributeListener.php","lineNumber":46,"sourceCode":"final class SerializeControllerResultAttributeListener implements EventSubscriberInterface\n{\n    public function __construct(private readonly ?SerializerInterface $serializer)\n    {\n    }\n\n    /**\n     * @param ControllerAttributeEvent<Serialize, ViewEvent> $event\n     */\n    public function onView(ControllerAttributeEvent $event): void\n    {\n        $kernelEvent = $event->kernelEvent;\n\n        if (!$kernelEvent instanceof ViewEvent) {\n            return;\n        }\n\n        if (!$this->serializer) {\n            throw new \\LogicException(\\sprintf('The \"symfony/serializer\" component is required to use the \"#[%s]\" attribute. Try running \"composer require symfony/serializer\".', Serialize::class));\n        }\n\n        $request = $kernelEvent->getRequest();\n        $controllerResult = $kernelEvent->getControllerResult();\n        $format = $request->getRequestFormat('json');\n\n        try {\n            $data = $this->serializer->serialize($controllerResult, $format, $event->attribute->context);\n        } catch (UnsupportedFormatException $exception) {\n            throw new UnsupportedMediaTypeHttpException(\\sprintf('Unsupported format \"%s\".', $format), $exception->getPrevious());\n        }\n\n        $headers = $this->mergeHeaders($event->attribute, $request, $format);\n        $response = new Response($data, $event->attribute->code, $headers);\n\n        $kernelEvent->setResponse($response);\n    }\n","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/symfony/http-kernel/blob/aa3a39d7286a62cdfea98f0e69c651a3da6e36cf/EventListener/SerializeControllerResultAttributeListener.php#L28-L64","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Run \"composer require symfony/serializer\".","Enable/configure the serializer in the framework (framework.serializer or installing symfony/serializer-pack).","Remove the #[Serialize] attribute and handle rendering manually if serialization is not wanted."],"exampleFix":"// before\n$listener = new SerializeControllerResultAttributeListener(null);\n// after\ncomposer require symfony/serializer\n$listener = new SerializeControllerResultAttributeListener($serializer);","handlingStrategy":"validation","validationCode":"if (!interface_exists(\\Symfony\\Component\\Serializer\\SerializerInterface::class)) { throw new \\RuntimeException('symfony/serializer required for #[Serialize]'); }","typeGuard":null,"tryCatchPattern":"try { $kernel->handle($request); } catch (\\LogicException $e) { if (str_contains($e->getMessage(), 'symfony/serializer')) { /* install component or remove #[Serialize] */ } throw $e; }","preventionTips":["composer require symfony/serializer before using #[Serialize]","Keep the serializer-pack in require for API projects","Smoke-test controllers using #[Serialize] in CI with production-like deps"],"tags":["serializer","missing-dependency","attributes","symfony"],"backgroundTag":"missing-optional-dependency","analyzedSha":"aa3a39d7286a62cdfea98f0e69c651a3da6e36cf","analyzedAt":"2026-09-13T18:03:36.509Z","contentChangedAt":"2026-09-13T18:03:36.509Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}