Problem/Motivation
When you try to create an entity through REST and it doesn't have defined a canonical URL, it returns:
No link template "canonical" found for the "paragraph" entity type.
Proposed resolution
The Post method from EntityResource assumes that the created entity has an url and try to return it:
$url = $entity->urlInfo('canonical', ['absolute' => TRUE])->toString(TRUE);
return new ModifiedResourceResponse($entity, 201, ['Location' => $url->getGeneratedUrl()]);
The attached patch checks first it there is a canonical URL.
Remaining tasks
* Review
User interface changes
None
API changes
Post method for entity resource shouldnt try to return the entity URL if it doesnt have one.
Data model changes
None
Comments
Comment #2
ruloweb commentedComment #3
ruloweb commentedFor 8.3.x it will require a new patch, because some lines have changed, but lets review this one first.
Comment #4
ruloweb commentedComment #5
dagmarMakes sense. We now need a test for this that doesn't include the canonical url.
Comment #6
ruloweb commentedComment #7
wim leersGreat find!
Makes sense!
But rather than creating a response object in two places, let's do that once. And if this if-test is true, only then do:
That makes the code a bit easier to follow.
For test coverage, I recommend updating
EntityResourceTestBase::testPost()(grep for 201). However, to then test this, we need an entity type in Drupal core that doesn't have acanonicallink template.AFAICT the only existing example of a canonical-less content entity type is
\Drupal\entity_test\Entity\EntityTestNoId(yes, I inspected every single content entity type class in core).Comment #8
wim leersComment #10
ruloweb commentedThanks @Wim Leers for the suggestions, attached is a new version updated for 8.3.x, I am working on the test case now.
Change to review to run tests.
Comment #11
wim leersLet's remove this comment. It's clear from the code what's going on here.
Then the only thing that's still necessary is test coverage; I gave pointers for that in #7 :)
Comment #12
shadcn commentedSidenote:
\Drupal\aggregator\Entity\Itemdoes not havecanonicalroute either. But it implementsbuildUri. Came across this while working on #2843752: EntityResource: Provide comprehensive test coverage for Item entity.Comment #13
wim leersGreat, that means we have two examples!
Comment #14
wim leersSee #2853211-25: EntityResource::post() incorrectly assumes that every entity type has a canonical URL. That issue solves this issue: it has all the necessary test coverage. It's RTBC.
Closing this as a duplicate, and then migrating this issue's tags.