CommentFileSizeAuthor
#33 2987609-33.patch31.07 KBgabesullice
#25 2987609-25.patch30.45 KBgabesullice
#23 2987609-23.combined.patch54.99 KBgabesullice
#23 2987609-23.patch28.83 KBgabesullice
#23 interdiff-23.txt793 bytesgabesullice
#22 2987609-22.combined.patch55.04 KBgabesullice
#22 2987609-22.patch28.05 KBgabesullice
#22 interdiff-reroll.txt57.87 KBgabesullice
#21 2987609-20.patch32.14 KBWim Leers
#20 2987609-19.patch32.08 KBgabesullice
#19 interdiff-14-19.txt2.5 KBgabesullice
#19 2987609-19.patch70.19 KBgabesullice
#14 interdiff-8-14.txt7.17 KBgabesullice
#14 2987609-14.combined.patch67.25 KBgabesullice
#14 2987609-14.patch29.99 KBgabesullice
#11 2987609-8.combined.patch62.21 KBgabesullice
#8 2987609-8.patch23.55 KBgabesullice
#8 interdiff-2-8.txt2.72 KBgabesullice
#2 2987609-2.patch21.49 KBgabesullice
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

gabesullice created an issue. See original summary.

gabesullice’s picture

Status: Active » Needs review
FileSize
21.49 KB

Status: Needs review » Needs work

The last submitted patch, 2: 2987609-2.patch, failed testing. View results

Wim Leers’s picture

What do we gain from this? Slightly more consistency/less variation in code?

gabesullice’s picture

The 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.

Wim Leers’s picture

Ah, so this is a necessary step to be able to do this:

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.

i.e. to use a route enhancer to achieve the same.

Correct?

But aren't they already being upcast by core's route enhancer?

gabesullice’s picture

Correct? But aren't they already being upcast by core's route enhancer?

I 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, like deleteIndividual(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:

$entity_type_id = $resource_type->getEntityTypeId();
if ($entity = $request->get($entity_type_id)) { ...

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".

gabesullice’s picture

Status: Needs work » Needs review
FileSize
2.72 KB
23.55 KB

Missed a few spots. Let's see what's left.

Status: Needs review » Needs work

The last submitted patch, 8: 2987609-8.patch, failed testing. View results

gabesullice’s picture

gabesullice’s picture

lol, actually attached.

gabesullice’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 11: 2987609-8.combined.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

gabesullice’s picture

The last submitted patch, 14: 2987609-14.patch, failed testing. View results

Wim Leers’s picture

Title: Rename the entity parameter from the entity type ID to 'entity' for all routes » [PP-1] Rename the entity parameter from the entity type ID to 'entity' for all routes
Status: Needs review » Postponed

Thanks 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:

+++ b/src/Controller/RequestHandler.php
@@ -45,9 +45,8 @@ class RequestHandler {
-    $entity_type_id = $resource_type->getEntityTypeId();
-    if ($entity = $request->get($entity_type_id)) {
-      $parameters[$entity_type_id] = $entity;
+    if ($entity = $request->get('entity')) {
+      $parameters['entity'] = $entity;
     }

This code change is not the end goal, right?

If we want that entity to be automatically passed to the controller method

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?

gabesullice’s picture

Thanks for #7. That helped!

Awesome!

The patch here looks good, but is blocked on #2987608: Move deserialization from RequestHandler to JsonApiParamEnhancer. Marking this postponed.

👍

This code change is not the end goal, right?

Definitely not.

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?

Exactly right.

Wim Leers’s picture

Thanks 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.

gabesullice’s picture

#2987608-13: Move deserialization from RequestHandler to JsonApiParamEnhancer added a few new test cases that need to have the parameter name updated.

gabesullice’s picture

#19 was actually a combined patch even though it wasn't named accordingly. Here's the patch that should apply after #2987608 lands.

Wim Leers’s picture

gabesullice’s picture

Wim Leers’s picture

AFAICT this could happen before #2987608: Move deserialization from RequestHandler to JsonApiParamEnhancer? That issue is far trickier to get done than this one.

gabesullice’s picture

Status: Postponed » Needs review
FileSize
30.45 KB

You're right, this can happen independently. Rerolled as such.

Wim Leers’s picture

Title: [PP-1] Rename the entity parameter from the entity type ID to 'entity' for all routes » Rename the entity parameter from the entity type ID to 'entity' for all routes
Status: Needs review » Reviewed & tested by the community

See #6 and #7 for the reason why we want to do this. I think this is a worthwhile simplification.

The last submitted patch, 21: 2987609-20.patch, failed testing. View results

The last submitted patch, 20: 2987609-19.patch, failed testing. View results

The last submitted patch, 22: 2987609-22.patch, failed testing. View results

The last submitted patch, 23: 2987609-23.patch, failed testing. View results

e0ipso’s picture

I am still unclear what we gain from this (@Wim Leers also asked about this in #4).

From #5:

The 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.

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:

  1. We move away from how Drupal core implements the parameter enhancers (our current implementation was drawn from there).
  2. We break internal APIs, which may be used by JSON API Extras, JSON API Defaults, …

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.

Wim Leers’s picture

Right, 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:

  1. \Drupal\jsonapi\Controller\EntityResource::getIndividual() and other methods on that class have an EntityInterface $entity parameter
  2. Right now we have custom logic in \Drupal\jsonapi\Controller\RequestHandler::handle() to map the route's parameter to the controller:
        $entity_type_id = $resource_type->getEntityTypeId();
        if ($entity = $request->get($entity_type_id)) {
          $parameters[$entity_type_id] = $entity;
        }
    
  3. If we'd rename it from node, user etc to just entity, then RequestHandler can just use Symfony's ArgumentsResolver, which brings us one step closer to getting rid of RequestHandler altogether
  4. Which would mean that we end up just with a single class with many controller methods: EntityResource. This means less code to maintain, fewer intermediate steps, and path paved for future customizations/features.

Correct, Gabe?

gabesullice’s picture

Rebased.

@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 ✋

  • gabesullice committed 1a26fb5 on 8.x-2.x
    Issue #2987609 by gabesullice, Wim Leers, e0ipso: Rename the entity...
gabesullice’s picture

Status: Reviewed & tested by the community » Fixed
Wim Leers’s picture

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 ✋

👍

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.