Skip to content

Commit 980ee04

Browse files
committed
fix(symfony): catch identifiers exceptions in iri converter
1 parent 20fbd06 commit 980ee04

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Symfony/Routing/IriConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function getIriFromResource($item, int $referenceType = UrlGeneratorInter
157157
if (\is_object($item)) {
158158
try {
159159
$identifiers = $this->identifiersExtractor->getIdentifiersFromItem($item, $operation);
160-
} catch (RuntimeException $e) {
160+
} catch (InvalidArgumentException|RuntimeException $e) {
161161
// We can try using context uri variables if any
162162
if (!$identifiers) {
163163
throw new InvalidArgumentException(sprintf('Unable to generate an IRI for the item of type "%s"', $resourceClass), $e->getCode(), $e);

tests/Symfony/Routing/IriConverterTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use ApiPlatform\Api\UrlGeneratorInterface;
1919
use ApiPlatform\Core\Api\IriConverterInterface;
2020
use ApiPlatform\Core\Tests\ProphecyTrait;
21+
use ApiPlatform\Exception\InvalidArgumentException;
2122
use ApiPlatform\Exception\RuntimeException;
2223
use ApiPlatform\Metadata\ApiResource;
2324
use ApiPlatform\Metadata\Get;
@@ -124,6 +125,25 @@ public function testGetIriFromItemWithNoOperations()
124125
$iriConverter->getIriFromResource($item);
125126
}
126127

128+
public function testGetIriFromItemWithBadIdentifiers()
129+
{
130+
$this->expectExceptionMessage(sprintf('Unable to generate an IRI for the item of type "%s"', Dummy::class));
131+
132+
$item = new Dummy();
133+
$item->setId(1);
134+
135+
$resourceMetadataCollectionFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
136+
$resourceMetadataCollectionFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn(new ResourceMetadataCollection(Dummy::class, [
137+
(new ApiResource())->withOperations(new Operations()),
138+
]));
139+
140+
$identifiersExtractorProphecy = $this->prophesize(IdentifiersExtractorInterface::class);
141+
$identifiersExtractorProphecy->getIdentifiersFromItem($item, Argument::type(HttpOperation::class))->willThrow(InvalidArgumentException::class);
142+
143+
$iriConverter = $this->getIriConverter(null, null, $identifiersExtractorProphecy, $resourceMetadataCollectionFactoryProphecy);
144+
$iriConverter->getIriFromResource($item);
145+
}
146+
127147
public function testGetCollectionIri()
128148
{
129149
$operationName = 'operation_name';

0 commit comments

Comments
 (0)