Skip to content
Open
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
47 changes: 32 additions & 15 deletions src/Doctrine/Orm/Filter/AbstractUuidFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface;
use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareTrait;
use ApiPlatform\Doctrine\Common\PropertyHelperTrait;
use ApiPlatform\Doctrine\Orm\PropertyHelperTrait as OrmPropertyHelperTrait;
use ApiPlatform\Doctrine\Orm\NestedPropertyHelperTrait;
use ApiPlatform\Doctrine\Orm\Util\QueryBuilderHelper;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait;
Expand All @@ -33,7 +33,6 @@
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;

/**
Expand All @@ -44,14 +43,30 @@ class AbstractUuidFilter implements FilterInterface, ManagerRegistryAwareInterfa
use BackwardCompatibleFilterDescriptionTrait;
use LoggerAwareTrait;
use ManagerRegistryAwareTrait;
use OrmPropertyHelperTrait;
use NestedPropertyHelperTrait;
use PropertyHelperTrait;

private const UUID_SCHEMA = [
'type' => 'string',
'format' => 'uuid',
];

/**
* Gets class metadata for the given resource.
*/
protected function getClassMetadata(string $resourceClass): \Doctrine\Persistence\Mapping\ClassMetadata
{
$manager = $this
->getManagerRegistry()
->getManagerForClass($resourceClass);

if ($manager) {
return $manager->getClassMetadata($resourceClass);
}

return new \Doctrine\ORM\Mapping\ClassMetadata($resourceClass);
}

public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
{
$parameter = $context['parameter'] ?? null;
Expand All @@ -60,26 +75,28 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
}

if (null === $parameter->getProperty()) {
throw new InvalidArgumentException(\sprintf('The filter parameter with key "%s" must specify a property. Nested properties are not automatically resolved. Please provide the property explicitly.', $parameter->getKey()));
throw new InvalidArgumentException(\sprintf('The filter parameter with key "%s" must specify a property. Please provide the property explicitly.', $parameter->getKey()));
}

$this->filterProperty($parameter->getProperty(), $parameter->getValue(), $queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context);
$this->filterProperty($parameter, $queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context);
}

private function filterProperty(string $property, mixed $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
private function filterProperty(Parameter $parameter, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
{
$property = $parameter->getProperty();
$value = $parameter->getValue();
$alias = $queryBuilder->getRootAliases()[0];
$field = $property;

$associations = [];
if ($this->isPropertyNested($property, $resourceClass)) {
[$alias, $field, $associations] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass, Join::INNER_JOIN);
}
[$alias, $field] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $parameter);

// Get the target resource class for nested properties
$nestedInfo = $parameter->getExtraProperties()['nested_property_info'] ?? null;
$targetResourceClass = $nestedInfo['leaf_class'] ?? $resourceClass;

$metadata = $this->getNestedMetadata($resourceClass, $associations);
$metadata = $this->getClassMetadata($targetResourceClass);

if ($metadata->hasField($field)) {
$value = $this->convertValuesToTheDatabaseRepresentation($queryBuilder, $this->getDoctrineFieldType($property, $resourceClass), $value);
$value = $this->convertValuesToTheDatabaseRepresentation($queryBuilder, $this->getDoctrineFieldType($field, $targetResourceClass), $value);
$this->addWhere($queryBuilder, $queryNameGenerator, $alias, $field, $value);

return;
Expand All @@ -89,8 +106,8 @@ private function filterProperty(string $property, mixed $value, QueryBuilder $qu
if (!$metadata->hasAssociation($field)) {
$this->logger->notice('Tried to filter on a non-existent field or association', [
'field' => $field,
'resource_class' => $resourceClass,
'exception' => new InvalidArgumentException(\sprintf('Property "%s" does not exist in resource "%s".', $field, $resourceClass)),
'resource_class' => $targetResourceClass,
'exception' => new InvalidArgumentException(\sprintf('Property "%s" does not exist in resource "%s".', $field, $targetResourceClass)),
]);

return;
Expand Down
5 changes: 5 additions & 0 deletions src/Doctrine/Orm/Filter/IriFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Doctrine\Orm\Filter;

use ApiPlatform\Doctrine\Common\Filter\OpenApiFilterTrait;
use ApiPlatform\Doctrine\Orm\NestedPropertyHelperTrait;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait;
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
Expand All @@ -29,6 +30,7 @@
final class IriFilter implements FilterInterface, OpenApiParameterFilterInterface, ParameterProviderFilterInterface
{
use BackwardCompatibleFilterDescriptionTrait;
use NestedPropertyHelperTrait;
use OpenApiFilterTrait;

public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
Expand All @@ -42,6 +44,9 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q

$property = $parameter->getProperty();
$alias = $queryBuilder->getRootAliases()[0];

[$alias, $property] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $parameter);

$parameterName = $queryNameGenerator->generateParameterName($property);

$queryBuilder->join(\sprintf('%s.%s', $alias, $property), $parameterName);
Expand Down
4 changes: 2 additions & 2 deletions src/Laravel/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
"phpstan/phpdoc-parser": "^1.29 || ^2.0",
"phpunit/phpunit": "^12.2",
"symfony/http-client": "^7.4 || ^8.0",
"symfony/mcp-bundle": "dev-main",
"symfony/object-mapper": "8.1.x-dev"
"symfony/mcp-bundle": "^0.3.0",
"symfony/object-mapper": "^7.4 || ^8.0"
},
"autoload": {
"psr-4": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static function attributesProvider(): array
];
}

#[\PHPUnit\Framework\Attributes\DataProvider('attributesProvider')]
#[DataProvider('attributesProvider')]
public function testCreateWithAttributes($readAttributes, $writeAttributes): void
{
$serializerClassMetadataFactoryProphecy = $this->prophesize(SerializerClassMetadataFactoryInterface::class);
Expand Down
60 changes: 60 additions & 0 deletions tests/Fixtures/TestBundle/Entity/FilterNestedTest/Company.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\FilterNestedTest;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

/**
* Company entity for testing nested filter support.
*/
#[ORM\Entity]
#[ApiResource(
operations: [
new GetCollection(),
]
)]
class Company
{
#[ORM\Id]
#[ORM\Column(type: 'uuid')]
private Uuid $id;

Check failure on line 34 in tests/Fixtures/TestBundle/Entity/FilterNestedTest/Company.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.5)

Property ApiPlatform\Tests\Fixtures\TestBundle\Entity\FilterNestedTest\Company::$id type mapping mismatch: database can contain Ramsey\Uuid\UuidInterface but property expects Symfony\Component\Uid\Uuid.

#[ORM\Column(type: 'string', length: 255)]
private string $name;

public function __construct()
{
$this->id = Uuid::v4();
}

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

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

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

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

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\FilterNestedTest;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

/**
* Department entity for testing nested filter support.
*/
#[ORM\Entity]
#[ApiResource(
operations: [
new GetCollection(),
]
)]
class Department
{
#[ORM\Id]
#[ORM\Column(type: 'uuid')]
private Uuid $id;

Check failure on line 34 in tests/Fixtures/TestBundle/Entity/FilterNestedTest/Department.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.5)

