Skip to content

Commit 5f050fe

Browse files
Rodrigue Villetardteohhanhui
authored andcommitted
inline and add deprecation notice
1 parent 41c9f8d commit 5f050fe

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

src/Bridge/Doctrine/EventListener/WriteListener.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
5858

5959
switch ($request->getMethod()) {
6060
case 'POST':
61-
$event->setControllerResult(
62-
$objectManager->persist($controllerResult) ?? $controllerResult
63-
);
61+
$objectManager->persist($controllerResult);
6462
break;
6563
case 'DELETE':
6664
$objectManager->remove($controllerResult);

src/EventListener/WriteListener.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,13 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
5050
case 'PUT':
5151
case 'PATCH':
5252
case 'POST':
53-
$event->setControllerResult(
54-
$this->dataPersister->persist($controllerResult)
55-
?? $controllerResult
56-
);
53+
$persistResult = $this->dataPersister->persist($controllerResult);
54+
55+
if (null === $persistResult) {
56+
@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);
57+
}
58+
59+
$event->setControllerResult($persistResult ?? $controllerResult);
5760
break;
5861
case 'DELETE':
5962
$this->dataPersister->remove($controllerResult);

src/GraphQl/Resolver/Factory/ItemMutationResolverFactory.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,13 @@ public function __invoke(string $resourceClass = null, string $rootClass = null,
9393
$context += $resourceMetadata->getGraphqlAttribute($operationName, 'denormalization_context', [], true);
9494
$item = $this->normalizer->denormalize($args['input'], $resourceClass, ItemNormalizer::FORMAT, $context);
9595
$this->validate($item, $info, $resourceMetadata, $operationName);
96-
$persistResult = $this->dataPersister->persist($item) ?? $item;
96+
$persistResult = $this->dataPersister->persist($item);
9797

98-
return $this->normalizer->normalize($persistResult, ItemNormalizer::FORMAT, $normalizationContext) + $data;
98+
if (null === $persistResult) {
99+
@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);
100+
}
101+
102+
return $this->normalizer->normalize($persistResult ?? $item, ItemNormalizer::FORMAT, $normalizationContext) + $data;
99103
case 'delete':
100104
if ($item) {
101105
$this->dataPersister->remove($item);

tests/EventListener/WriteListenerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,37 @@ public function testOnKernelViewWithControllerResultAndPersist()
3131
$dummy = new Dummy();
3232
$dummy->setName('Dummyrino');
3333

34+
$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
35+
$dataPersisterProphecy->supports($dummy)->willReturn(true)->shouldBeCalled();
36+
$dataPersisterProphecy->persist($dummy)->willReturn($dummy)->shouldBeCalled();
37+
38+
$request = new Request();
39+
$request->attributes->set('_api_resource_class', Dummy::class);
40+
41+
$event = new GetResponseForControllerResultEvent(
42+
$this->prophesize(HttpKernelInterface::class)->reveal(),
43+
$request,
44+
HttpKernelInterface::MASTER_REQUEST,
45+
$dummy
46+
);
47+
48+
foreach (['PATCH', 'PUT', 'POST'] as $httpMethod) {
49+
$request->setMethod($httpMethod);
50+
51+
(new WriteListener($dataPersisterProphecy->reveal()))->onKernelView($event);
52+
$this->assertSame($dummy, $event->getControllerResult());
53+
}
54+
}
55+
56+
/**
57+
* @group legacy
58+
* @expectedDeprecation Returning void from ApiPlatform\Core\DataPersister\DataPersisterInterface::persist() is deprecated since API Platform 2.3 and will not be supported in API Platform 3, an object should always be returned.
59+
*/
60+
public function testOnKernelViewWithControllerResultAndPersistReturningVoid()
61+
{
62+
$dummy = new Dummy();
63+
$dummy->setName('Dummyrino');
64+
3465
$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
3566
$dataPersisterProphecy->supports($dummy)->willReturn(true)->shouldBeCalled();
3667
$dataPersisterProphecy->persist($dummy)->shouldBeCalled();

0 commit comments

Comments
 (0)