Skip to content

Commit cfdc9ad

Browse files
authored
fix(metadata): add operations config (#5459)
1 parent 24acc45 commit cfdc9ad

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/Metadata/Resource/Factory/OperationDefaultsTrait.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use ApiPlatform\Metadata\GraphQl\Subscription;
2828
use ApiPlatform\Metadata\HttpOperation;
2929
use ApiPlatform\Metadata\Operation;
30+
use ApiPlatform\Metadata\Operations;
3031
use ApiPlatform\Metadata\Patch;
3132
use ApiPlatform\Metadata\Post;
3233
use ApiPlatform\Metadata\Put;
@@ -43,7 +44,12 @@ trait OperationDefaultsTrait
4344
private function addGlobalDefaults(ApiResource|Operation $operation): ApiResource|Operation
4445
{
4546
$extraProperties = $this->defaults['extra_properties'] ?? [];
47+
4648
foreach ($this->defaults as $key => $value) {
49+
if ('operations' === $key) {
50+
continue;
51+
}
52+
4753
$upperKey = ucfirst($this->camelCaseToSnakeCaseNameConverter->denormalize($key));
4854
$getter = 'get'.$upperKey;
4955

@@ -82,6 +88,16 @@ private function getResourceWithDefaults(string $resourceClass, string $shortNam
8288

8389
private function getDefaultHttpOperations($resource): iterable
8490
{
91+
if (($defaultOperations = $this->defaults['operations'] ?? null) && null === $resource->getOperations()) {
92+
$operations = [];
93+
94+
foreach ($defaultOperations as $defaultOperation) {
95+
$operations[] = new $defaultOperation();
96+
}
97+
98+
return new Operations($operations);
99+
}
100+
85101
$post = new Post();
86102
if ($resource->getUriTemplate() && !$resource->getProvider()) {
87103
$post = $post->withProvider(CreateProvider::class);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
18+
#[ApiResource]
19+
final class AttributeConfigOperations
20+
{
21+
}

tests/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactoryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use ApiPlatform\Metadata\Put;
2828
use ApiPlatform\Metadata\Resource\Factory\AttributesResourceMetadataCollectionFactory;
2929
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
30+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\AttributeConfigOperations;
3031
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\PasswordResource;
3132
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeDefaultOperations;
3233
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeOnlyOperation;
@@ -176,6 +177,22 @@ class: AttributeDefaultOperations::class,
176177
]), $attributeResourceMetadataCollectionFactory->create(AttributeDefaultOperations::class));
177178
}
178179

180+
public function testCreateWithConfigOperations(): void
181+
{
182+
$attributesResourceMetadataCollectionFactory = new AttributesResourceMetadataCollectionFactory(defaults: ['operations' => [Get::class, Post::class]]);
183+
184+
$this->assertEquals(new ResourceMetadataCollection(AttributeConfigOperations::class, [
185+
new ApiResource(
186+
shortName: 'AttributeConfigOperations',
187+
operations: [
188+
'_api_AttributeConfigOperations_get' => new Get(shortName: 'AttributeConfigOperations', class: AttributeConfigOperations::class, priority: 0, extraProperties: ['generated_operation' => true]),
189+
'_api_AttributeConfigOperations_post' => new Post(shortName: 'AttributeConfigOperations', class: AttributeConfigOperations::class, priority: 1, extraProperties: ['generated_operation' => true]),
190+
],
191+
class: AttributeConfigOperations::class,
192+
),
193+
]), $attributesResourceMetadataCollectionFactory->create(AttributeConfigOperations::class));
194+
}
195+
179196
public function testCreateShouldNotOverrideWithDefault(): void
180197
{
181198
$attributeResourceMetadataCollectionFactory = new AttributesResourceMetadataCollectionFactory(

0 commit comments

Comments
 (0)