Skip to content

Commit

Permalink
Allow a POST request to have an empty body
Browse files Browse the repository at this point in the history
  • Loading branch information
gorghoa committed Apr 6, 2018
1 parent c11f03a commit a449fb2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/EventListener/DeserializeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ public function __construct(SerializerInterface $serializer, SerializerContextBu
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$method = $request->getMethod();
if (
$request->isMethodSafe(false)
|| $request->isMethod('DELETE')
|| 'DELETE' === $method
|| !($attributes = RequestAttributesExtractor::extractAttributes($request))
|| !$attributes['receive']
|| ('' === ($requestContent = $request->getContent()) && $request->isMethod('PUT'))
|| (
'' === ($requestContent = $request->getContent())
&& ('POST' === $method || 'PUT' === $method)
)
) {
return;
}
Expand Down
12 changes: 10 additions & 2 deletions tests/EventListener/DeserializeListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ public function testDoNotCallWhenRequestMethodIsSafe()
$listener->onKernelRequest($eventProphecy->reveal());
}

public function testDoNotCallWhenPutAndEmptyRequestContent()
/**
* @dataProvider allowedEmptyRequestMethodsProvider
*/
public function testDoNotCallWhenSendingAndEmptyRequestContent($method)
{
$eventProphecy = $this->prophesize(GetResponseEvent::class);

$request = new Request([], [], ['data' => new \stdClass(), '_api_resource_class' => 'Foo', '_api_item_operation_name' => 'put'], [], [], [], '');
$request->setMethod('PUT');
$request->setMethod($method);
$request->headers->set('Content-Type', 'application/json');
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();

Expand All @@ -66,6 +69,11 @@ public function testDoNotCallWhenPutAndEmptyRequestContent()
$listener->onKernelRequest($eventProphecy->reveal());
}

public function allowedEmptyRequestMethodsProvider()
{
return [['PUT'], ['POST']];
}

public function testDoNotCallWhenRequestNotManaged()
{
$eventProphecy = $this->prophesize(GetResponseEvent::class);
Expand Down

0 comments on commit a449fb2

Please sign in to comment.