-
-
Notifications
You must be signed in to change notification settings - Fork 960
Deserialize in a view listener #584
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace ApiPlatform\Core\Action; | ||
|
|
||
| /** | ||
| * Empty action. Useful to trigger the kernel.view event without doing anything specific in the action | ||
| * (e.g. the POST action). | ||
| * | ||
| * @author Kévin Dunglas <[email protected]> | ||
| */ | ||
| final class NullAction | ||
| { | ||
| public function __invoke() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. http://symfony.com/doc/current/components/http_kernel/introduction.html#the-kernel-controller-event
Seems like the documentation is not right then?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting, it doesn't throw an exception as highlighted by tests (so there is an error in the doc anyway). However maybe should we return another value than
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is the same dilemma as above. If we zoom out and look at the bigger picture it's a bigger dilemma, which might not have been obvious to us all this while. The
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the |
||
| { | ||
| } | ||
| } | ||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,44 +9,20 @@ | |
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace ApiPlatform\Core\Action; | ||
| namespace ApiPlatform\Core\Api; | ||
|
|
||
| use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; | ||
| use ApiPlatform\Core\Exception\RuntimeException; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
|
||
| /** | ||
| * Checks if the request is properly configured. | ||
| * Extracts data used by the library form a Request instance. | ||
| * | ||
| * @author Kévin Dunglas <[email protected]> | ||
| */ | ||
| trait ActionUtilTrait | ||
| final class RequestAttributesExtractor | ||
| { | ||
| /** | ||
| * Gets an item using the data provider. Throws a 404 error if not found. | ||
| * | ||
| * @param ItemDataProviderInterface $itemDataProvider | ||
| * @param string $resourceClass | ||
| * @param string $operationName | ||
| * @param string|int $id | ||
| * | ||
| * @throws NotFoundHttpException | ||
| * | ||
| * @return object | ||
| */ | ||
| private function getItem(ItemDataProviderInterface $itemDataProvider, string $resourceClass, string $operationName, $id) | ||
| { | ||
| $data = $itemDataProvider->getItem($resourceClass, $id, $operationName, true); | ||
| if (!$data) { | ||
| throw new NotFoundHttpException('Not Found'); | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
|
|
||
| /** | ||
| * Extract resource class, operation name and format request attributes. Throws an exception if the request does not contain required | ||
| * Extracts resource class, operation name and format request attributes. Throws an exception if the request does not contain required | ||
| * attributes. | ||
| * | ||
| * @param Request $request | ||
|
|
@@ -55,7 +31,7 @@ private function getItem(ItemDataProviderInterface $itemDataProvider, string $re | |
| * | ||
| * @return array | ||
| */ | ||
| private function extractAttributes(Request $request) | ||
| public static function extractAttributes(Request $request) | ||
| { | ||
| $resourceClass = $request->attributes->get('_resource_class'); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace ApiPlatform\Core\EventListener; | ||
|
|
||
| use ApiPlatform\Core\Api\RequestAttributesExtractor; | ||
| use ApiPlatform\Core\Exception\RuntimeException; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; | ||
| use Symfony\Component\Serializer\SerializerInterface; | ||
|
|
||
| /** | ||
| * Updates the entity retrieved by the data provider with data contained in the request body. | ||
| * | ||
| * @author Kévin Dunglas <[email protected]> | ||
| */ | ||
| final class DeserializerViewListener | ||
| { | ||
| private $serializer; | ||
|
|
||
| public function __construct(SerializerInterface $serializer) | ||
| { | ||
| $this->serializer = $serializer; | ||
| } | ||
|
|
||
| public function onKernelView(GetResponseForControllerResultEvent $event) | ||
| { | ||
| $data = $event->getControllerResult(); | ||
| $request = $event->getRequest(); | ||
|
|
||
| if ($data instanceof Response || !in_array($request->getMethod(), [Request::METHOD_POST, Request::METHOD_PUT], true)) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| list($resourceClass, $collectionOperation, $itemOperation, $format) = RequestAttributesExtractor::extractAttributes($request); | ||
| } catch (RuntimeException $e) { | ||
| return; | ||
| } | ||
|
|
||
| $context = ['resource_class' => $resourceClass]; | ||
| if ($collectionOperation) { | ||
| $context['collection_operation_name'] = $collectionOperation; | ||
| } else { | ||
| $context['item_operation_name'] = $itemOperation; | ||
| } | ||
|
|
||
| if (null !== $data) { | ||
| $context['object_to_populate'] = $data; | ||
| } | ||
|
|
||
| $event->setControllerResult($this->serializer->deserialize($request->getContent(), $resourceClass, $format, $context)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace ApiPlatform\Core\Tests\Action; | ||
|
|
||
| use ApiPlatform\Core\Action\GetCollectionAction; | ||
| use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
|
|
||
| /** | ||
| * @author Kévin Dunglas <[email protected]> | ||
| */ | ||
| class GetCollectionActionTest extends \PHPUnit_Framework_TestCase | ||
| { | ||
| public function testGetCollection() | ||
| { | ||
| $result = new \stdClass(); | ||
|
|
||
| $dataProviderProphecy = $this->prophesize(CollectionDataProviderInterface::class); | ||
| $dataProviderProphecy->getCollection('Foo', 'get')->willReturn($result); | ||
|
|
||
| $request = new Request([], [], ['_resource_class' => 'Foo', '_collection_operation_name' => 'get', '_api_format' => 'json']); | ||
|
|
||
| $action = new GetCollectionAction($dataProviderProphecy->reveal()); | ||
| $this->assertSame($result, $action($request)); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the value of having this. It's far too easy to create an empty action, so there's no benefit of reuse here. This should just be
PostCollectionAction, as before... And we will be free to change it maybe to do something later on without the name tying it down.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It allows to create custom operations triggering the event system with only config. It's why I've renamed it. Why not doing it now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not go all the way and remove all other actions? 😆
So we will end up with an event listener which fetches data from the data provider, and another which performs the deserialization (if applicable).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the benefit of getting data from a listener (which is triggered at every request) instead of in an action?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. We should probably not try to fight against how Symfony works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The benefit is turtles all the way (kind of... at least, events all the way). And we sidestep the confusion of what the controller is supposed to do by consistently leaving it out of the picture (as a no-op).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we try that in another PR?