-
-
Notifications
You must be signed in to change notification settings - Fork 966
Add the ability to declare empty item operations #3079
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,29 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Core\Action; | ||
|
|
||
| use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
|
||
| /** | ||
| * Not found action. | ||
| * | ||
| * @author Antoine Bluchet <soyuka@gmail.com> | ||
| */ | ||
| final class NotFoundAction | ||
| { | ||
| public function __invoke() | ||
| { | ||
| throw new NotFoundHttpException(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |||||||
|
|
||||||||
| namespace ApiPlatform\Core\Metadata\Resource\Factory; | ||||||||
|
|
||||||||
| use ApiPlatform\Core\Action\NotFoundAction; | ||||||||
| use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; | ||||||||
|
|
||||||||
| /** | ||||||||
|
|
@@ -81,6 +82,13 @@ public function create(string $resourceClass): ResourceMetadata | |||||||
| $resourceMetadata = $this->normalize(false, $resourceClass, $resourceMetadata, $itemOperations); | ||||||||
| } | ||||||||
|
|
||||||||
| if ($this->needsDefaultGetOperation($resourceMetadata)) { | ||||||||
| $resourceMetadata = $resourceMetadata->withItemOperations(array_merge( | ||||||||
| $resourceMetadata->getItemOperations(), | ||||||||
| ['get' => ['method' => 'GET', 'read' => false, 'output' => ['class' => false], 'controller' => NotFoundAction::class]]) | ||||||||
|
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 believe you've made a mistake here, it's supposed to be
|
||||||||
| ); | ||||||||
| } | ||||||||
|
|
||||||||
| $graphql = $resourceMetadata->getGraphql(); | ||||||||
| if (null === $graphql) { | ||||||||
| $resourceMetadata = $resourceMetadata->withGraphql(['item_query' => [], 'collection_query' => [], 'delete' => [], 'update' => [], 'create' => []]); | ||||||||
|
|
@@ -148,4 +156,17 @@ private function normalizeGraphQl(ResourceMetadata $resourceMetadata, array $ope | |||||||
|
|
||||||||
| return $resourceMetadata->withGraphql($operations); | ||||||||
| } | ||||||||
|
|
||||||||
| private function needsDefaultGetOperation(ResourceMetadata $resourceMetadata): bool | ||||||||
| { | ||||||||
| $itemOperations = $resourceMetadata->getItemOperations(); | ||||||||
|
|
||||||||
| foreach ($itemOperations as $itemOperation) { | ||||||||
| if ('GET' === ($itemOperation['method'] ?? false)) { | ||||||||
|
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. Why not
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. Actually, on second thought, this check is unsafe. There might be a custom The only safe option seems to me to be for the user to declare this /**
* @ApiResource(
* itemOperations={
* "get"={
* "method"="GET",
* "controller"=NotFoundAction::class,
* "read"=false,
* "output"=false,
* },
* },
* )
*/Which to me seems like we should revert and make this a doc entry instead, while providing 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. We could document the custom get operation with one that disables it! I'm fixing what you said!
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.
What do you mean? Anyway, I've given it more thought and it's still unsafe anyway. We don't know that there's not already a
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.
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. you said it yourself it's not recommended to use
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. Yes, but if there's an operation using |
||||||||
| return false; | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| return true; | ||||||||
| } | ||||||||
| } | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Document; | ||
|
|
||
| use ApiPlatform\Core\Annotation\ApiResource; | ||
| use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
|
|
||
| /** | ||
| * DisableItemOperation. | ||
| * | ||
| * @author Antoine Bluchet <soyuka@gmail.com> | ||
| * | ||
| * @ApiResource(itemOperations={}, collectionOperations={"get"}) | ||
| * @ODM\Document | ||
| */ | ||
| class DisableItemOperation | ||
| { | ||
| /** | ||
| * @ODM\Id(strategy="INCREMENT", type="integer") | ||
| */ | ||
| private $id; | ||
|
|
||
| /** | ||
| * @var string The dummy name | ||
| * | ||
| * @ODM\Field | ||
| */ | ||
| public $name; | ||
|
|
||
| public function getId() | ||
| { | ||
| return $this->id; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity; | ||
|
|
||
| use ApiPlatform\Core\Annotation\ApiResource; | ||
| use Doctrine\ORM\Mapping as ORM; | ||
|
|
||
| /** | ||
| * DisableItemOperation. | ||
| * | ||
| * @author Antoine Bluchet <soyuka@gmail.com> | ||
| * | ||
| * @ApiResource(itemOperations={}, collectionOperations={"get", "post"}) | ||
| * @ORM\Entity | ||
| */ | ||
| class DisableItemOperation | ||
| { | ||
| /** | ||
| * @var int The id | ||
| * | ||
| * @ORM\Column(type="integer", nullable=true) | ||
| * @ORM\Id | ||
| * @ORM\GeneratedValue(strategy="AUTO") | ||
| */ | ||
| private $id; | ||
|
|
||
| /** | ||
| * @var string The dummy name | ||
| * | ||
| * @ORM\Column | ||
| */ | ||
| public $name; | ||
|
|
||
| public function getId() | ||
| { | ||
| return $this->id; | ||
| } | ||
| } |
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.
Should be reverted. See https://github.com/api-platform/core/pull/3079/files#r327557730