Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

namespace ApiPlatform\Metadata\Resource\Factory;

use ApiPlatform\Doctrine\Odm\State\Options as DoctrineOdmOptions;
use ApiPlatform\Doctrine\Orm\State\Options as DoctrineOrmOptions;
use ApiPlatform\Metadata\FilterInterface;
use ApiPlatform\Metadata\HeaderParameterInterface;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Parameter;
use ApiPlatform\Metadata\Parameters;
use ApiPlatform\Metadata\QueryParameter;
Expand Down Expand Up @@ -54,14 +57,15 @@ public function create(string $resourceClass): ResourceMetadataCollection
$resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);

foreach ($resourceMetadataCollection as $i => $resource) {
$resourceClass = $resource->getClass();
$operations = $resource->getOperations();

$internalPriority = -1;
foreach ($operations as $operationName => $operation) {
$parameters = $operation->getParameters() ?? new Parameters();
foreach ($parameters as $key => $parameter) {
$key = $parameter->getKey() ?? $key;
$parameter = $this->setDefaults($key, $parameter, $resourceClass);
$parameter = $this->setDefaults($key, $parameter, $resourceClass, $operation);
$priority = $parameter->getPriority() ?? $internalPriority--;
$parameters->add($key, $parameter->withPriority($priority));
}
Expand All @@ -87,7 +91,7 @@ public function create(string $resourceClass): ResourceMetadataCollection
$parameters = $operation->getParameters() ?? new Parameters();
foreach ($operation->getParameters() ?? [] as $key => $parameter) {
$key = $parameter->getKey() ?? $key;
$parameter = $this->setDefaults($key, $parameter, $resourceClass);
$parameter = $this->setDefaults($key, $parameter, $resourceClass, $operation);
$priority = $parameter->getPriority() ?? $internalPriority--;
$parameters->add($key, $parameter->withPriority($priority));
}
Expand All @@ -101,7 +105,7 @@ public function create(string $resourceClass): ResourceMetadataCollection
return $resourceMetadataCollection;
}

private function setDefaults(string $key, Parameter $parameter, string $resourceClass): Parameter
private function setDefaults(string $key, Parameter $parameter, string $resourceClass, Operation $operation): Parameter
{
if (null === $parameter->getKey()) {
$parameter = $parameter->withKey($key);
Expand All @@ -117,7 +121,7 @@ private function setDefaults(string $key, Parameter $parameter, string $resource
}

// Read filter description to populate the Parameter
$description = $filter instanceof FilterInterface ? $filter->getDescription($resourceClass) : [];
$description = $filter instanceof FilterInterface ? $filter->getDescription($this->getFilterClass($operation)) : [];
if (($schema = $description[$key]['schema'] ?? null) && null === $parameter->getSchema()) {
$parameter = $parameter->withSchema($schema);
}
Expand Down Expand Up @@ -242,7 +246,7 @@ private function addFilterValidation(HttpOperation $operation): Parameters
}

$filter = $this->filterLocator->get($filter);
foreach ($filter->getDescription($operation->getClass()) as $parameterName => $definition) {
foreach ($filter->getDescription($this->getFilterClass($operation)) as $parameterName => $definition) {
$key = $parameterName;
$required = $definition['required'] ?? false;
$schema = $definition['schema'] ?? null;
Expand Down Expand Up @@ -296,4 +300,19 @@ private function addFilterValidation(HttpOperation $operation): Parameters

return $parameters;
}

private function getFilterClass(Operation $operation): ?string
{
$stateOptions = $operation->getStateOptions();

if ($stateOptions instanceof DoctrineOrmOptions) {
return $stateOptions->getEntityClass();
}

if ($stateOptions instanceof DoctrineOdmOptions) {
return $stateOptions->getDocumentClass();
}

return $operation->getClass();
}
}
90 changes: 90 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/AgentApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource;

use ApiPlatform\Doctrine\Odm\Filter\DateFilter as OdmDateFilter;
use ApiPlatform\Doctrine\Odm\State\Options as OdmOptions;
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\AgentDocument;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Agent;

#[ApiFilter(DateFilter::class, properties: ['birthday'], alias: 'app_filter_date')]
#[ApiResource(
shortName: 'Agent',
operations: [
new GetCollection(parameters: [
'birthday' => new QueryParameter(filter: 'app_filter_date'),
]),
],
stateOptions: new Options(entityClass: Agent::class)
)]
#[ApiFilter(OdmDateFilter::class, properties: ['birthday'], alias: 'app_filter_date_odm')]
#[ApiResource(
shortName: 'AgentDocument',
operations: [
new GetCollection(parameters: [
'birthday' => new QueryParameter(filter: 'app_filter_date_odm'),
]),
],
stateOptions: new OdmOptions(documentClass: AgentDocument::class)
)]
class AgentApi
{
private ?int $id = null;

private ?string $name = null;

private ?\DateTimeInterface $birthday = null;

public function getId(): ?int
{
return $this->id;
}

public function setId(?int $id): self
{
$this->id = $id;

return $this;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(?string $name): self
{
$this->name = $name;

return $this;
}

public function getBirthday(): ?\DateTimeInterface
{
return $this->birthday;
}

public function setBirthday(?\DateTimeInterface $birthday): self
{
$this->birthday = $birthday;

return $this;
}
}
95 changes: 95 additions & 0 deletions tests/Fixtures/TestBundle/Document/AgentDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

#[ODM\Document]
class AgentDocument
{
#[ODM\Id(strategy: 'INCREMENT', type: 'int')]
public ?int $id = null;

#[ODM\Field]
public ?string $name = null;

#[ODM\Field]
public ?string $apiKey = null;

#[ODM\Field]
public ?\DateTimeImmutable $createdAt = null;

#[ODM\Field]
public ?\DateTimeImmutable $birthday = null;

public function getId(): ?int
{
return $this->id;
}

public function setId(int $id): static
{
$this->id = $id;

return $this;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): static
{
$this->name = $name;

return $this;
}

public function getApiKey(): ?string
{
return $this->apiKey;
}

public function setApiKey(string $apiKey): static
{
$this->apiKey = $apiKey;

return $this;
}

public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}

public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;

return $this;
}

public function getBirthday(): ?\DateTimeImmutable
{
return $this->birthday;
}

public function setBirthday(\DateTimeImmutable $birthday): static
{
$this->birthday = $birthday;

return $this;
}
}
82 changes: 82 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Agent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Agent
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
public ?int $id = null;

#[ORM\Column(length: 255)]
public ?string $name = null;

#[ORM\Column]
public ?\DateTimeImmutable $createdAt = null;

#[ORM\Column]
public ?\DateTimeImmutable $birthday = null;

public function getId(): ?int
{
return $this->id;
}

public function setId(int $id): static
{
$this->id = $id;

return $this;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): static
{
$this->name = $name;

return $this;
}

public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}

public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;

return $this;
}

public function getBirthday(): ?\DateTimeImmutable
{
return $this->birthday;
}

public function setBirthday(\DateTimeImmutable $birthday): static
{
$this->birthday = $birthday;

return $this;
}
}
Loading
Loading