Skip to content

Commit f9707ec

Browse files
authored
Merge pull request #905 from Simperfit/feature/fix-api-platform-198
fix: add test to reproduce && fix the issue using wrong relation IRI
2 parents be4e0c1 + 23baf42 commit f9707ec

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

features/main/relation.feature

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,18 @@ Feature: Relations support
303303
And the response should be in JSON
304304
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
305305

306+
Scenario: Post a relation with a not existing IRI
307+
When I add "Content-Type" header equal to "application/ld+json"
308+
And I send a "POST" request to "/relation_embedders" with body:
309+
"""
310+
{
311+
"related": "/related_dummies/123"
312+
}
313+
"""
314+
Then the response status code should be 400
315+
And the response should be in JSON
316+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
317+
306318
@dropSchema
307319
Scenario: Update an embedded relation
308320
When I add "Content-Type" header equal to "application/ld+json"

src/Bridge/Symfony/Routing/IriConverter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use ApiPlatform\Core\Api\UrlGeneratorInterface;
1616
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
1717
use ApiPlatform\Core\Exception\InvalidArgumentException;
18+
use ApiPlatform\Core\Exception\ItemNotFoundException;
1819
use ApiPlatform\Core\Exception\RuntimeException;
1920
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2021
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
@@ -69,7 +70,7 @@ public function getItemFromIri(string $iri, array $context = [])
6970
return $item;
7071
}
7172

72-
throw new InvalidArgumentException(sprintf('Item not found for "%s".', $iri));
73+
throw new ItemNotFoundException(sprintf('Item not found for "%s".', $iri));
7374
}
7475

7576
/**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ApiPlatform\Core\Exception;
13+
14+
/**
15+
* Item not found exception.
16+
*
17+
* @author Amrouche Hamza <[email protected]>
18+
*/
19+
class ItemNotFoundException extends InvalidArgumentException
20+
{
21+
}

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function denormalize($data, $class, $format = null, array $context = [])
9696
throw new InvalidArgumentException('Update is not allowed for this operation.');
9797
}
9898

99-
$context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['@id'], $context + ['fetch_data' => false]);
99+
$context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['@id'], $context + ['fetch_data' => true]);
100100
}
101101

102102
return parent::denormalize($data, $class, $format, $context);

src/Serializer/AbstractItemNormalizer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use ApiPlatform\Core\Api\IriConverterInterface;
1515
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
1616
use ApiPlatform\Core\Exception\InvalidArgumentException;
17+
use ApiPlatform\Core\Exception\ItemNotFoundException;
1718
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
1819
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
1920
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
@@ -273,7 +274,9 @@ private function denormalizeRelation(string $attributeName, PropertyMetadata $pr
273274
{
274275
if (is_string($value)) {
275276
try {
276-
return $this->iriConverter->getItemFromIri($value, $context + ['fetch_data' => false]);
277+
return $this->iriConverter->getItemFromIri($value, $context + ['fetch_data' => true]);
278+
} catch (ItemNotFoundException $e) {
279+
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
277280
} catch (InvalidArgumentException $e) {
278281
// Give a chance to other normalizers (e.g.: DateTimeNormalizer)
279282
}

0 commit comments

Comments
 (0)