Skip to content

Commit

Permalink
Add the ability to declare empty item operations
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Sep 27, 2019
1 parent 115cf92 commit ba2057a
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 13 deletions.
8 changes: 8 additions & 0 deletions features/main/operation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ Feature: Operation support
}
}
"""

Scenario: Get the collection of a resource that have disabled item operation
When I send a "GET" request to "/disable_item_operations"
Then the response status code should be 200

Scenario: Get a 404 response for the disabled item operation
When I send a "GET" request to "/disable_item_operations/1"
Then the response status code should be 404
29 changes: 29 additions & 0 deletions src/Action/NotFoundAction.php
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();
}
}
2 changes: 2 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@
<service id="api_platform.action.put_item" alias="api_platform.action.placeholder" public="true" />
<service id="api_platform.action.delete_item" alias="api_platform.action.placeholder" public="true" />
<service id="api_platform.action.get_subresource" alias="api_platform.action.placeholder" public="true" />
<service id="api_platform.action.not_found" class="ApiPlatform\Core\Action\NotFoundAction" public="true" />
<service id="ApiPlatform\Core\Action\NotFoundAction" alias="api_platform.action.not_found" public="true" />

<service id="api_platform.action.entrypoint" class="ApiPlatform\Core\Action\EntrypointAction" public="true">
<argument type="service" id="api_platform.metadata.resource.name_collection_factory" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\Core\Tests\Bridge\Symfony\Bundle\DependencyInjection;

use ApiPlatform\Core\Action\NotFoundAction;
use ApiPlatform\Core\Api\FilterInterface;
use ApiPlatform\Core\Api\IdentifiersExtractorInterface;
use ApiPlatform\Core\Api\IriConverterInterface;
Expand Down Expand Up @@ -843,6 +844,7 @@ private function getPartialContainerBuilderProphecy()
'api_platform.action.documentation',
'api_platform.action.entrypoint',
'api_platform.action.exception',
'api_platform.action.not_found',
'api_platform.action.placeholder',
'api_platform.cache.identifiers_extractor',
'api_platform.cache.metadata.property',
Expand Down Expand Up @@ -938,23 +940,24 @@ private function getPartialContainerBuilderProphecy()
'api_platform.property_accessor' => 'property_accessor',
'api_platform.property_info' => 'property_info',
'api_platform.serializer' => 'serializer',
Pagination::class => 'api_platform.pagination',
IriConverterInterface::class => 'api_platform.iri_converter',
UrlGeneratorInterface::class => 'api_platform.router',
SerializerContextBuilderInterface::class => 'api_platform.serializer.context_builder',
CollectionDataProviderInterface::class => 'api_platform.collection_data_provider',
ItemDataProviderInterface::class => 'api_platform.item_data_provider',
SubresourceDataProviderInterface::class => 'api_platform.subresource_data_provider',
DataPersisterInterface::class => 'api_platform.data_persister',
ResourceNameCollectionFactoryInterface::class => 'api_platform.metadata.resource.name_collection_factory',
ResourceMetadataFactoryInterface::class => 'api_platform.metadata.resource.metadata_factory',
GroupFilter::class => 'api_platform.serializer.group_filter',
IdentifiersExtractorInterface::class => 'api_platform.identifiers_extractor.cached',
IriConverterInterface::class => 'api_platform.iri_converter',
ItemDataProviderInterface::class => 'api_platform.item_data_provider',
NotFoundAction::class => 'api_platform.action.not_found',
OperationAwareFormatsProviderInterface::class => 'api_platform.formats_provider',
Pagination::class => 'api_platform.pagination',
PropertyFilter::class => 'api_platform.serializer.property_filter',
PropertyNameCollectionFactoryInterface::class => 'api_platform.metadata.property.name_collection_factory',
PropertyMetadataFactoryInterface::class => 'api_platform.metadata.property.metadata_factory',
ResourceClassResolverInterface::class => 'api_platform.resource_class_resolver',
PropertyFilter::class => 'api_platform.serializer.property_filter',
GroupFilter::class => 'api_platform.serializer.group_filter',
OperationAwareFormatsProviderInterface::class => 'api_platform.formats_provider',
IdentifiersExtractorInterface::class => 'api_platform.identifiers_extractor.cached',
ResourceNameCollectionFactoryInterface::class => 'api_platform.metadata.resource.name_collection_factory',
ResourceMetadataFactoryInterface::class => 'api_platform.metadata.resource.metadata_factory',
SerializerContextBuilderInterface::class => 'api_platform.serializer.context_builder',
SubresourceDataProviderInterface::class => 'api_platform.subresource_data_provider',
UrlGeneratorInterface::class => 'api_platform.router',
];

foreach ($aliases as $alias => $service) {
Expand Down
52 changes: 52 additions & 0 deletions tests/Fixtures/TestBundle/Document/DisableItemOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\Action\NotFoundAction;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
* DisableItemOperation.
*
* @author Antoine Bluchet <soyuka@gmail.com>
*
* @ApiResource(itemOperations={
* "get"={
* "read"=false,
* "output"=false,
* "controller"=NotFoundAction::class
* }
* }, 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;
}
}
56 changes: 56 additions & 0 deletions tests/Fixtures/TestBundle/Entity/DisableItemOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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\Action\NotFoundAction;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;

/**
* DisableItemOperation.
*
* @author Antoine Bluchet <soyuka@gmail.com>
*
* @ApiResource(itemOperations={
* "get"={
* "read"=false,
* "output"=false,
* "controller"=NotFoundAction::class
* }
* }, collectionOperations={"get"})
* @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;
}
}
2 changes: 1 addition & 1 deletion tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function testExecuteWithTooManyOptions()
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--collectionOperation' => 'get', '--itemOperation' => 'get', '--type' => 'output']);

$this->assertStringStartsWith('[ERROR] You can only use one of "--itemOperation" and "--collectionOperation"', trim(str_replace(["\r", "\n"], '', $this->tester->getDisplay())));
$this->assertStringStartsWith('[ERROR] You can only use one of "--itemOperation" and "--collectionOperation" options at the same time.', trim(preg_replace('/\s+/', ' ', $this->tester->getDisplay())));
}

public function testExecuteWithJsonldFormatOption()
Expand Down

0 comments on commit ba2057a

Please sign in to comment.