Skip to content

Commit

Permalink
fix: remove PUT from default operations
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Sep 1, 2024
1 parent 5d5697f commit 79f4367
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 12 deletions.
16 changes: 15 additions & 1 deletion src/Laravel/workbench/app/Models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@

use ApiPlatform\Laravel\Eloquent\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\QueryParameter;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -25,7 +31,15 @@
#[ApiResource(
paginationEnabled: true,
paginationItemsPerPage: 5,
rules: BookFormRequest::class
rules: BookFormRequest::class,
operations: [
new Put(),
new Patch(),
new Get(),
new Post(),
new Delete(),
new GetCollection(),
]
)]
#[QueryParameter(key: ':property', filter: SearchFilter::class)]
class Book extends Model
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/composer.lock
/vendor
/.phpunit.result.cache
/.phpunit.cache
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ private function isResourceMetadata(string $name): bool
* Get
* Post
* Resource
* Put
* Get
* In the future, we will be able to use nested attributes (https://wiki.php.net/rfc/new_in_initializers).
*
Expand Down
13 changes: 9 additions & 4 deletions src/Metadata/Resource/Factory/OperationDefaultsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use ApiPlatform\Metadata\Operations;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Util\CamelCaseToSnakeCaseNameConverter;
use ApiPlatform\State\CreateProvider;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -96,18 +95,24 @@ private function getDefaultHttpOperations($resource): iterable
$operations = [];

foreach ($defaultOperations as $defaultOperation) {
$operations[] = new $defaultOperation();
$operation = new $defaultOperation();

if ($operation instanceof Post && $resource->getUriTemplate() && !$resource->getProvider()) {
$operation = $operation->withProvider(CreateProvider::class);
}

$operations[] = $operation;
}

return new Operations($operations);
return $operations;
}

$post = new Post();
if ($resource->getUriTemplate() && !$resource->getProvider()) {
$post = $post->withProvider(CreateProvider::class);
}

return [new Get(), new GetCollection(), $post, new Put(), new Patch(), new Delete()];
return [new Get(), new GetCollection(), $post, new Patch(), new Delete()];
}

private function addDefaultGraphQlOperations(ApiResource $resource): ApiResource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use ApiPlatform\Metadata\Operations;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\Metadata\Resource\Factory\ExtractorResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\Factory\OperationDefaultsTrait;
Expand Down Expand Up @@ -549,7 +548,7 @@ private function buildApiResources(): array
if (null === $fixtures) {
// Build default operations
$operations = [];
foreach ([new Get(), new GetCollection(), new Post(), new Put(), new Patch(), new Delete()] as $operation) {
foreach ([new Get(), new GetCollection(), new Post(), new Patch(), new Delete()] as $operation) {
[$name, $operation] = $this->getOperationWithDefaults($resource, $operation);
$operations[$name] = $operation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ class: AttributeDefaultOperations::class,
'_api_AttributeDefaultOperations_get' => (new Get())->withOperation($operation),
'_api_AttributeDefaultOperations_get_collection' => (new GetCollection())->withOperation($operation),
'_api_AttributeDefaultOperations_post' => (new Post())->withOperation($operation),
'_api_AttributeDefaultOperations_put' => (new Put())->withOperation($operation),
'_api_AttributeDefaultOperations_patch' => (new Patch())->withOperation($operation),
'_api_AttributeDefaultOperations_delete' => (new Delete())->withOperation($operation),
],
Expand Down Expand Up @@ -208,7 +207,6 @@ class: AttributeDefaultOperations::class,
'_api_AttributeDefaultOperations_get' => (new Get())->withOperation($operation),
'_api_AttributeDefaultOperations_get_collection' => (new GetCollection())->withOperation($operation),
'_api_AttributeDefaultOperations_post' => (new Post())->withOperation($operation),
'_api_AttributeDefaultOperations_put' => (new Put())->withOperation($operation),
'_api_AttributeDefaultOperations_patch' => (new Patch())->withOperation($operation),
'_api_AttributeDefaultOperations_delete' => (new Delete())->withOperation($operation),
],
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/TestBundle/Entity/SubresourceEmployee.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Doctrine\ORM\Mapping as ORM;

#[ApiResource(
'/subresource_organizations/{subresourceOrganization}/subresource_employees',
uriTemplate: '/subresource_organizations/{subresourceOrganization}/subresource_employees',
types: ['https://schema.org/Person']
)]
#[ORM\Entity]
Expand Down
14 changes: 14 additions & 0 deletions tests/Fixtures/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

declare(strict_types=1);

use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Symfony\Bundle\ApiPlatformBundle;
use ApiPlatform\Tests\Behat\DoctrineContext;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\User as UserDocument;
Expand Down Expand Up @@ -262,6 +268,14 @@ class_exists(NativePasswordHasher::class) ? 'password_hashers' : 'encoders' => [
'extra_properties' => [
'standard_put' => true,
],
'operations' => [
Get::class,
GetCollection::class,
Post::class,
Put::class,
Patch::class,
Delete::class,
],
],
]);

Expand Down

0 comments on commit 79f4367

Please sign in to comment.