Problem/Motivation

JSON:API responds with a 500 error on the resource list when a referenced entity is deleted without removing the reference from the resource.

Steps to reproduce with a default Drupal 8.6.10, JSON:API 2.3 and JSON:API Extras 3.4 installation (latest):
- Have a content type, e.g. Article with a taxonomy reference field, e.g. field_tag referencing Tag taxonomy terms
- Overwrite JSON:API settings and change the alias of field_tag to something else, e.g. field_tag_alias
- Create an Article and choose a Tag
- Delete the Tag taxonomy term that is referenced, but do not remove it from the Article
- Visit /jsonapi/node/article and get a 500 error with the following message

Error: Call to a member function getTypeName() on boolean in Drupal\jsonapi\JsonApiResource\ResourceIdentifier->__construct() (line 79 of modules/contrib/jsonapi/src/JsonApiResource/ResourceIdentifier.php).

Drupal\jsonapi\JsonApiResource\ResourceIdentifier->__construct(, 'missing', Array) (Line: 450)
Drupal\jsonapi\JsonApiResource\ResourceIdentifier::getVirtualOrMissingResourceIdentifier(Object) (Line: 282)
Drupal\jsonapi\JsonApiResource\ResourceIdentifier::toResourceIdentifier(Object) (Line: 315)
Drupal\jsonapi\JsonApiResource\ResourceIdentifier::toResourceIdentifiers(Object) (Line: 52)
Drupal\jsonapi\Normalizer\EntityReferenceFieldNormalizer->normalize(Object, 'api_json', Array) (Line: 143)
Symfony\Component\Serializer\Serializer->normalize(Object, 'api_json', Array) (Line: 58)
Drupal\jsonapi\Serializer\Serializer->normalize(Object, 'api_json', Array)
call_user_func_array(Array, Array) (Line: 67)
Drupal\jsonapi_extras\SerializerDecorator->relay('normalize', Array) (Line: 102)
Drupal\jsonapi_extras\SerializerDecorator->normalize(Object, 'api_json', Array) (Line: 95)
Drupal\jsonapi\Normalizer\ResourceObjectNormalizer->serializeField(Object, Array, 'api_json') (Line: 55)
Drupal\jsonapi\Normalizer\ResourceObjectNormalizer->normalize(Object, 'api_json', Array) (Line: 37)
Drupal\jsonapi_extras\Normalizer\ResourceObjectNormalizer->normalize(Object, 'api_json', Array) (Line: 143)
Symfony\Component\Serializer\Serializer->normalize(Object, 'api_json', Array) (Line: 61)
Drupal\jsonapi\Serializer\Serializer->normalize(Object, 'api_json', Array) (Line: 253)
Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer->Drupal\jsonapi\Normalizer\{closure}(Object)
array_map(Object, Array) (Line: 254)
Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer->normalizeEntityCollection(Object, 'api_json', Array) (Line: 182)
Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer->normalize(Object, 'api_json', Array) (Line: 143)
Symfony\Component\Serializer\Serializer->normalize(Object, 'api_json', Array) (Line: 58)
Drupal\jsonapi\Serializer\Serializer->normalize(Object, 'api_json', Array)
call_user_func_array(Array, Array) (Line: 67)
Drupal\jsonapi_extras\SerializerDecorator->relay('normalize', Array) (Line: 102)
Drupal\jsonapi_extras\SerializerDecorator->normalize(Object, 'api_json', Array) (Line: 115)
Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber->renderResponseBody(Object, Object, Object, 'api_json') (Line: 80)
Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber->onResponse(Object, 'kernel.response', Object)
call_user_func(Array, Object, 'kernel.response', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.response', Object) (Line: 191)
Symfony\Component\HttpKernel\HttpKernel->filterResponse(Object, Object, 1) (Line: 173)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 41)
Drupal\jsonapi\StackMiddleware\FormatSetter->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Proposed resolution

I think it has something to do with \Drupal\jsonapi\ResourceType\ResourceType::getRelatableResourceTypesByField() not being overriden in \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType.

Remaining tasks

Discussion, Patch, etc.

User interface changes

None expected.

API changes

None expected.

Data model changes

None expected.

Release notes snippet

None expected.

Comments

vtcore created an issue. See original summary.

ndobromirov’s picture

+1 here.
I've managed to reproduce this.

ndobromirov’s picture

Issue summary: View changes
e0ipso’s picture

Issue tags: +Needs tests

This looks like a good candidate to have an automated test for.

Raman Starshykh’s picture

Issue tags: +epam-contrib-2019.03
Andrei_tab’s picture

Hi there. This issue is more related to the cores entity_refefence
Here is the issue that will solve the same problems.
Entity reference isEmpty() fix.

linichalexey’s picture

Issue tags: -epam-contrib-2019.03
br0ken’s picture

Version: 8.x-3.4 » 8.x-3.x-dev
Assigned: Unassigned » br0ken
Status: Active » Needs review
Issue tags: -Needs tests
StatusFileSize
new1.51 KB

Status: Needs review » Needs work

The last submitted patch, 8: 3035544-8.patch, failed testing. View results

br0ken’s picture

Status: Needs work » Needs review
StatusFileSize
new3.17 KB
new2.01 KB

Status: Needs review » Needs work

The last submitted patch, 10: 3035544-10.patch, failed testing. View results

br0ken’s picture

Status: Needs work » Needs review

Setting to NR since tests were passed (have no idea why it's red while there are only deprecation warnings).

ndobromirov’s picture

Maybe CI builds do not approve of deprecated APIs being used?

br0ken’s picture

I think this is because of a custom drupalci.yml I've added to the patch. The important thing here is that tests were passed with the patch applied. The bug is in jsonapi but connected to resources renaming so tests could be written only using this project. So this issue will be automatically resolved altogether with #3034786: ResourceIdentifier::getVirtualOrMissingResourceIdentifier() ignores field aliases; causes $relatable_resource_types field to be empty and results in an error.

wim leers’s picture

Project: JSON:API Extras » Drupal core
Version: 8.x-3.x-dev » 8.9.x-dev
Component: Code » jsonapi.module
Assigned: br0ken » Unassigned
Status: Needs review » Closed (duplicate)
Issue tags: +API-First Initiative

#3034786: ResourceIdentifier::getVirtualOrMissingResourceIdentifier() ignores field aliases; causes $relatable_resource_types field to be empty and results in an error landed in Drupal 9.0 and 8.9 some time ago and earlier today it also landed in 8.8.

So: as soon as you update to Drupal 8.8 this will be gone :)