Problem/Motivation
Discovered by @alexpott in #3224523: [PHP 8.1] Add ReturnTypeWillChange attribute where necessary, we include DependencySerializationTrait in the JSON API exceptions EntityAccessDeniedHttpException and UnprocessableHttpEntityException. The code has existed since these exceptions were first commited.
However, this seems unnecessary as neither exception stores anything which requires special handling if the exceptions are serialized. @alexpott already proved this does not break tests in #2561915-24: alexpott's test issue.
Steps to reproduce
Proposed resolution
Remove DependencySerializationTrait from the exceptions.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 3231040-4.patch | 1.73 KB | anul |
| #10 | 3231040-revert-10.patch | 1.73 KB | alexpott |
Comments
Comment #2
alexpottThese were created in https://git.drupalcode.org/project/jsonapi/-/commit/cdbd063974f645fefad7... - gonna ping Wim.
Comment #3
longwaveThat commit also added a todo to remove a
__sleep()in #2855693: Remove \Drupal\jsonapi\Controller\RequestHandler::renderJsonApiResponse(), add \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber, which was then done as planned, so I am hopeful this means the use of this trait was obsoleted at the same time.Comment #4
anul commentedHi Dave,
Removed
DependencySerializationTraitfrom the exceptionsEntityAccessDeniedHttpExceptionandUnprocessableHttpEntityException.Adding a patch for the same.
Comment #5
bbralaI've reviewed the patch and the linked issues and commit. I don't see any objections why this would result in issues. Setting RTBC, thanks!
Comment #6
catchLooks like this is redundant, nice find.
Committed 06e7f02 and pushed to 9.3.x. Thanks!
Comment #8
catchComment #10
alexpottThis has caused a regression on a client project of mine. JsonAPI access denied requests are failing due to
This happens because
\Drupal\jsonapi\JsonApiResource\ErrorCollectioncontains exceptions and exceptions have traces and traces have arguments and these can be anything. And if one of these contains a reference to a database we’re done for.The super super amusing thing is that the only part of the code from DependencySerializationTrait that is necessary is
because that happens to stop the trace being serialised as the trace is not returned in
get_object_vars($this).This serialisation occurs because
\Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber::setEventResponseadds all this to the response object which is serialised as part of the page cache.I think we should revert this change and then turn this into one that adds a test for serializing an exception with a db connection in the trace as part of a cacheable JSON api response.
Here's a patch to prove the revert doesn't break anything.
Comment #11
larowlanComment #12
bbralaOk, that was unexpected. Nice one.
Comment #15
catchCommitted/pushed the revert to 9.4.x and 9.3.x.
Restoring this back to old metadata for now.
Comment #16
bbralaIn order to get this moving forward again we need a few things:
ErrorCollectionso we have a clear plan.Comment #17
alexpott@bbrala we will be able to remove the DependencySerializationTrait.
One option is to add ExceptionSerializationTrait which does:
as this prevents the trace being serialised which is what is causing the problems.
But I think in an ideal world we wouldn't be serialising the exceptions at all. It feels wrong.
Comment #18
longwaveShouldn't the page cache contain the already normalized response, instead of all the source objects required to build the response? To me this implies that it's possible that a cached page may be normalized differently (and there is unnecessary work being done) if it is re-normalized every time it is retrieved from cache.
ResourceResponseSubscriber has this note:
but maybe this isn't being run for exception responses?