Closed (fixed)
Project:
JSON:API
Version:
8.x-1.x-dev
Component:
Code
Priority:
Major
Category:
Bug report
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
1 Jun 2017 at 15:50 UTC
Updated:
18 Apr 2018 at 17:09 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
salmonek commentedComment #3
spleshkaHi @salmonek, thanks for the report. I was able to reproduce the issue. It's quite interesting that the file is being created properly, but JSON API can't return it back to the client.
Comment #4
spleshkaI've debugged to the point where render context becomes not empty for private files. I'm not sure what is the reason of this behavior. For other file streams context behavior remains empty.
In the debugger I see this for the
$contextvar:For other file streams
$contextlooks like this:It looks to me like it's some kind of var dump metadata in the variable which makes it non-empty.
Comment #5
wim leersSomething must be leaking cacheability metadata onto the global render context that
\Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscribercreates specifically to detect those leaks.What you want to do is put a breakpoint in
\Drupal\Core\Render\RenderContext::bubble(). I'd bet that\Drupal\Core\StreamWrapper\PrivateStream::getExternalUrl()is causing cacheability metadata to bubble and hence leak onto the global render context.(Many/most URLs are not generated in a hardcoded way — they're generated depending on the current request's hostname, base path, and so on. Which means they vary by those things, and hence bubble the associated cache contexts. I bet that that's happening here.)
Comment #6
wim leersComment #7
spleshka@Wim, you're absolutely right, it's
\Drupal\Core\StreamWrapper\PrivateStream::getExternalUrl()bubbling the cacheability metadata. Is there a good way around this?Comment #8
tom_ek commentedHi, as far as I understand this - the problem lies in cache metadata for the POST request which should not be cached / should not contain the cacheability metadata. This is the core url generation issue and is being tackled here: https://www.drupal.org/node/2744715 . Applying the patch from this thread solved the issue for me.
Comment #9
grndlvl commentedThis patch mentioned in #8 did not resolve the issue for me and still issued the same issue as before after applying the patch from #18 from #2744715: Generated URLs should only have cache contexts on GET/HEAD. I plan on looking into this more today.
Comment #10
grndlvl commentedOk... so I am not sure if this is the RIGHT fix here as I am not entirely sure how this is suppose to be fixed, however, based on what I understand from #2638686: Correctly handle cache data instead of throwing an Exception in EarlyRenderingControllerWrapperSubscriber() it appears closer to my issue in that I am using JSONAPI to add a new file and thus a POST, however, this check was still happening all because the Response implemented CacheableResponseInterface. So I took a page out of the rest module and creating different responses based on if the response should be cacheable.... I don't think I got them all, but I was able to get my particular instance to work this way.
@Wim Leers Additional guidance would certainly be appreciated here as this seems to be something you are well familiar with... it's a bit confusing to me as to what's actually happening here as from what I can tell all the code if functioning as intended, but something is wrong.
Comment #11
grndlvl commentedFixing error in patch for Error: Call to a member function isEmpty() on null in Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber->Drupal\jsonapi\EventSubscriber\{closure}() (line 131 of modules/contrib/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php).
Comment #12
jayelless commentedThanks @grndlvl. The patch in #11 solved the problem for me.
Note that this patch need to be applied to module "jsonapi" rather than "jsonapi_file".
I think the name of the patch file could also be improved (refer https://www.drupal.org/patch/submit) to help with identification.
Comment #13
spleshkaChanging the issue project, since the decision has to be made by JSONAPI's maintainers.
Comment #14
wim leers"the decision" has to be made by JSON API's maintainers, you say, but the patch doesn't seem to have anything to do with the issue title or summary?
An updated issue title + summary would make it much easier to provide feedback :)
Comment #15
spleshkaFair enough :) Fixed the title & updated the summary. I'm not entirely sure that the summary is completely correct given the lack of the involvement into the issue, so feel free to correct me (left the tag in place). So far it feels like the issue outside of JSON API File module and the potential work around (or a solid fix) should be implemented on JSON API level.
Comment #16
spleshkaComment #17
wim leersThe issue title + summary claim this is about private file uploads. But there's zero mentions of that in the patch.
The patch appears to be porting #2626298: REST module must cache only responses to GET requests from REST module to JSON API module. I've been wanting to do something like that for a while now, but hadn't gotten to it yet.
Yes, I support the overall direction of this patch, but it has very little to do with file uploads at this point. We should still add an explicit test with the originally reported bug, to prevent regressions.
Review:
Do we really want the exact same class names and even inheritance trees as the REST module's classes, but have alternative implementations?
And let's mark all of them
@internal!Comment #18
jayelless commentedThe problem with creation of files in the "private://" file storage system appears to have disappeared with the jsonapi 8.x-1.7 release (noting that I went direct from 1.4 to 1.7).
Comment #19
e0ipsoI agree with the review in #17. Additionally:
Maybe use
!$request->isMethodSafe()from the request object.Let's make sure we always add the
'cacheable_metadata'to the context. Even if it's not a cacheable response. We may need to create a newNullCacheableMetadataobject that follows the Null pattern.Comment #20
e0ipso@jlscott w00t! Can we reflect that in the issue summary?
Comment #21
jayelless commentedUnfortunately I appear to have been mistaken. The success I had must have been due to an anomaly on my development environment, as the issue is still present on the Acquia environments. What is more, the patch no longer applies to 1.7 :(. I guess it needs to be re-rolled.
Comment #22
jayelless commentedRerolled patch file attached.
Comment #23
e0ipso@jlscott any chance you can write the tests requested in #17? If so, we can find a way to implement the missing feedback in here one way or another :-)
Thanks for you contributions!
Comment #24
wim leersRecently, more improvements landed in core's REST
RequestHandler(#2853460: Simplify RequestHandler: make it no longer ContainerAware, split up ::handle() + #2935783: RequestHandler's handle method interacts with the Route object but should be able to just declare arguments). I'm going to port all of these over at once. Thanks to #2930028: Comprehensive JSON API integration test coverage phase 1: for every entity type, individual resources only + #2932031: Comprehensive JSON API integration test coverage phase 2: for every entity type, complex use-cases, we can now do this with a high level of certainty that we're not regressing!The test coverage unveiled problems wrt cacheable responses, which #2933939: JSON API module must not send cacheable responses to PATCH, POST and DELETE requests fixed. So this:
which I raised in #17 and @e0ipso agreed with in #19 is no longer relevant, because we don't need to fix it here anymore: #2933939 implemented a simpler approach that is also more in line with what the JSON API module's architecture. (Where there's more levels of indirection, more reuse of objects, more value objects — because http://jsonapi.org/format is a much more capable format.)
Comment #25
wim leersI'll take this on next week.
Comment #26
gabesulliceIt appears that this patch probably needs a reroll. Context in this issue: #2946746: Unhandled exceptions/fatal errors when POST/PATCH documents contain unknown field names
Comment #27
wim leersNow actively working on this, it's a blocker for some of the things @gabesullice is/was working on.
Comment #28
wim leersRelated:
Comment #29
wim leersThis is blocked on #2940342: Cacheability metadata on an entity fields' properties is lost. Because without that issue being fixed, there's a definite risk that we regress in the handling of cacheability in this issue.
Comment #30
wim leers#2940342: Cacheability metadata on an entity fields' properties is lost landed, let's get this going again! :D
Comment #31
gabesulliceComment #32
wim leersThanks 😅
Comment #33
wim leersI’ve really started working on this now. I’m increasingly convinced we should block this on:
(in that order), and then roll this on top of both of them. Because both of them make absolutely crucial simplifications.
Comment #34
wim leersPer #33, this is blocked on #2935370: Mark the JSON API serializer, normalizer and encoder services as private (currently RTBC) ⇒ PP-1
Per #33, this is also blocked on #2941685: Port parameter based resource config from REST module (⇒ PP-2), which in turn is 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 (⇒ PP-3).
Comment #35
wim leersTo clarify #33:
Comment #36
wim leersI've successfully rebased #2941685 on top of #2935370, see #2941685-15: Port parameter based resource config from REST module: tests are green. Time to get this going!
Let's start with something simple: remove
\Drupal\jsonapi\Controller\RequestHandler::$requiredCacheContexts. That should no longer be necessary.The "combined" patch includes #2935370-24: Mark the JSON API serializer, normalizer and encoder services as private + #2941685-15: Port parameter based resource config from REST module.
Comment #37
wim leersExcellent, only
BlockTestfailed, and this makes sense: its expectations just need to be updated. Now it matches core's\Drupal\Tests\rest\Functional\EntityResource\Block\BlockResourceTestBase::getExpectedCacheContexts()!Comment #38
wim leersThis ports #2853460: Simplify RequestHandler: make it no longer ContainerAware, split up ::handle() as much as possible:
::deserializeBody()with REST's more refined::deserialize(). Doing that actually fixes #2943165: Failing to decode should result in a 400 response, failing to denormalize should result in a 422 response.\Drupal\jsonapi\Controller\RequestHandler::getNormalizedRequestMethod(), because unlike the REST module, JSON API supports multiple methods per route.Comment #39
wim leersThis is what #2853460: Simplify RequestHandler: make it no longer ContainerAware, split up ::handle() was all about! So, yay for seeing this ported!
Separate decoding and denormalizing is what fixes #2943165: Failing to decode should result in a 400 response, failing to denormalize should result in a 422 response.
This should've been part of #37 😳
This is what proves it fixed #2943165: Failing to decode should result in a 400 response, failing to denormalize should result in a 422 response.
Comment #40
wim leersInheriting issue metadata from #2943165: Failing to decode should result in a 400 response, failing to denormalize should result in a 422 response.
Comment #41
wim leersTwo fails.
This is doing a pseudo unit test of
RequestHandler::deserializeBody(). That method was renamed. But also, apparently it was public before. Now we know why: just for this test. Fortunately we now have integration test coverage actually catching this, so we can simply remove this test altogether!Fixing #2943165: Failing to decode should result in a 400 response, failing to denormalize should result in a 422 response caused this to fail. Because we're now getting the expected 400 instead of 422. Expanded this test coverage.
Comment #42
wim leers100% done now with porting #2853460: Simplify RequestHandler: make it no longer ContainerAware, split up ::handle(). Current diffstat:
🎉
Comment #43
wim leersAnother issue that should definitely be ported is #2807501: ResourceResponse::$responseData is serialized, but is never used: unserializing it on every Page Cache hit is a huge performance penalty. Even though #2855693: Remove \Drupal\jsonapi\Controller\RequestHandler::renderJsonApiResponse(), add \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber already ported this over a year ago, it did not yet remove all
executeInRenderContext()calls: somehow JSON API ended up with multiple of those. No problem. Let's see what fails.Comment #44
wim leersThis should fix some (hopefully most) of #43's failures.
Comment #45
wim leersOh, #2935370: Mark the JSON API serializer, normalizer and encoder services as private landed in the mean time! Redoing the combined patch :) Now only on top of #2941685: Port parameter based resource config from REST module.
Comment #46
wim leersComment #47
wim leersFixing all of #43's failure leads us too far out of scope. #44 already kind of shows this: the current
LinkManagerdocumentation is a lie: it pretendsstrings are returned and that's it, but it actually also bubbles cacheability metadata out-of-band. That's where all the failures are coming from.The JSON API module is hardly to blame: it based this "link manager" idea off of core's
\Drupal\hal\LinkManager\LinkManager… and that was similarly flawed until very recently: #2864816: HAL LinkManager doesn't add 'url.site' cache context when needed.Created #2952714: Group module's GroupAccessResult::allowedIfHasGroupPermission(s)() does not include cacheability to fix that at some point in the future. So, reverting #43+#44's interdiffs, and instead adding two @todos pointing to that new issue.
Comment #48
wim leersPer #24, we don't need the
ResourceResponse(Interface)changes anymore, and #2626298: REST module must cache only responses to GET requests has been solved. What is necessary, is verifying that JSON API'sResourceResponseSubscriberresemblesrest.module's as much as possible, to ensure minimum divergence and maximum carrying over of improvements/bugfixes.Doing that comparison shows that they are already maximally identical. (Note that JSON API's
ResourceResponseSubscriberwas added in #2855693: Remove \Drupal\jsonapi\Controller\RequestHandler::renderJsonApiResponse(), add \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber.)Notes:
This has since been fixed! So now this can be made consistent! Point removed :)
Comment #49
wim leersAnd with that, this is done as far as I'm concerned! This is ready to be reviewed (and hopefully RTBC-worthy) as soon as the blockers are in.
There's no need for additional tests — #2930028: Comprehensive JSON API integration test coverage phase 1: for every entity type, individual resources only + #2932031: Comprehensive JSON API integration test coverage phase 2: for every entity type, complex use-cases + #2945093: Comprehensive JSON API integration test coverage phase 3: test JSON API-specific use cases: related/relationship routes, includes and sparse field sets already ensure we don't regress.
Comment #50
wim leersOh, almost forgot: I also inspected the git log for both REST's
RequestHandlerandResourceResponseSubscriber, and did not find any other issues/commits that need to be ported to JSON API's classes with the same name.Comment #51
wim leersd.o--
Comment #52
gabesulliceThis looks awesome. I have a couple suggestions that are independent of the prerequisites, so moving back to needs work.
Understanding: The
$requestalready carries the method information.s/ikf/if/
Understanding: We can drop the
isMethodCacheablebecause anything with a body (POST/PATCH) is not cacheable.This is a nice DX improvement.
Clean!
Why 128? That seems arbitrary and deserving of a comment.
Comment #53
gabesulliceOnly blocked on #2941685: Port parameter based resource config from REST module now
Comment #54
wim leers\Drupal\rest\EventSubscriber\ResourceResponseSubscriber::getSubscribedEvents()does.It means we'll probably/hopefully be able to refactor
restandjsonapi'sResourceResponseSubscriberclasses away, and move them to a shared one inserialization.Added a comment to clarify this.
Comment #55
gabesullice💥
Comment #58
chihada commentedI have the same problem in #2946746: Field data is unknown
I applied the patch 2883086-54-combined.patch but the problem did not solved
I am using Acquia Lightning 3.0.3 release
Comment #60
e0ipso🎊❗️
Merged.
Comment #61
jayelless commentedThanks everyone. I can confirm that release 1.15 has fixed the problems I had with saving files in the private file system.
Comment #63
wim leers@jlscott: great! 🎉 Thanks for letting us know :)