src/Normalizer/JsonApiDocumentTopLevelNormalizer.php | 6 +++++- tests/src/Functional/JsonApiFunctionalTest.php | 9 +-------- .../JsonApiDocumentTopLevelNormalizerTest.php | 20 +++++++++++++++----- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php b/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php index 22c7e6e..51d0b1a 100644 --- a/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php +++ b/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php @@ -143,7 +143,11 @@ class JsonApiDocumentTopLevelNormalizer extends NormalizerBase implements Denorm // meta values from the relationship, whose deltas match with $id_list. $canonical_ids = []; foreach ($id_list as $delta => $uuid) { - if (empty($map[$uuid])) { + if (!isset($map[$uuid])) { + // @see \Drupal\jsonapi\Normalizer\EntityReferenceFieldNormalizer::normalize() + if ($uuid === 'virtual') { + 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 = [ diff --git a/tests/src/Functional/JsonApiFunctionalTest.php b/tests/src/Functional/JsonApiFunctionalTest.php index 4af293b..e25e4a0 100644 --- a/tests/src/Functional/JsonApiFunctionalTest.php +++ b/tests/src/Functional/JsonApiFunctionalTest.php @@ -530,12 +530,6 @@ class JsonApiFunctionalTest extends JsonApiFunctionalTestBase { ], ], 'relationships' => [ - 'type' => [ - 'data' => [ - 'type' => 'node_type--node_type', - 'id' => 'article', - ], - ], 'field_tags' => [ 'data' => [ [ @@ -618,8 +612,7 @@ class JsonApiFunctionalTest extends JsonApiFunctionalTestBase { 'headers' => ['Content-Type' => 'application/vnd.api+json'], ]); $created_response = Json::decode($response->getBody()->__toString()); - $this->assertEquals(201, $response->getStatusCode()); - $this->assertEquals(0, count($created_response['data']['relationships']['field_tags']['data'])); + $this->assertEquals(404, $response->getStatusCode()); // 6. Decoding error. $response = $this->request('POST', $collection_url, [ 'body' => '{"bad json",,,}', diff --git a/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php b/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php index a7da7b7..ff368a3 100644 --- a/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php +++ b/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php @@ -24,6 +24,7 @@ use Prophecy\Argument; use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * @coversDefaultClass \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer @@ -499,6 +500,7 @@ class JsonApiDocumentTopLevelNormalizerTest extends JsonapiKernelTestBase { [$this->term1->id()], $this->user2->id(), ], + 'taxonomy_term--tags:invalid-uuid', ], // Bad data in user and first tag. [ @@ -510,6 +512,7 @@ class JsonApiDocumentTopLevelNormalizerTest extends JsonapiKernelTestBase { [$this->term1->id()], NULL, ], + 'user--user:also-invalid-uuid', ], ]; @@ -519,11 +522,18 @@ class JsonApiDocumentTopLevelNormalizerTest extends JsonapiKernelTestBase { list($request, $resource_type) = $this->generateProphecies('node', 'article'); $this->container->get('request_stack')->push($request); - $node = $this - ->getNormalizer() - ->denormalize(Json::decode($payload), NULL, 'api_json', [ - 'resource_type' => $resource_type, - ]); + try { + $node = $this + ->getNormalizer() + ->denormalize(Json::decode($payload), NULL, 'api_json', [ + 'resource_type' => $resource_type, + ]); + } + catch (NotFoundHttpException $e) { + $non_existing_resource_identifier = $configuration[2]; + $this->assertEquals("The resource identified by `$non_existing_resource_identifier` (given as a relationship item) could not be found.", $e->getMessage()); + continue; + } /* @var \Drupal\node\Entity\Node $node */ $this->assertInstanceOf('\Drupal\node\Entity\Node', $node);