From daee3ca32e619ff79b8413b43dd2bac7b1fe97f0 Mon Sep 17 00:00:00 2001
From: William Hearn <sylus1984@gmail.com>
Date: Wed, 18 Mar 2020 18:51:06 -0400
Subject: [PATCH] Issue #2759397: Add recursion protection to
 EntityReferenceItem normalizers

---
 .../src/Normalizer/EntityReferenceItemNormalizer.php   | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php b/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php
index c2d65bfdbc..eae130ba55 100644
--- a/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php
+++ b/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php
@@ -80,8 +80,14 @@ public function normalize($field_item, $format = NULL, array $context = []) {
     unset($context['langcode']);
     $context['included_fields'] = ['uuid'];
 
-    // Normalize the target entity.
-    $embedded = $this->serializer->normalize($target_entity, $format, $context);
+    // Normalize the target entity unless it recurses onto itself
+    if ($field_item->getEntity()->getEntityType() == $target_entity->getEntityType() &&
+      $field_item->getEntity()->id() == $target_entity->id()) {
+      return parent::normalize($field_item, $format, $context);
+    }
+    else {
+      $embedded = $this->serializer->normalize($target_entity, $format, $context);
+    }
     // @todo https://www.drupal.org/project/drupal/issues/3110815 $embedded will
     //   be NULL if the target entity does not exist. Use null coalescence
     //   operator to preserve behaviour in PHP 7.4.
-- 
2.21.0 (Apple Git-122)

