Skip to content

Commit 2c01977

Browse files
authored
Merge branch '4.2' into refactor-imported-DataProvider-instead-of-FQCN
2 parents 9b4cda6 + 03b18a5 commit 2c01977

20 files changed

Lines changed: 215 additions & 33 deletions

src/Doctrine/Odm/Metadata/Resource/DoctrineMongoDbOdmResourceCollectionMetadataFactory.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public function create(string $resourceClass): ResourceMetadataCollection
5050
continue;
5151
}
5252

53-
$operations->add($operationName, $this->addDefaults($operation));
53+
$operation = $this->addDefaults($operation);
54+
$operation = $this->setParametersFilterClass($operation, $documentClass);
55+
$operations->add($operationName, $operation);
5456
}
5557

5658
$resourceMetadata = $resourceMetadata->withOperations($operations);
@@ -60,11 +62,14 @@ public function create(string $resourceClass): ResourceMetadataCollection
6062

6163
if ($graphQlOperations) {
6264
foreach ($graphQlOperations as $operationName => $graphQlOperation) {
63-
if (!$this->managerRegistry->getManagerForClass($graphQlOperation->getClass()) instanceof DocumentManager) {
65+
$documentClass = $this->getStateOptionsClass($graphQlOperation, $graphQlOperation->getClass(), Options::class);
66+
if (!$this->managerRegistry->getManagerForClass($documentClass) instanceof DocumentManager) {
6467
continue;
6568
}
6669

67-
$graphQlOperations[$operationName] = $this->addDefaults($graphQlOperation);
70+
$graphQlOperation = $this->addDefaults($graphQlOperation);
71+
$graphQlOperation = $this->setParametersFilterClass($graphQlOperation, $documentClass);
72+
$graphQlOperations[$operationName] = $graphQlOperation;
6873
}
6974

7075
$resourceMetadata = $resourceMetadata->withGraphQlOperations($graphQlOperations);
@@ -112,4 +117,20 @@ private function getProcessor(Operation $operation): string
112117

113118
return 'api_platform.doctrine_mongodb.odm.state.persist_processor';
114119
}
120+
121+
private function setParametersFilterClass(Operation $operation, string $documentClass): Operation
122+
{
123+
$parameters = $operation->getParameters();
124+
if (!$parameters) {
125+
return $operation;
126+
}
127+
128+
foreach ($parameters as $key => $parameter) {
129+
if (null === $parameter->getFilterClass()) {
130+
$parameters->add($key, $parameter->withFilterClass($documentClass));
131+
}
132+
}
133+
134+
return $operation->withParameters($parameters);
135+
}
115136
}

src/Doctrine/Orm/Metadata/Resource/DoctrineOrmResourceCollectionMetadataFactory.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public function create(string $resourceClass): ResourceMetadataCollection
5151
continue;
5252
}
5353

54-
$operations->add($operationName, $this->addDefaults($operation));
54+
$operation = $this->addDefaults($operation);
55+
$operation = $this->setParametersFilterClass($operation, $entityClass);
56+
$operations->add($operationName, $operation);
5557
}
5658

5759
$resourceMetadata = $resourceMetadata->withOperations($operations);
@@ -67,7 +69,9 @@ public function create(string $resourceClass): ResourceMetadataCollection
6769
continue;
6870
}
6971

70-
$graphQlOperations[$operationName] = $this->addDefaults($graphQlOperation);
72+
$graphQlOperation = $this->addDefaults($graphQlOperation);
73+
$graphQlOperation = $this->setParametersFilterClass($graphQlOperation, $entityClass);
74+
$graphQlOperations[$operationName] = $graphQlOperation;
7175
}
7276

7377
$resourceMetadata = $resourceMetadata->withGraphQlOperations($graphQlOperations);
@@ -115,4 +119,20 @@ private function getProcessor(Operation $operation): string
115119

116120
return 'api_platform.doctrine.orm.state.persist_processor';
117121
}
122+
123+
private function setParametersFilterClass(Operation $operation, string $entityClass): Operation
124+
{
125+
$parameters = $operation->getParameters();
126+
if (!$parameters) {
127+
return $operation;
128+
}
129+
130+
foreach ($parameters as $key => $parameter) {
131+
if (null === $parameter->getFilterClass()) {
132+
$parameters->add($key, $parameter->withFilterClass($entityClass));
133+
}
134+
}
135+
136+
return $operation->withParameters($parameters);
137+
}
118138
}

