Skip to content

Commit 2fc74f2

Browse files
authoredSep 2, 2024
fix: remove PUT from default operations (#6570)
1 parent 5d5697f commit 2fc74f2

File tree

8 files changed

+40
-11
lines changed

8 files changed

+40
-11
lines changed
 

‎src/Laravel/workbench/app/Models/Book.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515

1616
use ApiPlatform\Laravel\Eloquent\Filter\SearchFilter;
1717
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\Delete;
19+
use ApiPlatform\Metadata\Get;
20+
use ApiPlatform\Metadata\GetCollection;
21+
use ApiPlatform\Metadata\Patch;
22+
use ApiPlatform\Metadata\Post;
23+
use ApiPlatform\Metadata\Put;
1824
use ApiPlatform\Metadata\QueryParameter;
1925
use Illuminate\Database\Eloquent\Concerns\HasUlids;
2026
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -25,7 +31,15 @@
2531
#[ApiResource(
2632
paginationEnabled: true,
2733
paginationItemsPerPage: 5,
28-
rules: BookFormRequest::class
34+
rules: BookFormRequest::class,
35+
operations: [
36+
new Put(),
37+
new Patch(),
38+
new Get(),
39+
new Post(),
40+
new Delete(),
41+
new GetCollection(),
42+
]
2943
)]
3044
#[QueryParameter(key: ':property', filter: SearchFilter::class)]
3145
class Book extends Model

‎src/Metadata/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/composer.lock
22
/vendor
3-
/.phpunit.result.cache
3+
/.phpunit.cache

‎src/Metadata/Resource/Factory/MetadataCollectionFactoryTrait.php

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ private function isResourceMetadata(string $name): bool
5555
* Get
5656
* Post
5757
* Resource
58-
* Put
5958
* Get
6059
* In the future, we will be able to use nested attributes (https://wiki.php.net/rfc/new_in_initializers).
6160
*

‎src/Metadata/Resource/Factory/OperationDefaultsTrait.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
use ApiPlatform\Metadata\Operations;
3131
use ApiPlatform\Metadata\Patch;
3232
use ApiPlatform\Metadata\Post;
33-
use ApiPlatform\Metadata\Put;
3433
use ApiPlatform\Metadata\Util\CamelCaseToSnakeCaseNameConverter;
3534
use ApiPlatform\State\CreateProvider;
3635
use Psr\Log\LoggerInterface;
@@ -96,7 +95,13 @@ private function getDefaultHttpOperations($resource): iterable
9695
$operations = [];
9796

9897
foreach ($defaultOperations as $defaultOperation) {
99-
$operations[] = new $defaultOperation();
98+
$operation = new $defaultOperation();
99+
100+
if ($operation instanceof Post && $resource->getUriTemplate() && !$resource->getProvider()) {
101+
$operation = $operation->withProvider(CreateProvider::class);
102+
}
103+
104+
$operations[] = $operation;
100105
}
101106

102107
return new Operations($operations);
@@ -107,7 +112,7 @@ private function getDefaultHttpOperations($resource): iterable
107112
$post = $post->withProvider(CreateProvider::class);
108113
}
109114

110-
return [new Get(), new GetCollection(), $post, new Put(), new Patch(), new Delete()];
115+
return [new Get(), new GetCollection(), $post, new Patch(), new Delete()];
111116
}
112117

113118
private function addDefaultGraphQlOperations(ApiResource $resource): ApiResource

‎src/Metadata/Tests/Extractor/ResourceMetadataCompatibilityTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use ApiPlatform\Metadata\Operations;
2929
use ApiPlatform\Metadata\Patch;
3030
use ApiPlatform\Metadata\Post;
31-
use ApiPlatform\Metadata\Put;
3231
use ApiPlatform\Metadata\QueryParameter;
3332
use ApiPlatform\Metadata\Resource\Factory\ExtractorResourceMetadataCollectionFactory;
3433
use ApiPlatform\Metadata\Resource\Factory\OperationDefaultsTrait;
@@ -549,7 +548,7 @@ private function buildApiResources(): array
549548
if (null === $fixtures) {
550549
// Build default operations
551550
$operations = [];
552-
foreach ([new Get(), new GetCollection(), new Post(), new Put(), new Patch(), new Delete()] as $operation) {
551+
foreach ([new Get(), new GetCollection(), new Post(), new Patch(), new Delete()] as $operation) {
553552
[$name, $operation] = $this->getOperationWithDefaults($resource, $operation);
554553
$operations[$name] = $operation;
555554
}

‎src/Metadata/Tests/Resource/Factory/AttributesResourceMetadataCollectionFactoryTest.php

-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ class: AttributeDefaultOperations::class,
164164
'_api_AttributeDefaultOperations_get' => (new Get())->withOperation($operation),
165165
'_api_AttributeDefaultOperations_get_collection' => (new GetCollection())->withOperation($operation),
166166
'_api_AttributeDefaultOperations_post' => (new Post())->withOperation($operation),
167-
'_api_AttributeDefaultOperations_put' => (new Put())->withOperation($operation),
168167
'_api_AttributeDefaultOperations_patch' => (new Patch())->withOperation($operation),
169168
'_api_AttributeDefaultOperations_delete' => (new Delete())->withOperation($operation),
170169
],
@@ -208,7 +207,6 @@ class: AttributeDefaultOperations::class,
208207
'_api_AttributeDefaultOperations_get' => (new Get())->withOperation($operation),
209208
'_api_AttributeDefaultOperations_get_collection' => (new GetCollection())->withOperation($operation),
210209
'_api_AttributeDefaultOperations_post' => (new Post())->withOperation($operation),
211-
'_api_AttributeDefaultOperations_put' => (new Put())->withOperation($operation),
212210
'_api_AttributeDefaultOperations_patch' => (new Patch())->withOperation($operation),
213211
'_api_AttributeDefaultOperations_delete' => (new Delete())->withOperation($operation),
214212
],

‎tests/Fixtures/TestBundle/Entity/SubresourceEmployee.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Doctrine\ORM\Mapping as ORM;
1818

1919
#[ApiResource(
20-
'/subresource_organizations/{subresourceOrganization}/subresource_employees',
20+
uriTemplate: '/subresource_organizations/{subresourceOrganization}/subresource_employees',
2121
types: ['https://schema.org/Person']
2222
)]
2323
#[ORM\Entity]

‎tests/Fixtures/app/AppKernel.php

+14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
declare(strict_types=1);
1313

14+
use ApiPlatform\Metadata\Delete;
15+
use ApiPlatform\Metadata\Get;
16+
use ApiPlatform\Metadata\GetCollection;
17+
use ApiPlatform\Metadata\Patch;
18+
use ApiPlatform\Metadata\Post;
19+
use ApiPlatform\Metadata\Put;
1420
use ApiPlatform\Symfony\Bundle\ApiPlatformBundle;
1521
use ApiPlatform\Tests\Behat\DoctrineContext;
1622
use ApiPlatform\Tests\Fixtures\TestBundle\Document\User as UserDocument;
@@ -262,6 +268,14 @@ class_exists(NativePasswordHasher::class) ? 'password_hashers' : 'encoders' => [
262268
'extra_properties' => [
263269
'standard_put' => true,
264270
],
271+
'operations' => [
272+
Get::class,
273+
GetCollection::class,
274+
Post::class,
275+
Put::class,
276+
Patch::class,
277+
Delete::class,
278+
],
265279
],
266280
]);
267281

0 commit comments

Comments
 (0)
Please sign in to comment.