.../rest/src/Plugin/rest/resource/EntityResource.php | 8 ++++++-- .../EntityResource/EntityResourceTestBase.php | 19 +++++++++++++++---- .../EntityTestLabelResourceTestBase.php | 7 ------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php index a4162b5..9b1d15f 100644 --- a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php +++ b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php @@ -189,8 +189,12 @@ public function post(EntityInterface $entity = NULL) { // 201 Created responses return the newly created entity in the response // body. These responses are not cacheable, so we add no cacheability // metadata here. - $url = $entity->urlInfo('canonical', ['absolute' => TRUE])->toString(TRUE); - return new ModifiedResourceResponse($entity, 201, ['Location' => $url->getGeneratedUrl()]); + $headers = []; + if (in_array('canonical', $entity->uriRelationships(), TRUE)) { + $url = $entity->urlInfo('canonical', ['absolute' => TRUE])->toString(TRUE); + $headers['Location'] = $url->getGeneratedUrl(); + } + return new ModifiedResourceResponse($entity, 201, $headers); } catch (EntityStorageException $e) { throw new HttpException(500, 'Internal Server Error', $e); diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php index d140e82..efc001a 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php @@ -584,6 +584,7 @@ public function testPost() { } $this->initAuthentication(); + $has_canonical_url = $this->entity->hasLinkTemplate('canonical'); // Try with all of the following request bodies. $unparseable_request_body = '!{>}<'; @@ -725,8 +726,13 @@ public function testPost() { // 201 for well-formed request. $response = $this->request('POST', $url, $request_options); $this->assertResourceResponse(201, FALSE, $response); - $location = $this->entityStorage->load(static::$firstCreatedEntityId)->toUrl('canonical')->setAbsolute(TRUE)->toString(); - $this->assertSame([$location], $response->getHeader('Location')); + if ($has_canonical_url) { + $location = $this->entityStorage->load(static::$firstCreatedEntityId)->toUrl('canonical')->setAbsolute(TRUE)->toString(); + $this->assertSame([$location], $response->getHeader('Location')); + } + else { + $this->assertSame([], $response->getHeader('Location')); + } $this->assertFalse($response->hasHeader('X-Drupal-Cache')); @@ -746,8 +752,13 @@ public function testPost() { // 201 for well-formed request. $response = $this->request('POST', $url, $request_options); $this->assertResourceResponse(201, FALSE, $response); - $location = $this->entityStorage->load(static::$secondCreatedEntityId)->toUrl('canonical')->setAbsolute(TRUE)->toString(); - $this->assertSame([$location], $response->getHeader('Location')); + if ($has_canonical_url) { + $location = $this->entityStorage->load(static::$secondCreatedEntityId)->toUrl('canonical')->setAbsolute(TRUE)->toString(); + $this->assertSame([$location], $response->getHeader('Location')); + } + else { + $this->assertSame([], $response->getHeader('Location')); + } $this->assertFalse($response->hasHeader('X-Drupal-Cache')); } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityTestLabel/EntityTestLabelResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityTestLabel/EntityTestLabelResourceTestBase.php index 28cf65e..d030787 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityTestLabel/EntityTestLabelResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityTestLabel/EntityTestLabelResourceTestBase.php @@ -153,11 +153,4 @@ protected function getExpectedUnauthorizedAccessMessage($method) { } } - /** - * {@inheritdoc} - */ - public function testPost() { - $this->markTestSkipped(); - } - }