Skip to content

Commit 20f4b70

Browse files
committed
fixes
1 parent 1fd61cb commit 20f4b70

File tree

5 files changed

+49
-112
lines changed

5 files changed

+49
-112
lines changed

src/Metadata/Metadata.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
namespace ApiPlatform\Metadata;
1515

16-
use ApiPlatform\Doctrine\Odm\State\Options as DoctrineOdmOptions;
17-
use ApiPlatform\Doctrine\Orm\State\Options as DoctrineOrmOptions;
1816
use ApiPlatform\State\OptionsInterface;
1917

2018
/**
@@ -619,21 +617,4 @@ public function withExtraProperties(array $extraProperties = []): static
619617

620618
return $self;
621619
}
622-
623-
public function getClassFromStateOptions(): ?string
624-
{
625-
$stateOptions = $this->getStateOptions();
626-
627-
if ($stateOptions instanceof OptionsInterface) {
628-
if ($stateOptions instanceof DoctrineOrmOptions) {
629-
return $stateOptions->getEntityClass();
630-
}
631-
632-
if ($stateOptions instanceof DoctrineOdmOptions) {
633-
return $stateOptions->getDocumentClass();
634-
}
635-
}
636-
637-
return $this->getClass();
638-
}
639620
}

src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313

1414
namespace ApiPlatform\Metadata\Resource\Factory;
1515

16+
use ApiPlatform\Doctrine\Odm\State\Options as DoctrineOdmOptions;
17+
use ApiPlatform\Doctrine\Orm\State\Options as DoctrineOrmOptions;
1618
use ApiPlatform\Metadata\FilterInterface;
1719
use ApiPlatform\Metadata\HeaderParameterInterface;
1820
use ApiPlatform\Metadata\HttpOperation;
21+
use ApiPlatform\Metadata\Operation;
1922
use ApiPlatform\Metadata\Parameter;
2023
use ApiPlatform\Metadata\Parameters;
2124
use ApiPlatform\Metadata\QueryParameter;
@@ -54,15 +57,15 @@ public function create(string $resourceClass): ResourceMetadataCollection
5457
$resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
5558

5659
foreach ($resourceMetadataCollection as $i => $resource) {
57-
$resourceClass = $resource->getClassFromStateOptions();
60+
$resourceClass = $resource->getClass();
5861
$operations = $resource->getOperations();
5962

6063
$internalPriority = -1;
6164
foreach ($operations as $operationName => $operation) {
6265
$parameters = $operation->getParameters() ?? new Parameters();
6366
foreach ($parameters as $key => $parameter) {
6467
$key = $parameter->getKey() ?? $key;
65-
$parameter = $this->setDefaults($key, $parameter, $resourceClass);
68+
$parameter = $this->setDefaults($key, $parameter, $resourceClass, $operation);
6669
$priority = $parameter->getPriority() ?? $internalPriority--;
6770
$parameters->add($key, $parameter->withPriority($priority));
6871
}
@@ -88,7 +91,7 @@ public function create(string $resourceClass): ResourceMetadataCollection
8891
$parameters = $operation->getParameters() ?? new Parameters();
8992
foreach ($operation->getParameters() ?? [] as $key => $parameter) {
9093
$key = $parameter->getKey() ?? $key;
91-
$parameter = $this->setDefaults($key, $parameter, $resourceClass);
94+
$parameter = $this->setDefaults($key, $parameter, $resourceClass, $operation);
9295
$priority = $parameter->getPriority() ?? $internalPriority--;
9396
$parameters->add($key, $parameter->withPriority($priority));
9497
}
@@ -102,7 +105,7 @@ public function create(string $resourceClass): ResourceMetadataCollection
102105
return $resourceMetadataCollection;
103106
}
104107

105-
private function setDefaults(string $key, Parameter $parameter, string $resourceClass): Parameter
108+
private function setDefaults(string $key, Parameter $parameter, string $resourceClass, Operation $operation): Parameter
106109
{
107110
if (null === $parameter->getKey()) {
108111
$parameter = $parameter->withKey($key);
@@ -118,7 +121,7 @@ private function setDefaults(string $key, Parameter $parameter, string $resource
118121
}
119122

120123
// Read filter description to populate the Parameter
121-
$description = $filter instanceof FilterInterface ? $filter->getDescription($resourceClass) : [];
124+
$description = $filter instanceof FilterInterface ? $filter->getDescription($this->getFilterClass($operation)) : [];
122125
if (($schema = $description[$key]['schema'] ?? null) && null === $parameter->getSchema()) {
123126
$parameter = $parameter->withSchema($schema);
124127
}
@@ -243,7 +246,7 @@ private function addFilterValidation(HttpOperation $operation): Parameters
243246
}
244247

245248
$filter = $this->filterLocator->get($filter);
246-
foreach ($filter->getDescription($operation->getClass()) as $parameterName => $definition) {
249+
foreach ($filter->getDescription($this->getFilterClass($operation)) as $parameterName => $definition) {
247250
$key = $parameterName;
248251
$required = $definition['required'] ?? false;
249252
$schema = $definition['schema'] ?? null;
@@ -297,4 +300,19 @@ private function addFilterValidation(HttpOperation $operation): Parameters
297300

298301
return $parameters;
299302
}
303+
304+
private function getFilterClass(Operation $operation): ?string
305+
{
306+
$stateOptions = $operation->getStateOptions();
307+
308+
if ($stateOptions instanceof DoctrineOrmOptions) {
309+
return $stateOptions->getEntityClass();
310+
}
311+
312+
if ($stateOptions instanceof DoctrineOdmOptions) {
313+
return $stateOptions->getDocumentClass();
314+
}
315+
316+
return $operation->getClass();
317+
}
300318
}

tests/Fixtures/TestBundle/ApiResource/AgentApi.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,43 @@
1313

1414
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource;
1515

16+
use ApiPlatform\Doctrine\Odm\Filter\DateFilter as OdmDateFilter;
17+
use ApiPlatform\Doctrine\Odm\State\Options as OdmOptions;
1618
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
1719
use ApiPlatform\Doctrine\Orm\State\Options;
1820
use ApiPlatform\Metadata\ApiFilter;
1921
use ApiPlatform\Metadata\ApiResource;
2022
use ApiPlatform\Metadata\GetCollection;
2123
use ApiPlatform\Metadata\QueryParameter;
24+
use ApiPlatform\Tests\Fixtures\TestBundle\Document\AgentDocument;
2225
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Agent;
23-
use Symfony\Component\Serializer\Attribute\Groups;
2426

2527
#[ApiFilter(DateFilter::class, properties: ['birthday'], alias: 'app_filter_date')]
2628
#[ApiResource(
2729
shortName: 'Agent',
2830
operations: [
2931
new GetCollection(parameters: [
30-
'bla[:property]' => new QueryParameter(filter: 'app_filter_date'),
32+
'birthday' => new QueryParameter(filter: 'app_filter_date'),
3133
]),
3234
],
3335
stateOptions: new Options(entityClass: Agent::class)
3436
)]
35-
#[ApiFilter(DateFilter::class, properties: ['birthday'])]
37+
#[ApiFilter(OdmDateFilter::class, properties: ['birthday'], alias: 'app_filter_date')]
3638
#[ApiResource(
37-
shortName: 'AgentSimple',
39+
shortName: 'AgentDocument',
3840
operations: [
39-
new GetCollection(),
41+
new GetCollection(parameters: [
42+
'birthday' => new QueryParameter(filter: 'app_filter_date'),
43+
]),
4044
],
41-
stateOptions: new Options(entityClass: Agent::class)
45+
stateOptions: new OdmOptions(documentClass: AgentDocument::class)
4246
)]
4347
class AgentApi
4448
{
45-
#[Groups(['agent:read'])]
4649
private ?int $id = null;
4750

48-
#[Groups(['agent:read', 'agent:write'])]
4951
private ?string $name = null;
5052

51-
#[Groups(['agent:read', 'agent:write'])]
5253
private ?\DateTimeInterface $birthday = null;
5354

5455
public function getId(): ?int

tests/Fixtures/TestBundle/Entity/Agent.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ class Agent
2626
#[ORM\Column(length: 255)]
2727
public ?string $name = null;
2828

29-
#[ORM\Column(length: 255)]
30-
public ?string $apiKey = null;
31-
3229
#[ORM\Column]
3330
public ?\DateTimeImmutable $createdAt = null;
3431

@@ -59,18 +56,6 @@ public function setName(string $name): static
5956
return $this;
6057
}
6158

62-
public function getApiKey(): ?string
63-
{
64-
return $this->apiKey;
65-
}
66-
67-
public function setApiKey(string $apiKey): static
68-
{
69-
$this->apiKey = $apiKey;
70-
71-
return $this;
72-
}
73-
7459
public function getCreatedAt(): ?\DateTimeImmutable
7560
{
7661
return $this->createdAt;

tests/Functional/Parameters/QueryParameterStateOptionsTest.php

Lines changed: 15 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,73 +17,25 @@
1717
use ApiPlatform\Tests\Fixtures\TestBundle\Document\AgentDocument;
1818
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Agent;
1919
use Doctrine\ODM\MongoDB\DocumentManager;
20-
use Doctrine\ODM\MongoDB\MongoDBException;
2120
use Doctrine\ORM\EntityManagerInterface;
2221
use Doctrine\ORM\Tools\SchemaTool;
23-
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
24-
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
25-
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
26-
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
27-
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
28-
use Symfony\Contracts\HttpClient\ResponseInterface;
2922

3023
final class QueryParameterStateOptionsTest extends ApiTestCase
3124
{
32-
/**
33-
* @throws TransportExceptionInterface
34-
* @throws ServerExceptionInterface
35-
* @throws RedirectionExceptionInterface
36-
* @throws DecodingExceptionInterface
37-
* @throws ClientExceptionInterface
38-
*/
39-
public function testWithBirthdayDateFilter(): void
40-
{
41-
$this->recreateSchema();
42-
$response = self::createClient()->request('GET', 'agent_simples?birthday[before]=2000-01-01&birthday[after]=1990-01-01');
43-
$this->assertResponseIsSuccessful();
44-
45-
$agents = $this->getResponseData($response);
46-
$this->assertCount(1, $agents);
47-
48-
$validBirthdays = array_column($agents, 'birthday');
49-
$this->assertValidBirthdayRange($validBirthdays);
50-
}
51-
52-
/**
53-
* @throws TransportExceptionInterface
54-
* @throws ServerExceptionInterface
55-
* @throws RedirectionExceptionInterface
56-
* @throws DecodingExceptionInterface
57-
* @throws ClientExceptionInterface
58-
*/
5925
public function testQueryParameterStateOptions(): void
6026
{
6127
$this->recreateSchema();
62-
$response = self::createClient()->request('GET', 'agents?bla[birthday][before]=2000-01-01&bla[birthday][after]=1990-01-01');
28+
$response = self::createClient()->request('GET', ($this->isMongoDb() ? 'agent_documents' : 'agents').'?birthday[before]=2000-01-01&birthday[after]=1990-01-01');
6329
$this->assertResponseIsSuccessful();
6430

65-
$agents = $this->getResponseData($response);
31+
$data = $response->toArray();
32+
$agents = $data['hydra:member'];
6633
$this->assertCount(1, $agents);
6734

6835
$validBirthdays = array_column($agents, 'birthday');
6936
$this->assertValidBirthdayRange($validBirthdays);
7037
}
7138

