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.
I was testing D8.2.2 REST API with Commerce 2-beta3.
When I simply GET a Commerce Order, I receive an error in the Drupal log:
Symfony\Component\Routing\Exception\RouteNotFoundException: Route "entity.commerce_order_item.canonical" does not exist. in Drupal\Core\Routing\RouteProvider->getRouteByName() (line 187 of /var/www/html/core/lib/Drupal/Core/Routing/RouteProvider.php).
This is because Commerce Order Item Entity doesn't have a canonical path nor it is going to have one.
We need to add support for entities which don't have canonical path.
I had a discussion with on IRC, he suggested the bug is somewhere near:
\Drupal\rest\Plugin\Deriver\EntityDeriver::getDerivativeDefinitions
and
\Drupal\rest\Routing\ResourceRoutes::getRoutesForResourceConfig
Comments
Comment #2
skyredwangComment #3
Wim LeersNeither does core's
\Drupal\file\Entity\File
.I don't see what the problem is? Some entities just don't have URLs, because they're not exposed to the world. That's a valid decision. What's the problem here?
Comment #4
dawehnerWell, the problem we have is actually that we make two decisions at the same time:
If you think about it, as you said before, its clear that a) doesn't have to be TRUE. On the other hand, for REST we need b). Now you probably argue,
sure, its totally enough to just use
'/' . strtr($this->pluginId, ':', '/') . '/{id}'
as URL for that entity, and we are fine. That's true, unless we are linking to those entities. In there we seem to use the URL to the entity rather than the REST URL to that particular entity.Comment #5
skyredwangThe problem is that REST module will throw errors when they encounter entities without canonical path.
Comment #6
Wim LeersThe REST module explicitly is checking whether a given entity type has a canonical URL or not. So please provide steps to reproduce this.
Comment #7
dawehnerI assume some of the serializers don't do so.
Comment #8
chrisrikli CreditAttribution: chrisrikli commentedI'm seeing the same error when performing a POST to create a custom entity. Adding an entry to the module's routing.yml table solves the issue but...this seems like a hack. I'm hoping a more knowledgeable party can tell me if this is the "right" way to resolve this issue.
Comment #9
BerdirSomething is definitely not doing that. you actually provided the best example file. It does *not* have a link template, but it overrides url() to do something custom. If we'd correctly check that, then we would *not* call $file->url() anymore and actually break that hack that was added there. That said, those url() calls are "interesting" because unlike toUrl(), they do not throw an exception, so not having one just results in an empty string atm.
Comment #10
Wim LeersThe code dealing with canonical URLs is not using the deprecated
urlInfo('canonical')
nortoUrl('canonical')
. It's using$entity_type->getLinkTemplate('canonical')
. See\Drupal\rest\Plugin\Deriver\EntityDeriver::getDerivativeDefinitions()
.Comment #11
skyredwangI made a test on File Entity with the steps below, but I cannot see a similar error on File Entity:
1. Fresh install D8.2.3
2. Add a file field to Article Content Type
3. Create an article node and filled with random info and uploaded two files in the file field
4. Enable REST and REST UI modules
5. GET /node/1?_format=json, then everything works fine.
Comment #12
skyredwangFollowing #11, I was trying to make a second test with Order Entity from Drupal Commerce. Then, without making to REST step, I was able to reproduce the original error above.
6. Add an Order Item (entityreference) field to the Article Content Type
7. Create a random Order with random Order Items via Drupal Commerce Order management /admin/commerce/orders
8. Edit the article node created above, and use auto-complete to add an existing Order Item to the Article.
9. Clicking save, then WSOD, in the Drupal log, the original error was thrown.
Can this result prove the bug is not in REST module? But, somewhere else?
Comment #13
skyredwang@Berdir showed me there is a error backtrace in D8
Comment #14
skyredwang@Berdir suggested the #12 was caused by Entity Reference Field Formatter. So, I update the field display to be no link. Then, I was able to finish saving on Step 9.
Then, Step 10, use REST to GET /node/1?_format=json. I still see the same original error.
Comment #15
Wim LeersI'm pretty sure I found the root cause.
Note the error is:
and not
So it's not about the
Order
entity. It's about theOrderItem
entity.Looking at the current code:
This has a
canonical
link and usesDefaultHtmlRouteProvider
, which provides acanonical
route.Compare this with:
This has a
canonical
link but has no route providers. Hence it says it has a canonical link/URI, but it doesn't have the corresponding route!Comment #16
skyredwangFollowing #15, I added
To the OrderItem definition.
Then, I am able to get the expected result for Step 10, and the original request on the top of this issue.
Comment #17
skyredwangOn the other hand, I also tested deleting this line:
It also fixed the problem, and REST works, too
Comment #18
Wim LeersYep, either #16 or #17 will work.
Comment #19
agoradesign CreditAttribution: agoradesign commented#17 is the right one. There's no need for a canonical link for order items
Comment #21
bojanz CreditAttribution: bojanz at Centarro commentedRemoved all of the links, they were years old and completely bogus.
Thank you for tracking this down!