Skip to content

Commit 2ab5c4f

Browse files
committed
always try to provide a _api_write_item_iri/Content-Location for controller results
1 parent 4a79bdd commit 2ab5c4f

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

src/EventListener/WriteListener.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
6565

6666
$event->setControllerResult($persistResult ?? $controllerResult);
6767

68-
// Controller result must be immutable for _api_write_item_iri
69-
// if it's class changed compared to the base class let's avoid calling the IriConverter
70-
// especially that the Output class could be a DTO that's not referencing any route
7168
if (null === $this->iriConverter) {
7269
return;
7370
}
@@ -79,8 +76,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
7976
$hasOutput = \array_key_exists('class', $outputMetadata) && null !== $outputMetadata['class'];
8077
}
8178

82-
$class = \get_class($controllerResult);
83-
if ($hasOutput && $attributes['resource_class'] === $class && $class === \get_class($event->getControllerResult())) {
79+
if ($hasOutput) {
8480
$request->attributes->set('_api_write_item_iri', $this->iriConverter->getIriFromItem($controllerResult));
8581
}
8682
break;

tests/EventListener/WriteListenerTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
use ApiPlatform\Core\EventListener\WriteListener;
1919
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
2020
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
21+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ConcreteDummy;
2122
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
22-
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyTableInheritance;
23-
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyTableInheritanceChild;
2423
use PHPUnit\Framework\TestCase;
2524
use Symfony\Component\HttpFoundation\Request;
2625
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
@@ -270,16 +269,16 @@ public function testOnKernelViewWithNoResourceClass()
270269

271270
public function testOnKernelViewWithParentResourceClass()
272271
{
273-
$dummy = new DummyTableInheritanceChild();
272+
$dummy = new ConcreteDummy();
274273

275274
$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
276275
$dataPersisterProphecy->supports($dummy)->willReturn(true)->shouldBeCalled();
277276
$dataPersisterProphecy->persist($dummy)->willReturn($dummy)->shouldBeCalled();
278277

279278
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
280-
$iriConverterProphecy->getIriFromItem($dummy)->shouldNotBeCalled();
279+
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummy/1')->shouldBeCalled();
281280

282-
$request = new Request([], [], ['_api_resource_class' => DummyTableInheritance::class, '_api_item_operation_name' => 'put', '_api_persist' => true]);
281+
$request = new Request([], [], ['_api_resource_class' => ConcreteDummy::class, '_api_item_operation_name' => 'put', '_api_persist' => true]);
283282
$request->setMethod('PUT');
284283

285284
$event = new GetResponseForControllerResultEvent(

0 commit comments

Comments
 (0)