72-
/**
73-
* @throws TransportExceptionInterface
74-
* @throws ServerExceptionInterface
75-
* @throws RedirectionExceptionInterface
76-
* @throws DecodingExceptionInterface
77-
* @throws ClientExceptionInterface
78-
*/
79-
private function getResponseData(ResponseInterface $response): array
80-
{
81-
$data = $response->toArray();
82-
$this->assertArrayHasKey('hydra:member', $data, '"hydra:member" key does not contain an array');
83-
84-
return $data['hydra:member'];
85-
}
86-
8739
/**
8840
* @param array<string> $birthdays
8941
*/
@@ -102,38 +54,38 @@ private function recreateSchema(array $options = []): void
10254
{
10355
self::bootKernel($options);
10456
$container = static::getContainer();
105-
106-
$isMongoDb = 'mongodb' === $container->getParameter('kernel.environment');
107-
$registry = $this->getContainer()->get($isMongoDb ? 'doctrine_mongodb' : 'doctrine');
57+
$isMongoDb = $this->isMongoDb();
58+
$registry = $container->get($isMongoDb ? 'doctrine_mongodb' : 'doctrine');
10859
$resourceClass = $isMongoDb ? AgentDocument::class : Agent::class;
109-
11060
$manager = $registry->getManager();
61+
11162
if ($manager instanceof EntityManagerInterface) {
11263
$classes = $manager->getClassMetadata($resourceClass);
11364
$schemaTool = new SchemaTool($manager);
11465

11566
@$schemaTool->dropSchema([$classes]);
11667
@$schemaTool->createSchema([$classes]);
117-
}
118-
if ($manager instanceof DocumentManager) {
68+
} elseif ($manager instanceof DocumentManager) {
11969
@$manager->getSchemaManager()->dropCollections();
12070
}
12171

12272
$birthdays = [new \DateTimeImmutable('2002-01-01'), new \DateTimeImmutable(), new \DateTimeImmutable('1990-12-31')];
12373
foreach ($birthdays as $birthday) {
12474
$agent = (new $resourceClass())
12575
->setName('Agent '.$birthday->format('Y'))
126-
->setApiKey('api_key_'.$birthday->format('Y'))
12776
->setBirthday($birthday)
12877
->setCreatedAt(new \DateTimeImmutable());
12978

13079
$manager->persist($agent);
13180
}
13281

133-
try {
134-
$manager->flush();
135-
} catch (MongoDBException|\Throwable $e) {
136-
throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
137-
}
82+
$manager->flush();
83+
}
84+
85+
private function isMongoDb(): bool
86+
{
87+
$container = static::getContainer();
88+
89+
return 'mongodb' === $container->getParameter('kernel.environment');
13890
}
13991
}

0 commit comments

Comments
 (0)