diff --git a/src/Laravel/workbench/app/Models/Book.php b/src/Laravel/workbench/app/Models/Book.php index 070d94cf5b..157c98a651 100644 --- a/src/Laravel/workbench/app/Models/Book.php +++ b/src/Laravel/workbench/app/Models/Book.php @@ -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; @@ -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 diff --git a/src/Metadata/.gitignore b/src/Metadata/.gitignore index eb0a8e7b26..8e6e8828bf 100644 --- a/src/Metadata/.gitignore +++ b/src/Metadata/.gitignore @@ -1,3 +1,3 @@ /composer.lock /vendor -/.phpunit.result.cache +/.phpunit.cache diff --git a/src/Metadata/Resource/Factory/MetadataCollectionFactoryTrait.php b/src/Metadata/Resource/Factory/MetadataCollectionFactoryTrait.php index e54d71f64e..64771a5c80 100644 --- a/src/Metadata/Resource/Factory/MetadataCollectionFactoryTrait.php +++ b/src/Metadata/Resource/Factory/MetadataCollectionFactoryTrait.php @@ -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). * diff --git a/src/Metadata/Resource/Factory/OperationDefaultsTrait.php b/src/Metadata/Resource/Factory/OperationDefaultsTrait.php index dab37b23da..1c37b27145 100644 --- a/src/Metadata/Resource/Factory/OperationDefaultsTrait.php +++ b/src/Metadata/Resource/Factory/OperationDefaultsTrait.php @@ -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; @@ -96,10 +95,16 @@ 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(); @@ -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 diff --git a/src/Metadata/Tests/Extractor/ResourceMetadataCompatibilityTest.php b/src/Metadata/Tests/Extractor/ResourceMetadataCompatibilityTest.php index 879e4572a9..d8dfc53f0f 100644 --- a/src/Metadata/Tests/Extractor/ResourceMetadataCompatibilityTest.php +++ b/src/Metadata/Tests/Extractor/ResourceMetadataCompatibilityTest.php @@ -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; @@ -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; } diff --git a/src/Metadata/Tests/Resource/Factory/AttributesResourceMetadataCollectionFactoryTest.php b/src/Metadata/Tests/Resource/Factory/AttributesResourceMetadataCollectionFactoryTest.php index 124def411e..2b38b63067 100644 --- a/src/Metadata/Tests/Resource/Factory/AttributesResourceMetadataCollectionFactoryTest.php +++ b/src/Metadata/Tests/Resource/Factory/AttributesResourceMetadataCollectionFactoryTest.php @@ -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), ], @@ -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), ], diff --git a/tests/Fixtures/TestBundle/Entity/SubresourceEmployee.php b/tests/Fixtures/TestBundle/Entity/SubresourceEmployee.php index d76304fc23..50dfd7811b 100644 --- a/tests/Fixtures/TestBundle/Entity/SubresourceEmployee.php +++ b/tests/Fixtures/TestBundle/Entity/SubresourceEmployee.php @@ -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] diff --git a/tests/Fixtures/app/AppKernel.php b/tests/Fixtures/app/AppKernel.php index a936340f22..15d6e40acf 100644 --- a/tests/Fixtures/app/AppKernel.php +++ b/tests/Fixtures/app/AppKernel.php @@ -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; @@ -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, + ], ], ]);