Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
The current class can be improved significantly in these ways:
- retrieves the serializer directly from the container (because
ContainerAwareInterface
), should instead be injected, because there's no measurable performance difference (see #2) - Then we're no longer using
ContainerAware(Interface|Trait)
, so we can remove them RequestHandler::handle()
is a 60-line function in which a lot of things happen. Splitting it up into multiple helper methods that are each called would make it far more maintainable.
Comment | File | Size | Author |
---|---|---|---|
#19 | 2853460-19.patch | 11.27 KB | Wim Leers |
Comments
Comment #2
Wim LeersThe performance difference for point 2, measured with Page Cache turned off, best of 5, with testing this:
There is no measurable difference. So then there is also no point in not injecting the serializer of course: better to do that, it's cleaner!
Point 2 was:
IS updated.
Comment #3
Wim LeersThis patch is relative to #2826407: PATCH + POST allowed format validation happens in RequestHandler::handle(), rather than in the routing system, hence this issue is marked postponed.
Comment #4
Wim LeersNow also fixing
array()
->[]
.Comment #5
Wim LeersRequestHandler::handle()
is a 60-line function in which a lot of things happen. Splitting it up into multiple helper methods that are each called would make it far more maintainable.Comment #6
dawehnerI don't see why this is a good change. It just gives you a fun time if you actually debug that.
+1
I think when the class was first introduced controllers did not had dependency injection yet.
We have a dedicated issue to do that.
Nice
Comment #7
Wim LeersHuh? Isn't it the rule to use this whenever possible?
Comment #8
dawehnerI talked with alexpott and we certainly don't recommend it core. For custom/contrib code sure, whatever, but for core we prefer injection over everything else.
Comment #9
Wim LeersOk. Will reroll this once #2826407: PATCH + POST allowed format validation happens in RequestHandler::handle(), rather than in the routing system lands. Thanks for checking!
Comment #10
Wim Leers#2826407 is now blocked on #2472337: Provide a lazy alternative to service collectors which just detects service IDs.
Comment #11
Wim Leers#2472337: Provide a lazy alternative to service collectors which just detects service IDs landed, #2858482: Simplify REST routing: disallow requesting POST/PATCH in any format, make consistent has been rerolled and is now blocked on review. Once that's in, we can continue here!
Comment #13
Wim LeersAs of #2858482-39: Simplify REST routing: disallow requesting POST/PATCH in any format, make consistent, #2858482 is blocked on another issue: #2883680: Force all route filters and route enhancers to be non-lazy. This therefore has a chain of 2 issues blocking it.
Comment #14
Wim Leers#2883680: Force all route filters and route enhancers to be non-lazy landed 2 months ago, ever since, we've only been blocked on #2858482: Simplify REST routing: disallow requesting POST/PATCH in any format, make consistent, which has received another ~80 comments since #13 (😮), but is now RTBC! 🎉
Comment #15
Wim LeersRebased #5.
Also: turns out this is no longer blocked on #2858482: Simplify REST routing: disallow requesting POST/PATCH in any format, make consistent! Earlier iterations of #2858482 were changing
RequestHandler
probably, but that's no longer the case.Comment #17
Wim LeersI forgot to update the unit test. Easy-peasy.
Comment #18
gabesulliceNit: "The normalized HTTP request method."
It took me a bit to realize this was not referring to class methods.
Otherwise, this looks really good!
Comment #19
Wim LeersDone!
Comment #20
gabesulliceAwesome!
Comment #21
neclimdulis this an "interface" we have to deprecate? yuck. :-/
Comment #22
Wim LeersI'm not sure what you're asking, @neclimdul?
Comment #23
neclimdulI guess I'm asking how strict we're going to be about our BC. We're removing a public method by removing the trait. Its not a particularly useful or likely to be used interface but it is one.
Comment #24
Wim LeersThis is a controller, which doesn't have any BC guarantees: https://www.drupal.org/core/d8-bc-policy#controllers, so we're good :)
Comment #25
larowlanShouldn't we be able to list this as an argument, and if properly named and type hinted then the controller resolver will upcast it? Or are you opting not to do that for some other reason (other than the naming
$_rest_resoure_config
which would suck). If so, should we document why?Comment #26
Wim LeersNo — things in a route definition's
defaults
are never upcasted. We're not changing the logic you're citing at all: we're just making sure clearly separate concerns are in separate methods, making theRequestHandler
easier to understand, maintain, debug.Proof this is just being moved:
Also, that'd be perfect to address in a follow-up, if it is indeed possible!
Comment #27
webchick@Wim Leers walked me through this on Slack. As stated, this is just refactoring/moving around existing code to make it more maintainable.
I had the same question as @neclimdul in #23, and got the same answer as #24. :D And the fact that the only tests being changed here as a result of this patch are internal back up the fact that this is an internal API.
It looks like @larowlan's concerns are addressed at #26, and this patch is blocking file upload functionality in the REST API, which is pretty flippin' critical, soooo...
Committed and pushed to 8.5.x. Thanks!
Comment #29
Wim LeersYay, this already really helped: #1927648-501: Allow creation of file entities from binary data via REST requests shows that that this allows us to bring the number of changes in
RequestHandler
down from 140 lines to 60! (And each of the remaining changes is much, much simpler, no longer ambiguous.)Comment #30
larowlanOpened #2935783: RequestHandler's handle method interacts with the Route object but should be able to just declare arguments to explore #25
Comment #31
Wim LeersThanks, @larowlan, and turns out that what you envisioned works, just not the upcasting bit (which is what I thought you meant and hence wrote#26). Already RTBC'd it :)