Skip to content

Commit

Permalink
Merge pull request #3112 from soyuka/fix-metadata-disabled
Browse files Browse the repository at this point in the history
Add the ability to declare empty item operations
  • Loading branch information
soyuka committed Sep 27, 2019
2 parents 115cf92 + 912fca4 commit bc2a8f6
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
27 changes: 27 additions & 0 deletions src/Action/NotFoundAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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;

/**
* An action which always returns HTTP 404 Not Found. Useful for disabling an operation.
*/
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
53 changes: 53 additions & 0 deletions tests/Fixtures/TestBundle/Document/DisableItemOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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;

/**
* @ApiResource(
* collectionOperations={
* "get",
* },
* itemOperations={
* "get"={
* "controller"=NotFoundAction::class,
* "read"=false,
* "output"=false,
* },
* },
* )
* @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;
}
}
57 changes: 57 additions & 0 deletions tests/Fixtures/TestBundle/Entity/DisableItemOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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;

/**
* @ApiResource(
* collectionOperations={
* "get",
* },
* itemOperations={
* "get"={
* "controller"=NotFoundAction::class,
* "read"=false,
* "output"=false,
* },
* },
* )
* @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 bc2a8f6

Please sign in to comment.