Skip to content

Commit b67350b

Browse files
authored
Merge pull request #2412 from antograssiot/avoid-iri-converter
Fix a small BC break
2 parents 999443d + 3abeb59 commit b67350b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/EventListener/WriteListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
6565
// Controller result must be immutable for _api_write_item_iri
6666
// if it's class changed compared to the base class let's avoid calling the IriConverter
6767
// especially that the Output class could be a DTO that's not referencing any route
68-
if (null !== $this->iriConverter && (false !== $attributes['output_class'] ?? null) && \get_class($controllerResult) === \get_class($event->getControllerResult())) {
68+
if (null !== $this->iriConverter && (false !== $attributes['output_class'] ?? null) && $attributes['resource_class'] === ($class = \get_class($controllerResult)) && $class === \get_class($event->getControllerResult())) {
6969
$request->attributes->set('_api_write_item_iri', $this->iriConverter->getIriFromItem($controllerResult));
7070
}
7171
break;

tests/EventListener/WriteListenerTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use ApiPlatform\Core\DataPersister\DataPersisterInterface;
1818
use ApiPlatform\Core\EventListener\WriteListener;
1919
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
20+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyTableInheritance;
21+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyTableInheritanceChild;
2022
use PHPUnit\Framework\TestCase;
2123
use Symfony\Component\HttpFoundation\Request;
2224
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
@@ -261,6 +263,30 @@ public function testOnKernelViewWithNoResourceClass()
261263
(new WriteListener($dataPersisterProphecy->reveal(), $iriConverterProphecy->reveal()))->onKernelView($event);
262264
}
263265

266+
public function testOnKernelViewWithParentResourceClass()
267+
{
268+
$dummy = new DummyTableInheritanceChild();
269+
270+
$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
271+
$dataPersisterProphecy->supports($dummy)->willReturn(true)->shouldBeCalled();
272+
$dataPersisterProphecy->persist($dummy)->willReturn($dummy)->shouldBeCalled();
273+
274+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
275+
$iriConverterProphecy->getIriFromItem($dummy)->shouldNotBeCalled();
276+
277+
$request = new Request([], [], ['_api_resource_class' => DummyTableInheritance::class, '_api_item_operation_name' => 'put', '_api_persist' => true]);
278+
$request->setMethod('PUT');
279+
280+
$event = new GetResponseForControllerResultEvent(
281+
$this->prophesize(HttpKernelInterface::class)->reveal(),
282+
$request,
283+
HttpKernelInterface::MASTER_REQUEST,
284+
$dummy
285+
);
286+
287+
(new WriteListener($dataPersisterProphecy->reveal(), $iriConverterProphecy->reveal()))->onKernelView($event);
288+
}
289+
264290
public function testOnKernelViewWithNoDataPersisterSupport()
265291
{
266292
$dummy = new Dummy();

0 commit comments

Comments
 (0)