{
  "id": "php/symfony-serializer-circular-reference",
  "signature": "Symfony\\Component\\Serializer\\Exception\\CircularReferenceException: A circular reference has been detected (configured limit: 1) in /var/www/app/src/Controller/ApiController.php:56",
  "signature_zh": "Symfony\\Component\\Serializer\\Exception\\CircularReferenceException：检测到循环引用（配置限制：1），位于/var/www/app/src/Controller/ApiController.php:56",
  "regex": "/CircularReferenceException: A circular reference has been detected \\(configured limit: \\d+\\)/",
  "domain": "php",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "The Symfony Serializer component detected a circular reference in an object graph (e.g., a parent-child relationship where both sides reference each other) and the configured circular reference limit was exceeded.",
  "root_cause_type": "generic",
  "root_cause_zh": "Symfony序列化组件在对象图中检测到循环引用（例如，父子关系中双方互相引用），且超出了配置的循环引用限制。",
  "versions": [
    {
      "version": "Symfony 5.4",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Symfony 6.4",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Symfony 7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "PHP 8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "PHP 8.1",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "This only postpones the issue; the serializer will still attempt to traverse the circular graph, potentially causing memory exhaustion or infinite loops.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Removing getters breaks the application logic and may cause other features to fail; it is a drastic and unnecessary workaround.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "This flag only changes error handling behavior; it does not prevent the circular reference from being detected and thrown by the serializer.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use the @Ignore annotation or the 'ignored_attributes' option to skip specific properties during serialization. Example in PHP: $context = ['ignored_attributes' => ['child', 'parent']]; $serializer->serialize($object, 'json', $context);",
      "success_rate": 0.85,
      "how": "Use the @Ignore annotation or the 'ignored_attributes' option to skip specific properties during serialization. Example in PHP: $context = ['ignored_attributes' => ['child', 'parent']]; $serializer->serialize($object, 'json', $context);",
      "condition": "",
      "sources": []
    },
    {
      "action": "Implement a custom circular reference handler: $encoder = new JsonEncoder(); $normalizer = new ObjectNormalizer(); $normalizer->setCircularReferenceHandler(function ($object) { return $object->getId(); }); $serializer = new Serializer([$normalizer], [$encoder]);",
      "success_rate": 0.9,
      "how": "Implement a custom circular reference handler: $encoder = new JsonEncoder(); $normalizer = new ObjectNormalizer(); $normalizer->setCircularReferenceHandler(function ($object) { return $object->getId(); }); $serializer = new Serializer([$normalizer], [$encoder]);",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use the MaxDepth annotation to limit serialization depth: #[MaxDepth(1)] on the property that causes the cycle. Then enable max depth handling: $context = [AbstractNormalizer::ENABLE_MAX_DEPTH => true];",
      "success_rate": 0.8,
      "how": "Use the MaxDepth annotation to limit serialization depth: #[MaxDepth(1)] on the property that causes the cycle. Then enable max depth handling: $context = [AbstractNormalizer::ENABLE_MAX_DEPTH => true];",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Use the @Ignore annotation or the 'ignored_attributes' option to skip specific properties during serialization. Example in PHP: $context = ['ignored_attributes' => ['child', 'parent']]; $serializer->serialize($object, 'json', $context);",
    "Implement a custom circular reference handler: $encoder = new JsonEncoder(); $normalizer = new ObjectNormalizer(); $normalizer->setCircularReferenceHandler(function ($object) { return $object->getId(); }); $serializer = new Serializer([$normalizer], [$encoder]);",
    "Use the MaxDepth annotation to limit serialization depth: #[MaxDepth(1)] on the property that causes the cycle. Then enable max depth handling: $context = [AbstractNormalizer::ENABLE_MAX_DEPTH => true];"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://symfony.com/doc/current/components/serializer.html#handling-circular-references",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.88,
  "resolvable": "true",
  "first_seen": "2024-04-22",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}