Skip to content
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

fix: remove PUT from default operations #6570

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 8 additions & 3 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,7 +95,13 @@ 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);
Expand All @@ -107,7 +112,7 @@ private function getDefaultHttpOperations($resource): iterable
$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
Loading