Skip to content

Commit a3b62e8

Browse files
thomasglachantsoyuka
authored andcommitted
Authorize empty content put in custom operations (#1274)
* Authorize empty request content for PUT in a custom action * Apply php-cs-fixer * Apply review changes * Apply review changes * Switch to fastest alternative. Add behat test for with empty body on PUT. * Refacto after dunglas review
1 parent acec7dd commit a3b62e8

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

features/main/crud.feature

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,41 @@ Feature: Create-Retrieve-Update-Delete
395395
}
396396
"""
397397

398+
Scenario: Update a resource with empty body
399+
When I add "Content-Type" header equal to "application/ld+json"
400+
And I send a "PUT" request to "/dummies/1"
401+
Then the response status code should be 200
402+
And the response should be in JSON
403+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
404+
And the JSON should be equal to:
405+
"""
406+
{
407+
"@context": "/contexts/Dummy",
408+
"@id": "/dummies/1",
409+
"@type": "Dummy",
410+
"description": null,
411+
"dummy": null,
412+
"dummyBoolean": null,
413+
"dummyDate": "2015-03-01T10:00:00+00:00",
414+
"dummyFloat": null,
415+
"dummyPrice": null,
416+
"relatedDummy": null,
417+
"relatedDummies": [],
418+
"jsonData": [
419+
{
420+
"key": "value1"
421+
},
422+
{
423+
"key": "value2"
424+
}
425+
],
426+
"name_converted": null,
427+
"id": 1,
428+
"name": "A nice dummy",
429+
"alias": null
430+
}
431+
"""
432+
398433
@dropSchema
399434
Scenario: Delete a resource
400435
When I send a "DELETE" request to "/dummies/1"

src/EventListener/DeserializeListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function onKernelRequest(GetResponseEvent $event)
5151
|| $request->isMethod(Request::METHOD_DELETE)
5252
|| !($attributes = RequestAttributesExtractor::extractAttributes($request))
5353
|| !$attributes['receive']
54+
|| ('' === ($requestContent = $request->getContent()) && $request->isMethod(Request::METHOD_PUT))
5455
) {
5556
return;
5657
}
@@ -66,7 +67,7 @@ public function onKernelRequest(GetResponseEvent $event)
6667
$request->attributes->set(
6768
'data',
6869
$this->serializer->deserialize(
69-
$request->getContent(), $attributes['resource_class'], $format, $context
70+
$requestContent, $attributes['resource_class'], $format, $context
7071
)
7172
);
7273
}

tests/EventListener/DeserializeListenerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ public function testDoNotCallWhenRequestMethodIsSafe()
4545
$listener->onKernelRequest($eventProphecy->reveal());
4646
}
4747

48+
public function testDoNotCallWhenPutAndEmptyRequestContent()
49+
{
50+
$eventProphecy = $this->prophesize(GetResponseEvent::class);
51+
52+
$request = new Request([], [], ['data' => new \stdClass(), '_api_resource_class' => 'Foo', '_api_item_operation_name' => 'put'], [], [], [], '');
53+
$request->setMethod(Request::METHOD_PUT);
54+
$request->headers->set('Content-Type', 'application/json');
55+
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
56+
57+
$serializerProphecy = $this->prophesize(SerializerInterface::class);
58+
$serializerProphecy->deserialize()->shouldNotBeCalled();
59+
60+
$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
61+
$serializerContextBuilderProphecy->createFromRequest(Argument::type(Request::class), false, Argument::type('array'))->shouldNotBeCalled();
62+
63+
$listener = new DeserializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal(), self::FORMATS);
64+
$listener->onKernelRequest($eventProphecy->reveal());
65+
}
66+
4867
public function testDoNotCallWhenRequestNotManaged()
4968
{
5069
$eventProphecy = $this->prophesize(GetResponseEvent::class);

0 commit comments

Comments
 (0)