IncludeResolver assumes entity reference fields will always have a value

Created on 30 August 2019, over 5 years ago
Updated 23 January 2024, 11 months ago

Problem/Motivation

Using core 8.7.6, if you make a JSON:API resource request which has an include and an entity does not have that included entity reference field populated, the following results:

Warning:

Warning: array_flip(): Can only flip STRING and INTEGER values! in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 265 of core/lib/Drupal/Core/Entity/EntityStorageBase.php).
Drupal\Core\Entity\EntityStorageBase->loadMultiple(Array) (Line: 147)
Drupal\jsonapi\IncludeResolver->resolveIncludeTree(Array, Object) (Line: 74)

The code stems from IncludeResolver.php:
$references[$target_type][] = $field_item->get($field_item::mainPropertyName())->getValue();

Proposed resolution

Check that the field returned a value before using it.

Remaining tasks

Write a patch.

πŸ› Bug report
Status

Needs work

Version

9.5

Component
JSON APIΒ  β†’

Last updated 1 day ago

Created by

πŸ‡ΊπŸ‡ΈUnited States mstef

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024