Property ApiPlatform\Tests\Fixtures\TestBundle\Entity\FilterNestedTest\Department::$id type mapping mismatch: database can contain Ramsey\Uuid\UuidInterface but property expects Symfony\Component\Uid\Uuid.

#[ORM\Column(type: 'string', length: 255)]
private string $name;

#[ORM\ManyToOne(targetEntity: Company::class)]
#[ORM\JoinColumn(nullable: false)]
private Company $company;

public function __construct()
{
$this->id = Uuid::v4();
}

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

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

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

return $this;
}

public function getCompany(): Company
{
return $this->company;
}

public function setCompany(Company $company): self
{
$this->company = $company;

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

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\FilterNestedTest;

use ApiPlatform\Doctrine\Orm\Filter\IriFilter;
use ApiPlatform\Doctrine\Orm\Filter\UuidFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

/**
* Employee entity for testing nested filter support with IriFilter and UuidFilter.
*/
#[ORM\Entity]
#[ApiResource(
operations: [
new GetCollection(
parameters: [
// Test direct relation filtering (should work)
'department' => new QueryParameter(filter: new IriFilter()),
'departmentId' => new QueryParameter(filter: new UuidFilter(), property: 'department'),

// Test nested relation filtering (currently broken, should be fixed)
'departmentCompany' => new QueryParameter(filter: new IriFilter(), property: 'department.company'),
'departmentCompanyId' => new QueryParameter(filter: new UuidFilter(), property: 'department.company'),
]
),
]
)]
class Employee
{
#[ORM\Id]
#[ORM\Column(type: 'uuid')]
private Uuid $id;

Check failure on line 47 in tests/Fixtures/TestBundle/Entity/FilterNestedTest/Employee.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.5)

Property ApiPlatform\Tests\Fixtures\TestBundle\Entity\FilterNestedTest\Employee::$id type mapping mismatch: database can contain Ramsey\Uuid\UuidInterface but property expects Symfony\Component\Uid\Uuid.

#[ORM\Column(type: 'string', length: 255)]
private string $name;

#[ORM\ManyToOne(targetEntity: Department::class)]
#[ORM\JoinColumn(nullable: false)]
private Department $department;

public function __construct()
{
$this->id = Uuid::v4();
}

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

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

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

return $this;
}

public function getDepartment(): Department
{
return $this->department;
}

public function setDepartment(Department $department): self
{
$this->department = $department;

return $this;
}
}
Loading
Loading