Function getRelated() in EntityResource.php not working when related entity field is empty becasue $field_list->entity is null. Not sure if this patch is the correct way to handle it, but it works & can be a start.

TypeError: Argument 1 passed to Drupal\jsonapi\Controller\EntityResource::getIndividual() must implement interface Drupal\Core\Entity\EntityInterface, null given, called in /var/www/html/web/modules/contrib/jsonapi/src/Controller/EntityResource.php on line 344 in Drupal\jsonapi\Controller\EntityResource->getIndividual() (line 121 of modules/contrib/jsonapi/src/Controller/EntityResource.php).

Comments

harrrrrrr created an issue. See original summary.

harrrrrrr’s picture

StatusFileSize
new689 bytes
wim leers’s picture

Issue tags: +Needs tests

What would be super helpful, is a failing test which reproduces this problem :)

harrrrrrr’s picture

StatusFileSize
new608 bytes

This patch provides a better solution by returning an empty data [] instead of an error.

If someone wants to add a test, any relationship call with an empty non multi value reference field will cause a failure without this patch.

fi: /jsonapi/node/article/[UUID]/field_image

gabesullice’s picture

Title: getRelated() not working when related entity field is empty » [PP-1] getRelated() not working when related entity field is empty
Priority: Normal » Major
Related issues: +#2945093: Comprehensive JSON API integration test coverage phase 3: test JSON API-specific use cases: related/relationship routes, includes and sparse field sets

I've confirmed this bug exists locally. I'll write some tests for this as part of #2945093: Comprehensive JSON API integration test coverage phase 3: test JSON API-specific use cases: related/relationship routes, includes and sparse field sets.

I can't believe this has existed for so long without being noticed. Great catch @harrrrrrr!

wim leers’s picture

👍 — thank you, @harrrrrrr! (Also: great nickname :D)

wim leers’s picture

Status: Active » Postponed
gabesullice’s picture

Assigned: Unassigned » gabesullice

Assigning to myself so I don't forget about this.

gabesullice’s picture

This again came up while working on tests in #2945093: Comprehensive JSON API integration test coverage phase 3: test JSON API-specific use cases: related/relationship routes, includes and sparse field sets

Which led me to #2940339: Port reference field support for non-empty entity reference fields not pointing to an entity from #2543726

Which clearly is related to this issue given this line of the IS: "Function getRelated() in EntityResource.php not working when related entity field is empty becasue $field_list->entity is null."

I'm not sure if one is a duplicate of the other yet, so leaving both open. This one is for "related" routes and the other is presumably focused on "individual" routes.

The good new is that test coverage does exist now, albeit indirectly :) I'll test this patch against it shortly.

wim leers’s picture

👍 — they indeed seem related and potentially even duplicates!

wim leers’s picture

Title: [PP-1] getRelated() not working when related entity field is empty » getRelated() not working when related entity field is empty
Status: Postponed » Closed (duplicate)

I was mistaken in #11: that is a different problem: that's about a relationship being omitted from data.relationships completely when it doesn't refer to another resource.

And interestingly, the bug reported here was fixed on February 14 in #2939722: Do not provide routes for internal resource types. Perhaps unsurprisingly, the changes suggested in the patch in #4 no longer apply because that code has been removed altogether!

That makes this effectively a duplicate that has already been fixed! :)

wim leers’s picture

I found this through git bisect and … deceiving git btw :D

I found that 8.x-1.7 worked as described in the IS. But HEAD didn't. Telling git bisect that HEAD was good and 1.7 was bad wouldn't work though (because that leads git to conclude that things are good in HEAD, hence no bisecting necessary). So, just flipping good and bad allowed me to use git bisect after all :)

wim leers’s picture

Assigned: Unassigned » wim leers
Status: Closed (duplicate) » Needs work

@gabesullice kindly pointed out that I was severely mistaken, because I was testing http://d8/jsonapi/node/article/c32bef09-21f5-4b63-a51f-29d0734edc3c/relationships/field_image instead of http://d8/jsonapi/node/article/c32bef09-21f5-4b63-a51f-29d0734edc3c/field_image…

wim leers’s picture

Assigned: wim leers » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new1.67 KB
new2.33 KB

To not have this be blocked on #2945093: Comprehensive JSON API integration test coverage phase 3: test JSON API-specific use cases: related/relationship routes, includes and sparse field sets, here's test coverage added to JsonApiFunctionalTest.

The test-only patch is also the interdiff.

wim leers’s picture

#2940339: Port reference field support for non-empty entity reference fields not pointing to an entity from #2543726 is for a different problem: the fact that core REST says {target_id: null}, but JSON API just says {data: []}. Removing that from the related issues.

The last submitted patch, 15: 2930217-15-test-only-FAIL.patch, failed testing. View results

gabesullice’s picture

Status: Needs review » Reviewed & tested by the community

The last submitted patch, 2: 2930217_fix_getrelated_2.patch, failed testing. View results

wim leers’s picture

e0ipso’s picture

Priority: Major » Normal
Status: Reviewed & tested by the community » Fixed

This looks good to me. Thanks everyone! Merging.

  • e0ipso committed e6ef003 on 8.x-1.x authored by Wim Leers
    Issue #2930217 by Wim Leers, harrrrrrr, gabesullice, e0ipso: getRelated...
wim leers’s picture

🎉

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.