Skip to content

Commit 8adbb31

Browse files
authored
Merge pull request #2354 from teohhanhui/fix/serialize-listener-killswitch
Fix SerializeListener to respect _api_respond request attribute
2 parents 19800ac + 02e84b6 commit 8adbb31

File tree

4 files changed

+28
-54
lines changed

4 files changed

+28
-54
lines changed

src/EventListener/RespondListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
3636
$controllerResult = $event->getControllerResult();
3737
$request = $event->getRequest();
3838

39-
if ($controllerResult instanceof Response || !$request->attributes->get('_api_respond')) {
39+
if ($controllerResult instanceof Response || !$request->attributes->getBoolean('_api_respond', true)) {
4040
return;
4141
}
4242

src/EventListener/SerializeListener.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
4747
$controllerResult = $event->getControllerResult();
4848
$request = $event->getRequest();
4949

50-
if ($controllerResult instanceof Response) {
50+
if ($controllerResult instanceof Response || !$request->attributes->getBoolean('_api_respond', true)) {
5151
return;
5252
}
5353

@@ -80,10 +80,6 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
8080
*/
8181
private function serializeRawData(GetResponseForControllerResultEvent $event, Request $request, $controllerResult)
8282
{
83-
if (!$request->attributes->get('_api_respond')) {
84-
return;
85-
}
86-
8783
if (\is_object($controllerResult)) {
8884
$event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat(), $request->attributes->get('_api_normalization_context', [])));
8985

tests/EventListener/RespondListenerTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\Core\EventListener\RespondListener;
1717
use PHPUnit\Framework\TestCase;
18+
use Prophecy\Argument;
1819
use Symfony\Component\HttpFoundation\Request;
1920
use Symfony\Component\HttpFoundation\Response;
2021
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
@@ -28,11 +29,24 @@ class RespondListenerTest extends TestCase
2829
public function testDoNotHandleResponse()
2930
{
3031
$request = new Request();
31-
$request->setRequestFormat('xml');
3232

3333
$eventProphecy = $this->prophesize(GetResponseForControllerResultEvent::class);
34-
$eventProphecy->getControllerResult()->willReturn(new Response())->shouldBeCalled();
35-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
34+
$eventProphecy->getControllerResult()->willReturn(new Response());
35+
$eventProphecy->getRequest()->willReturn($request);
36+
$eventProphecy->setResponse(Argument::any())->shouldNotBeCalled();
37+
38+
$listener = new RespondListener();
39+
$listener->onKernelView($eventProphecy->reveal());
40+
}
41+
42+
public function testDoNotHandleWhenRespondFlagIsFalse()
43+
{
44+
$request = new Request([], [], ['_api_respond' => false]);
45+
46+
$eventProphecy = $this->prophesize(GetResponseForControllerResultEvent::class);
47+
$eventProphecy->getControllerResult()->willReturn('foo');
48+
$eventProphecy->getRequest()->willReturn($request);
49+
$eventProphecy->setResponse(Argument::any())->shouldNotBeCalled();
3650

3751
$listener = new RespondListener();
3852
$listener->onKernelView($eventProphecy->reveal());

tests/EventListener/SerializeListenerTest.php

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class SerializeListenerTest extends TestCase
3232
public function testDoNotSerializeResponse()
3333
{
3434
$serializerProphecy = $this->prophesize(SerializerInterface::class);
35-
$serializerProphecy->serialize()->shouldNotBeCalled();
35+
$serializerProphecy->serialize(Argument::cetera())->shouldNotBeCalled();
3636

3737
$request = new Request();
3838
$request->setRequestFormat('xml');
@@ -42,61 +42,25 @@ public function testDoNotSerializeResponse()
4242
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
4343

4444
$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
45-
$serializerContextBuilderProphecy->createFromRequest()->shouldNotBeCalled();
45+
$serializerContextBuilderProphecy->createFromRequest(Argument::cetera())->shouldNotBeCalled();
4646

4747
$listener = new SerializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal());
4848
$listener->onKernelView($eventProphecy->reveal());
4949
}
5050

51-
public function testDoNotSerializeWhenFormatNotSet()
51+
public function testDoNotSerializeWhenRespondFlagIsFalse()
5252
{
5353
$serializerProphecy = $this->prophesize(SerializerInterface::class);
54-
$serializerProphecy->serialize()->shouldNotBeCalled();
55-
56-
$eventProphecy = $this->prophesize(GetResponseForControllerResultEvent::class);
57-
$eventProphecy->getControllerResult()->willReturn(new \stdClass())->shouldBeCalled();
58-
$eventProphecy->getRequest()->willReturn(new Request())->shouldBeCalled();
59-
60-
$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
61-
$serializerContextBuilderProphecy->createFromRequest()->shouldNotBeCalled();
62-
63-
$listener = new SerializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal());
64-
$listener->onKernelView($eventProphecy->reveal());
65-
}
66-
67-
public function testDoNotSerializeWhenResourceClassNotSet()
68-
{
69-
$serializerProphecy = $this->prophesize(SerializerInterface::class);
70-
$serializerProphecy->serialize()->shouldNotBeCalled();
71-
72-
$request = new Request([], [], ['_api_collection_operation_name' => 'get']);
73-
$request->setRequestFormat('xml');
74-
75-
$eventProphecy = $this->prophesize(GetResponseForControllerResultEvent::class);
76-
$eventProphecy->getControllerResult()->willReturn(new \stdClass())->shouldBeCalled();
77-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
54+
$serializerProphecy->serialize(Argument::cetera())->shouldNotBeCalled();
7855

7956
$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
80-
$serializerContextBuilderProphecy->createFromRequest()->shouldNotBeCalled();
8157

82-
$listener = new SerializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal());
83-
$listener->onKernelView($eventProphecy->reveal());
84-
}
85-
86-
public function testDoNotSerializeWhenOperationNotSet()
87-
{
88-
$serializerProphecy = $this->prophesize(SerializerInterface::class);
89-
$serializerProphecy->serialize()->shouldNotBeCalled();
90-
91-
$request = new Request([], [], ['_api_resource_class' => 'Foo']);
92-
$request->setRequestFormat('xml');
58+
$request = new Request([], [], ['_api_respond' => false]);
9359

9460
$eventProphecy = $this->prophesize(GetResponseForControllerResultEvent::class);
95-
$eventProphecy->getControllerResult()->willReturn(new \stdClass())->shouldBeCalled();
96-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
97-
98-
$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
99-
$serializerContextBuilderProphecy->createFromRequest()->shouldNotBeCalled();
61+
$eventProphecy->getControllerResult()->willReturn(new \stdClass());
62+
$eventProphecy->getRequest()->willReturn($request);
63+
$eventProphecy->setControllerResult(Argument::any())->shouldNotBeCalled();
10064

10165
$listener = new SerializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal());
10266
$listener->onKernelView($eventProphecy->reveal());
@@ -178,7 +142,7 @@ public function testEncode()
178142
$serializerProphecy = $this->prophesize(SerializerInterface::class);
179143
$serializerProphecy->willImplement(EncoderInterface::class);
180144
$serializerProphecy->encode(Argument::any(), 'xml')->willReturn('bar')->shouldBeCalled();
181-
$serializerProphecy->serialize()->shouldNotBeCalled();
145+
$serializerProphecy->serialize(Argument::cetera())->shouldNotBeCalled();
182146

183147
$request = new Request([], [], ['_api_respond' => true]);
184148
$request->setRequestFormat('xml');

0 commit comments

Comments
 (0)