src/Hal/Tests/Serializer/CollectionNormalizerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use ApiPlatform\Metadata\ResourceClassResolverInterface;
2323
use ApiPlatform\State\Pagination\PaginatorInterface;
2424
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
25+
use PHPUnit\Framework\Attributes\Group;
2526
use PHPUnit\Framework\TestCase;
2627
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2728

@@ -30,7 +31,7 @@
3031
*/
3132
class CollectionNormalizerTest extends TestCase
3233
{
33-
#[\PHPUnit\Framework\Attributes\Group('legacy')]
34+
#[Group('legacy')]
3435
public function testSupportsNormalize(): void
3536
{
3637
$resourceClassResolverMock = $this->createMock(ResourceClassResolverInterface::class);

src/Hal/Tests/Serializer/ItemNormalizerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MaxDepthDummy;
3030
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
3131
use PHPUnit\Framework\Attributes\DataProvider;
32+
use PHPUnit\Framework\Attributes\Group;
3233
use PHPUnit\Framework\TestCase;
3334
use Prophecy\Argument;
3435
use Prophecy\PhpUnit\ProphecyTrait;
@@ -73,7 +74,7 @@ public function testDoesNotSupportDenormalization(): void
7374
$normalizer->denormalize(['foo'], 'Foo');
7475
}
7576

76-
#[\PHPUnit\Framework\Attributes\Group('legacy')]
77+
#[Group('legacy')]
7778
public function testSupportsNormalization(): void
7879
{
7980
$std = new \stdClass();

src/Hal/Tests/Serializer/ObjectNormalizerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Hal\Serializer\ObjectNormalizer;
1717
use ApiPlatform\Hal\Tests\Fixtures\Dummy;
1818
use ApiPlatform\Metadata\IriConverterInterface;
19+
use PHPUnit\Framework\Attributes\Group;
1920
use PHPUnit\Framework\TestCase;
2021
use Symfony\Component\Serializer\Exception\LogicException;
2122
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
@@ -42,7 +43,7 @@ public function testDoesNotSupportDenormalization(): void
4243
$normalizer->denormalize(['foo'], 'Foo');
4344
}
4445

45-
#[\PHPUnit\Framework\Attributes\Group('legacy')]
46+
#[Group('legacy')]
4647
public function testSupportsNormalization(): void
4748
{
4849
$std = new \stdClass();

src/Metadata/Parameter.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ abstract class Parameter
3030
* @param Type $nativeType the PHP native type, we cast values to an array if its a CollectionType, if not and it's an array with a single value we use it (eg: HTTP Header)
3131
* @param ?bool $castToNativeType whether API Platform should cast your parameter to the nativeType declared
3232
* @param ?callable(mixed): mixed $castFn the closure used to cast your parameter, this gets called only when $castToNativeType is set
33+
* @param ?string $filterClass the class to use when resolving filter properties (from stateOptions)
3334
*
3435
* @phpstan-param array<string, mixed>|null $schema
3536
*
@@ -56,6 +57,7 @@ public function __construct(
5657
protected ?bool $castToArray = null,
5758
protected ?bool $castToNativeType = null,
5859
protected mixed $castFn = null,
60+
protected ?string $filterClass = null,
5961
) {
6062
}
6163

@@ -370,4 +372,17 @@ public function withCastFn(mixed $castFn): self
370372

371373
return $self;
372374
}
375+
376+
public function getFilterClass(): ?string
377+
{
378+
return $this->filterClass;
379+
}
380+
381+
public function withFilterClass(?string $filterClass): self
382+
{
383+
$self = clone $this;
384+
$self->filterClass = $filterClass;
385+
386+
return $self;
387+
}
373388
}

