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.
Comment | File | Size | Author |
---|---|---|---|
#33 | 2987609-33.patch | 31.07 KB | gabesullice |
| |||
#25 | 2987609-25.patch | 30.45 KB | gabesullice |
| |||
#23 | 2987609-23.combined.patch | 54.99 KB | gabesullice |
#23 | 2987609-23.patch | 28.83 KB | gabesullice |
#23 | interdiff-23.txt | 793 bytes | gabesullice |
Comments
Comment #2
gabesulliceComment #4
Wim LeersWhat do we gain from this? Slightly more consistency/less variation in code?
Comment #5
gabesulliceThe routing system inspects method parameter names to associate them with provided route parameters. These parameters are drawn from route defaults or the URL. Without doing this, we'd need to do some gymnastics with a route enhancer to make this work because we can't change the name of the PHP argument to the controller methods.
IOW, this is just removing unnecessary complexity, it was just baked in from the beginning. There's no technical reason to have the route params named by the entity type ID even today.
Comment #6
Wim LeersAh, so this is a necessary step to be able to do this:
i.e. to use a route enhancer to achieve the same.
Correct?
But aren't they already being upcast by core's route enhancer?
Comment #7
gabesulliceI don't think so. And yes, but that has nothing to do with this.
The routing system uses an arguments resolver. That resolver matches route parameters, like
{node}
in/jsonapi/node/article/{node}
with arguments to a controller method, likedeleteIndividual(ResourceType $resource_type, EntityInterface $node)
. It uses reflection to find the order of the method arguments and matches them by name to parameters. Of course, we're dealing with a lot more than just nodes, so our method argument is actually$entity
.Now, this isn't a problem today in the RequestHandler because we get the route parameter like so:
We determine which request parameter to fetch based on the resource type. Then the
RequestHandler
just passes it in a hardcoded order.If we want that entity to be automatically passed to the controller method, then we'll need to do this to rename it to be
'entity'
as it's done in the patch. Otherwise, we'll have to somehow transfer the 'node' or 'taxonomy_term' to an 'entity' parameter at runtime after core's route enhancer has already upcast it, i.e. do "gymnastics".Comment #8
gabesulliceMissed a few spots. Let's see what's left.
Comment #10
gabesulliceSame patch, combined with #2987608-10: Move deserialization from RequestHandler to JsonApiParamEnhancer.
Comment #11
gabesullicelol, actually attached.
Comment #12
gabesulliceComment #14
gabesulliceComment #16
Wim LeersThanks for #7. That helped!
The patch here looks good, but is blocked on #2987608: Move deserialization from RequestHandler to JsonApiParamEnhancer. Marking this postponed.
Also, to ensure I understand the end goal:
This code change is not the end goal, right?
This is, right? Which would mean something like #2720233: Use arguments resolver in RequestHandler to have consistent parameter ordering, right? Do you plan to do that as part of #2987610: Remove RequestHandler class and service and add EntityResource methods to each route definition?
Comment #17
gabesulliceAwesome!
👍
Definitely not.
Exactly right.
Comment #18
Wim LeersThanks for confirming! 🙏
Sounds good! 👌
I would like to see a patch for that final issue then, so we can see how all these steps actually simplify things.
Comment #19
gabesullice#2987608-13: Move deserialization from RequestHandler to JsonApiParamEnhancer added a few new test cases that need to have the parameter name updated.
Comment #20
gabesullice#19 was actually a combined patch even though it wasn't named accordingly. Here's the patch that should apply after #2987608 lands.
Comment #21
Wim LeersRebased; #2949807: Spec Compliance: Error responses are missing the `jsonapi` top-level member conflicted with this slightly.
Comment #22
gabesulliceRerolled onto #2987608-19: Move deserialization from RequestHandler to JsonApiParamEnhancer
Comment #23
gabesulliceMissed a new rename.
Comment #24
Wim LeersAFAICT this could happen before #2987608: Move deserialization from RequestHandler to JsonApiParamEnhancer? That issue is far trickier to get done than this one.
Comment #25
gabesulliceYou're right, this can happen independently. Rerolled as such.
Comment #26
Wim LeersSee #6 and #7 for the reason why we want to do this. I think this is a worthwhile simplification.
Comment #31
e0ipsoI am still unclear what we gain from this (@Wim Leers also asked about this in #4).
From #5:
One would assume that would translate on code simplification (hinted in #7), as the gymnastics were removed. However the patch RTBCed in #25 is purely cosmetic and doesn't come with such simplifications.
With that in mind, I'm unsure about the value of this given that:
I don't feel strongly against this issue, but I'd like you to give it a second thought before merging. After that, if you still think the benefits I fail to see outweight the inconviniencies listed here, do not hesitate to merge.
Comment #32
Wim LeersRight, I think @gabesullice should show more clearly what the benefits are that this enables in next steps.
In my understanding, it boils down to this:
\Drupal\jsonapi\Controller\EntityResource::getIndividual()
and other methods on that class have anEntityInterface $entity
parameter\Drupal\jsonapi\Controller\RequestHandler::handle()
to map the route's parameter to the controller:node
,user
etc to justentity
, thenRequestHandler
can just use Symfony'sArgumentsResolver
, which brings us one step closer to getting rid ofRequestHandler
altogetherEntityResource
. This means less code to maintain, fewer intermediate steps, and path paved for future customizations/features.Correct, Gabe?
Comment #33
gabesulliceRebased.
@Wim Leers, that's all correct :)
Also, for our own sanity, it makes it a lot easier to generate links because all you need is the resource type name and the UUID. You don't need the entity or a resource type object since you need to know the entity type ID as well.
Will commit if tests pass.
FWIW, I did a grep in JSON API Extras but was not able to find anywhere that this change would break. If it does break something, I'll pledge to write the patch ✋
Comment #35
gabesulliceComment #36
Wim Leers👍