-
-
Notifications
You must be signed in to change notification settings - Fork 935
Description
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