Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authorize empty content put in custom operations #1274

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions features/main/crud.feature
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,41 @@ Feature: Create-Retrieve-Update-Delete
}
"""

Scenario: Update a resource with empty body
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dunglas Do you think it's necessary to add this test ? I'm not convinced ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no useless integration test ;).

When I add "Content-Type" header equal to "application/ld+json"
And I send a "PUT" request to "/dummies/1"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be equal to:
"""
{
"@context": "/contexts/Dummy",
"@id": "/dummies/1",
"@type": "Dummy",
"description": null,
"dummy": null,
"dummyBoolean": null,
"dummyDate": "2015-03-01T10:00:00+00:00",
"dummyFloat": null,
"dummyPrice": null,
"relatedDummy": null,
"relatedDummies": [],
"jsonData": [
{
"key": "value1"
},
{
"key": "value2"
}
],
"name_converted": null,
"id": 1,
"name": "A nice dummy",
"alias": null
}
"""

@dropSchema
Scenario: Delete a resource
When I send a "DELETE" request to "/dummies/1"
Expand Down
3 changes: 2 additions & 1 deletion src/EventListener/DeserializeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function onKernelRequest(GetResponseEvent $event)
|| $request->isMethod(Request::METHOD_DELETE)
|| !($attributes = RequestAttributesExtractor::extractAttributes($request))
|| !$attributes['receive']
|| ('' === ($requestContent = $request->getContent()) && $request->isMethod(Request::METHOD_PUT))
) {
return;
}
Expand All @@ -66,7 +67,7 @@ public function onKernelRequest(GetResponseEvent $event)
$request->attributes->set(
'data',
$this->serializer->deserialize(
$request->getContent(), $attributes['resource_class'], $format, $context
$requestContent, $attributes['resource_class'], $format, $context
)
);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/EventListener/DeserializeListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ public function testDoNotCallWhenRequestMethodIsSafe()
$listener->onKernelRequest($eventProphecy->reveal());
}

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

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

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->deserialize()->shouldNotBeCalled();

$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
$serializerContextBuilderProphecy->createFromRequest(Argument::type(Request::class), false, Argument::type('array'))->shouldNotBeCalled();

$listener = new DeserializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal(), self::FORMATS);
$listener->onKernelRequest($eventProphecy->reveal());
}

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