Problem/Motivation
Discovered while working on #2930028: Comprehensive JSON API integration test coverage phase 1: for every entity type, individual resources only.
Sending a bodyless POST or PATCH request results in
1) Drupal\Tests\jsonapi\Functional\NodeTest::testPatchIndividual
Exception: TypeError: Argument 2 passed to Drupal\jsonapi\Controller\EntityResource::patchIndividual() must implement interface Drupal\Core\Entity\EntityInterface, instance of Symfony\Component\HttpFoundation\Request given
Drupal\jsonapi\Controller\EntityResource->patchIndividual()() (Line: 242)
…
2) ) Drupal\Tests\jsonapi\Functional\NodeTest::testPostIndividual
Exception: TypeError: Argument 1 passed to Drupal\jsonapi\Controller\EntityResource::createIndividual() must implement interface Drupal\Core\Entity\EntityInterface, instance of Symfony\Component\HttpFoundation\Request given
Drupal\jsonapi\Controller\EntityResource->createIndividual()() (Line: 193)
Proposed resolution
Fix \Drupal\jsonapi\Controller\RequestHandler::handle().
Remaining tasks
TBD
User interface changes
None.
API changes
None.
Data model changes
None.
Comments
Comment #2
wim leersNow that #2883086: [PP-1] Port RequestHandler + ResourceResponseSubscriber improvements from REST module to JSON API is done, it's much easier to move this forward!
And in fact, the two places where this issue was cited and test assertions were commented both covered too much ground. Both included test assertions that #2934149: [>=8.5] JSON API routes not specifying _content_type_format route requirement, resulting in bad DX will uncomment. So, selectively uncommenting and updating the
@todos.Note that core REST does this check in
\Drupal\rest\Plugin\rest\resource\EntityResource::patch()and\Drupal\rest\Plugin\rest\resource\EntityResource::post(). But JSON API can be smarter about it: do it in\Drupal\jsonapi\Controller\RequestHandler::handle()!Comment #3
wim leersComment #4
e0ipsohttps://httpstatuses.com/422
If the unserialized entity is NULL because of a missing body, 400 is OK. If it's NULL because the body is not valid JSON or not valid application/vnd.api+json, then 415. If it's NULL because it contains valid application/vnd.api+json but an entity could not be built with it, we need a 422.
Comment #5
wim leersYes, and those cases are handled by
\Drupal\jsonapi\Controller\RequestHandler::deserialize()since #2883086: [PP-1] Port RequestHandler + ResourceResponseSubscriber improvements from REST module to JSON API:I totally agree that it's not very clear though. So, moving that new if-test into
::deserializeBody()which I think makes this 10x more clear :)Comment #6
gabesulliceNit: "Empty request body"
Let's avoid Drupal terms (although technically you could say they're not :P )
RTBC if nit is addressed.
Comment #7
wim leers👌 (I c/p'ed that from
\Drupal\rest\Plugin\rest\resource\EntityResource::post().)Comment #9
wim leersGreen test run! Also: yay, again more of the test coverage ported from core REST is actually running!
Comment #10
wim leersThis also broke #2934149-26: [>=8.5] JSON API routes not specifying _content_type_format route requirement, resulting in bad DX, so rebased that: #2934149-31: [>=8.5] JSON API routes not specifying _content_type_format route requirement, resulting in bad DX.
Comment #11
pwolanin commentedI just updated to 8.x-1.17 and I am still getting similar errors when running our behat tests. In the test a node is unpublished via PATCH and then the user tries to fetch it with GET.
The error is not happening with a simple GET when I do it manually
The error message is the same as in the issue description here:
Comment #12
pwolanin commentedCreated a new issue here: #2972107: JSON API's RequestHandler causes fatal PHP error when a GET request has a body