diff --git a/core/modules/jsonapi/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php b/core/modules/jsonapi/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php index 09041ef..d3aaab0 100644 --- a/core/modules/jsonapi/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php +++ b/core/modules/jsonapi/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php @@ -2,6 +2,7 @@ namespace Drupal\jsonapi\Normalizer; +use Drupal\Component\Utility\NestedArray; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\EntityReferenceFieldItemListInterface; use Drupal\jsonapi\Context\CurrentContext; @@ -124,10 +125,16 @@ public function normalize($object, $format = NULL, array $context = array()) { $normalized = $value_extractor->rasterizeValue(); $included = array_filter($value_extractor->rasterizeIncludes()); if (!empty($included)) { - $included = array_map(function ($value) { - return $value['data'] === FALSE ? ['meta' => $value['meta']] : $value['data']; - }, $included); - $normalized['included'] = $included; + $normalized['included'] = []; + foreach ($included as $included_item) { + if ($included_item['data'] === FALSE) { + unset($included_item['data']); + $normalized = NestedArray::mergeDeep($normalized, $included_item); + } + else { + $normalized['included'][] = $included_item['data']; + } + } } return $normalized; diff --git a/core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php b/core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php index 7feb0d0..9316bcf 100644 --- a/core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php +++ b/core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php @@ -221,12 +221,12 @@ public function testNormalize() { ], $normalized['data']['relationships']['uid']); $this->assertEquals( 'Access checks failed for entity user:' . $this->user->id() . '.', - $normalized['included'][0]['meta']['errors'][0]['detail'] + $normalized['meta']['errors'][0]['detail'] ); - $this->assertEquals(403, $normalized['included'][0]['meta']['errors'][0]['status']); - $this->assertEquals($this->term1->uuid(), $normalized['included'][1]['id']); - $this->assertEquals('taxonomy_term--tags', $normalized['included'][1]['type']); - $this->assertEquals($this->term1->label(), $normalized['included'][1]['attributes']['name']); + $this->assertEquals(403, $normalized['meta']['errors'][0]['status']); + $this->assertEquals($this->term1->uuid(), $normalized['included'][0]['id']); + $this->assertEquals('taxonomy_term--tags', $normalized['included'][0]['type']); + $this->assertEquals($this->term1->label(), $normalized['included'][0]['attributes']['name']); $this->assertTrue(!isset($normalized['included'][0]['attributes']['created'])); // Make sure that the cache tags for the includes and the requested entities // are bubbling as expected. @@ -310,8 +310,9 @@ public function testNormalizeUuid() { $this->assertStringMatchesFormat($this->node->uuid(), $normalized['data']['id']); $this->assertEquals($this->node->type->entity->uuid(), $normalized['data']['relationships']['type']['data']['id']); $this->assertEquals($this->user->uuid(), $normalized['data']['relationships']['uid']['data']['id']); - $this->assertTrue(empty($normalized['included'][0]['id'])); - $this->assertEquals($this->term1->uuid(), $normalized['included'][1]['id']); + $this->assertFalse(empty($normalized['included'][0]['id'])); + $this->assertFalse(empty($normalized['meta']['errors'])); + $this->assertEquals($this->term1->uuid(), $normalized['included'][0]['id']); // Make sure that the cache tags for the includes and the requested entities // are bubbling as expected. $this->assertSame(