SearchFilter not working on Uuid property anymore since update to api-platform 4.2.2
API Platform version(s) affected: 4.2.2
Note: I updated from 4.1.25 to 4.2.2. This bug is probably also present in versions 4.2.0 and 4.2.1.
Description
The SearchFilter seems to be broken when used with an Uuid property
How to reproduce
I have an endpoint with a SearchFilter on an Uuid property:
#[ApiResource(
operations: [
new GetCollection(
uriTemplate: '/human/my_devices',
),
],
)]
#[ApiFilter(
filterClass: SearchFilter::class,
properties: [
'myDevice' => 'exact',
],
)]
#[ApiFilter(
filterClass: DateFilter::class,
properties: [
'date',
],
)]
#[ORM\Entity(repositoryClass: MyDevicesRepository::class)]
#[UniqueEntity(fields: ['myDevice', 'date'])]
class MyDeviceEndpoint
{
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\Column(type: 'uuid', unique: true)]
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
private Uuid $id;
#[ORM\ManyToOne(targetEntity: MyDevice::class)]
#[ORM\JoinColumn(nullable: false)]
#[Assert\NotNull]
private MyDevice $myDevice;
[...]
class MyDevice extends AbstractEntity
{
use BlameableEntityTrait;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\Column(type: 'uuid', unique: true)]
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
private Uuid $id;
When I call my endpoint, I get the following exception if I use api-platform 4.2.2:
app.ERROR: An exception occured, transforming to an Error resource. {"exception":"[object] (TypeError(code: 0): Cannot assign string to property App\Entity\Default\MyDevice\MyDevice::$id of type Symfony\Component\Uid\Uuid at /srv/app/vendor/doctrine/persistence/src/Persistence/Reflection/RuntimeReflectionProperty.php:70)","operation":{"ApiPlatform\Metadata\GetCollection":[]}}
It was working properly on api-platform 4.1.25
Possible Solution
I tried several workarounds, and finally found a solution: I created a custom UuidSearchFilter :
<?php
namespace App\Filter;
use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Uid\Uuid;
class UuidSearchFilter extends AbstractFilter
{
protected function filterProperty(
string $property,
mixed $value,
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
?Operation $operation = null,
array $context = [],
): void {
if (null === $value || '' === $value) {
return;
}
if (is_string($value) && str_contains($value, '/')) {
$value = rtrim($value, '/');
$segments = explode('/', $value);
$value = end($segments);
} else {
// Not an IRI, ignore
return;
}
/* @phpstan-ignore-next-line */
if (is_string($value)) {
if (!Uuid::isValid($value)) {
return;
}
$value = Uuid::fromString($value);
}
$rootAlias = $queryBuilder->getRootAliases()[0];
$assocAlias = $property.'_assoc';
$joins = $queryBuilder->getDQLPart('join');
$alreadyJoined = false;
if (isset($joins[$rootAlias])) {
foreach ($joins[$rootAlias] as $join) {
if ($join->getAlias() === $assocAlias) {
$alreadyJoined = true;
break;
}
}
}
if (!$alreadyJoined) {
$queryBuilder->leftJoin(sprintf('%s.%s', $rootAlias, $property), $assocAlias);
}
$paramName = $property.'_uuid';
$queryBuilder
->andWhere(sprintf('%s.id = :%s', $assocAlias, $paramName))
->setParameter($paramName, $value);
}
Questions:
- is this behavior expected in api-platform 4.2.2?
- is my workaround a good approach ?
- is there anything else I should try or change to make this working?
Thanks, Fred
I understand this problem is difficult to understand and maybe to reproduce.
I try to make a first analysis:
- With api-platform 4.2.2 and the SearchFilter:
$this->getIriConverter()is an instance ofApp\ApiPlatform\IriConvertercode is crashing inside the call to$item = $this->getIriConverter()->getResourceFromIri($value, ['fetch_data' => false]);infilterProperty
in getResourceFromIri of IriConverter, code is crashing inside $this->provider->provide $this->provider is an instance of ApiPlatform\State\CallableProvider
in CallableProvider::provide, code is crashing inside providerInstance::provide providerInstance is an instance of ApiPlatform\State\ErrorProvider
- With api-platform 4.1.25:
$this->getIriConverter()is an instance ofApp\ApiPlatform\IriConverter$itemis an instance of MyDevice
in getResourceFromIri of IriConverter, $this->provider is an instance of ApiPlatform\State\CallableProvider
in CallableProvider::provide, providerInstance is an instance of ApiPlatform\Doctrine\Orm\State\ItemProvider
Question is: why providerInstance was an ItemProvider with 4.1.25 version, and becomes en ErrorProvider with 4.2.2
Same here
Uuid is no more mapped as binary input, but passed as object to the Query
Oh I see the faulty is https://github.com/api-platform/core/commit/d7bab4bb36af636ee11fc2c875dbb1ce687a50d1 I'm surprised we have no test that covers this, it'd be awesome if someone could add some fixtures and a test inside DoctrineTest.php.
@soyuka Were you able to reproduce it?
I had tried on branch 4.2 and with postgres but without success
This doesn't work with SQLite, probably due to https://github.com/doctrine/orm/issues/11358, @ambroisemaupate Are you using SQLite ? But this doesn't exactly resemble the problem of @fcharrier
<?php
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7465;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use Symfony\Component\Uid\Uuid;
use Doctrine\ORM\Mapping as ORM;
#[ApiResource]
#[ApiFilter(
filterClass: SearchFilter::class,
properties: [
'myDevice' => 'exact',
],
)]
#[ORM\Entity]
class MyDeviceEndpoint
{
#[ORM\Id]
#[ORM\Column(type: 'symfony_uuid', unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
public Uuid $id;
#[ORM\ManyToOne]
public ?MyDevice $myDevice = null;
public function __construct()
{
$this->id = Uuid::v7();
}
}
<?php
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7465;
use ApiPlatform\Metadata\ApiResource;
use Symfony\Component\Uid\Uuid;
use Doctrine\ORM\Mapping as ORM;
#[ApiResource]
#[ORM\Entity]
class MyDevice
{
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\Column(type: 'symfony_uuid', unique: true)]
#[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
public Uuid $id;
public function __construct()
{
$this->id = Uuid::v7();
}
}
'http://127.0.0.1:37131/my_device_endpoints?page=1&itemsPerPage=7&myDevice=019b16f9-df6d-7349-9c64-6731f1c3759f' \
-H 'accept: application/ld+json'
{
"@context": "/contexts/MyDeviceEndpoint",
"@id": "/my_device_endpoints",
"@type": "hydra:Collection",
"hydra:totalItems": 1,
"hydra:member": [
{
"@id": "/my_device_endpoints/019b16fa-4725-74a4-a5cd-4440b1b66ed5",
"@type": "MyDeviceEndpoint",
"id": "019b16fa-4725-74a4-a5cd-4440b1b66ed5",
"myDevice": "/my_devices/019b16f9-df6d-7349-9c64-6731f1c3759f"
}
],
"hydra:view": {
"@id": "/my_device_endpoints?itemsPerPage=7&myDevice=019b16f9-df6d-7349-9c64-6731f1c3759f",
"@type": "hydra:PartialCollectionView"
},
"hydra:search": {
"@type": "hydra:IriTemplate",
"hydra:template": "/my_device_endpoints{?myDevice,myDevice[]}",
"hydra:variableRepresentation": "BasicRepresentation",
"hydra:mapping": [
{
"@type": "IriTemplateMapping",
"variable": "myDevice",
"property": "myDevice",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "myDevice[]",
"property": "myDevice",
"required": false
}
]
}
}
SELECT m0_.id AS id_0, m0_.myDevice_id AS mydevice_id_1 FROM MyDeviceEndpoint m0_ WHERE m0_.myDevice_id = '019b16f9-df6d-7349-9c64-6731f1c3759f' ORDER BY m0_.id ASC LIMIT 7;
Did I miss something ?
Looks like the search occurs in a relation... Also we have lots of uuid bugs, I would like to reuse the code of #4689 to create a proper uuid filter I know quite exactly where we could head to have something powerful and clean.