diff --git a/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php b/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php index 7df2a05..22c7e6e 100644 --- a/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php +++ b/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php @@ -16,6 +16,7 @@ use Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel; use Drupal\jsonapi\ResourceType\ResourceType; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; @@ -143,7 +144,7 @@ class JsonApiDocumentTopLevelNormalizer extends NormalizerBase implements Denorm $canonical_ids = []; foreach ($id_list as $delta => $uuid) { if (empty($map[$uuid])) { - continue; + throw new NotFoundHttpException(sprintf('The resource identified by `%s:%s` (given as a relationship item) could not be found.', $relationship['data'][$delta]['type'], $uuid)); } $reference_item = [ 'target_id' => $map[$uuid], diff --git a/tests/src/Functional/NodeTest.php b/tests/src/Functional/NodeTest.php index ca6a2bb..ed06941 100644 --- a/tests/src/Functional/NodeTest.php +++ b/tests/src/Functional/NodeTest.php @@ -355,7 +355,7 @@ class NodeTest extends ResourceTestBase { $random_uuid = \Drupal::service('uuid')->generate(); $doc = $this->getPostDocument(); - $doc['data']['relationships']['uid'] = [ + $doc['data']['relationships']['uid']['data'] = [ 'type' => 'user--user', 'id' => $random_uuid, ]; @@ -369,9 +369,23 @@ class NodeTest extends ResourceTestBase { // POST request: 404 when adding relationships to non-existing resources. $response = $this->request('POST', $url, $request_options); - // @todo Remove $expected + assertResourceResponse() in favor of the commented line below once https://www.drupal.org/project/jsonapi/issues/2943176 lands. $expected_document = [ - '…', + 'errors' => [ + 0 => [ + 'status' => 404, + 'title' => 'Not Found', + 'detail' => "The resource identified by `user--user:$random_uuid` (given as a relationship item) could not be found.", + 'links' => [ + 'info' => [ + 'href' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5', + ], + 'via' => [ + 'href' => 'http://jsonapi.test/jsonapi/node/camelids', + ], + ], + ], + ], + 'jsonapi' => static::$jsonApiMember, ]; $this->assertResourceResponse(404, $expected_document, $response); }