Skip to content

Commit 431cf6f

Browse files
Tjeerdteohhanhui
authored andcommitted
Make sure to always use the persistResult for follow-up actions
Not using the persistResult could cause issues in generating the IRI for new resources because the ID was not set on the controllerResult but only on the persistResult for example.
1 parent 42e0bdc commit 431cf6f

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/EventListener/WriteListener.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ public function onKernelView(GetResponseForControllerResultEvent $event): void
6161

6262
if (null === $persistResult) {
6363
@trigger_error(sprintf('Returning void from %s::persist() is deprecated since API Platform 2.3 and will not be supported in API Platform 3, an object should always be returned.', DataPersisterInterface::class), E_USER_DEPRECATED);
64+
} else {
65+
$controllerResult = $persistResult;
66+
$event->setControllerResult($controllerResult);
6467
}
6568

66-
$event->setControllerResult($persistResult ?? $controllerResult);
67-
6869
if (null === $this->iriConverter) {
6970
return;
7071
}

tests/EventListener/WriteListenerTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,25 +95,25 @@ public function testOnKernelViewWithControllerResultAndPersistReturningVoid()
9595

9696
/**
9797
* @see https://github.com/api-platform/core/issues/1799
98+
* @see https://github.com/api-platform/core/issues/2692
9899
*/
99100
public function testOnKernelViewWithControllerResultAndPersistWithImmutableResource()
100101
{
101102
$dummy = new Dummy();
102103
$dummy->setName('Dummyrino');
103104

104105
$dummy2 = new Dummy();
106+
$dummy2->setId(2);
105107
$dummy2->setName('Dummyferoce');
106108

107109
$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
108-
$dataPersisterProphecy->supports($dummy, Argument::type('array'))->willReturn(true)->shouldBeCalled();
110+
$dataPersisterProphecy->supports($dummy, Argument::type('array'))->willReturn(true);
111+
$dataPersisterProphecy->persist($dummy, Argument::type('array'))->willReturn($dummy2);
109112

110113
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
111-
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummy/1')->shouldBeCalled();
114+
$iriConverterProphecy->getIriFromItem($dummy2)->willReturn('/dummy/2');
112115

113-
$dataPersisterProphecy
114-
->persist($dummy, Argument::type('array'))
115-
->willReturn($dummy2) // Persist is not mutating $dummy, but return a brand new technically unrelated object instead
116-
->shouldBeCalled();
116+
$writeListener = new WriteListener($dataPersisterProphecy->reveal(), $iriConverterProphecy->reveal());
117117

118118
$request = new Request([], [], ['_api_resource_class' => Dummy::class]);
119119

@@ -128,10 +128,10 @@ public function testOnKernelViewWithControllerResultAndPersistWithImmutableResou
128128
$request->setMethod($httpMethod);
129129
$request->attributes->set(sprintf('_api_%s_operation_name', 'POST' === $httpMethod ? 'collection' : 'item'), strtolower($httpMethod));
130130

131-
(new WriteListener($dataPersisterProphecy->reveal(), $iriConverterProphecy->reveal()))->onKernelView($event);
131+
$writeListener->onKernelView($event);
132132

133133
$this->assertSame($dummy2, $event->getControllerResult());
134-
$this->assertEquals('/dummy/1', $request->attributes->get('_api_write_item_iri'));
134+
$this->assertEquals('/dummy/2', $request->attributes->get('_api_write_item_iri'));
135135
}
136136
}
137137

0 commit comments

Comments
 (0)