src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,23 @@ public function create(string $resourceClass): ResourceMetadataCollection
9494
/**
9595
* @return array{propertyNames: string[], properties: array<string, ApiProperty>}
9696
*/
97-
private function getProperties(string $resourceClass, ?Parameter $parameter = null): array
97+
private function getProperties(string $resourceClass, ?Parameter $parameter = null, ?Operation $operation = null): array
9898
{
99-
$k = $resourceClass.($parameter?->getProperties() ? ($parameter->getKey() ?? '') : '').(\is_string($parameter->getFilter()) ? $parameter->getFilter() : '');
99+
$filterClass = $parameter?->getFilterClass();
100+
if (null === $filterClass && null !== $operation) {
101+
$filterClass = $this->getStateOptionsClass($operation, $resourceClass);
102+
}
103+
$filterClass ??= $resourceClass;
104+
105+
$k = $resourceClass.($parameter?->getProperties() ? ($parameter->getKey() ?? '') : '').(\is_string($parameter->getFilter()) ? $parameter->getFilter() : '').$filterClass;
100106
if (isset($this->localPropertyCache[$k])) {
101107
return $this->localPropertyCache[$k];
102108
}
103109

104110
$propertyNames = [];
105111
$properties = [];
106-
foreach ($parameter?->getProperties() ?? $this->propertyNameCollectionFactory->create($resourceClass) as $property) {
107-
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property);
112+
foreach ($parameter?->getProperties() ?? $this->propertyNameCollectionFactory->create($filterClass) as $property) {
113+
$propertyMetadata = $this->propertyMetadataFactory->create($filterClass, $property);
108114
if ($propertyMetadata->isReadable()) {
109115
$propertyNames[] = $property;
110116
$properties[$property] = $propertyMetadata;
@@ -147,7 +153,7 @@ private function getDefaultParameters(Operation $operation, string $resourceClas
147153
$parameter = $parameter->withKey($key);
148154
}
149155

150-
['propertyNames' => $propertyNames, 'properties' => $properties] = $this->getProperties($resourceClass, $parameter);
156+
['propertyNames' => $propertyNames, 'properties' => $properties] = $this->getProperties($resourceClass, $parameter, $operation);
151157
$parameter = $parameter->withProperties($propertyNames);
152158

153159
foreach ($propertyNames as $property) {
@@ -176,7 +182,7 @@ private function getDefaultParameters(Operation $operation, string $resourceClas
176182

177183
$key = $parameter->getKey();
178184

179-
['propertyNames' => $propertyNames, 'properties' => $properties] = $this->getProperties($resourceClass, $parameter);
185+
['propertyNames' => $propertyNames, 'properties' => $properties] = $this->getProperties($resourceClass, $parameter, $operation);
180186

181187
if ($filter instanceof PropertiesAwareInterface) {
182188
$parameter = $parameter->withProperties($propertyNames);

src/Serializer/Tests/AbstractItemNormalizerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\RelatedDummy;
4444
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\SecuredDummy;
4545
use Doctrine\Common\Collections\ArrayCollection;
46+
use PHPUnit\Framework\Attributes\Group;
4647
use PHPUnit\Framework\TestCase;
4748
use Prophecy\Argument;
4849
use Prophecy\PhpUnit\ProphecyTrait;
@@ -65,7 +66,7 @@ class AbstractItemNormalizerTest extends TestCase
6566
{
6667
use ProphecyTrait;
6768

68-
#[\PHPUnit\Framework\Attributes\Group('legacy')]
69+
#[Group('legacy')]
6970
public function testSupportNormalizationAndSupportDenormalization(): void
7071
{
7172
$std = new \stdClass();

src/Symfony/Bundle/Resources/config/doctrine_mongodb_odm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@
217217
$services->alias('api_platform.state.item_provider', 'ApiPlatform\Doctrine\Odm\State\ItemProvider');
218218

219219
$services->set('api_platform.doctrine.odm.metadata.resource.metadata_collection_factory', DoctrineMongoDbOdmResourceCollectionMetadataFactory::class)
220-
->decorate('api_platform.metadata.resource.metadata_collection_factory', null, 40)
220+
->decorate('api_platform.metadata.resource.metadata_collection_factory', null, -50)
221221
->args([
222222
service('doctrine_mongodb'),
223223
service('api_platform.doctrine.odm.metadata.resource.metadata_collection_factory.inner'),

src/Symfony/Bundle/Resources/config/doctrine_orm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@
247247
->args([[]]);
248248

249249
$services->set('api_platform.doctrine.orm.metadata.resource.metadata_collection_factory', DoctrineOrmResourceCollectionMetadataFactory::class)
250-
->decorate('api_platform.metadata.resource.metadata_collection_factory', null, 40)
250+
->decorate('api_platform.metadata.resource.metadata_collection_factory', null, -50)
251251
->args([
252252
service('doctrine'),
253253
service('api_platform.doctrine.orm.metadata.resource.metadata_collection_factory.inner'),

0 commit comments

Comments
 (0)