Skip to content

feat: add support for Uuid and Ulid in SearchFilter #6131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
11 changes: 10 additions & 1 deletion src/Doctrine/Odm/Filter/SearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;

/**
* The search filter allows to filter a collection by given properties.
Expand Down Expand Up @@ -221,7 +223,14 @@ protected function filterProperty(string $property, $value, Builder $aggregation
return;
}

$values = array_map($this->getIdFromValue(...), $values);
$values = array_map(function ($value) {
$id = $this->getIdFromValue($value);
if ($id instanceof Uuid || $id instanceof Ulid) {
return $id->toBinary();
}

return $id;
}, $values);

$associationResourceClass = $metadata->getAssociationTargetClass($field);
$associationFieldIdentifier = $metadata->getIdentifierFieldNames()[0];
Expand Down
5 changes: 3 additions & 2 deletions src/Doctrine/Orm/Filter/SearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;

/**
* The search filter allows to filter a collection by given properties.
Expand Down Expand Up @@ -196,7 +198,6 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
}

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

if ($metadata->hasField($field)) {
if ('id' === $field) {
$values = array_map($this->getIdFromValue(...), $values);
Expand Down Expand Up @@ -255,6 +256,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB

$expected = \count($values);
$values = array_filter($values, static fn ($value) => null !== $value);
$values = array_map(static fn ($value) => $value instanceof Uuid || $value instanceof Ulid ? $value->toBinary() : $value, $values);
if ($expected > \count($values)) {
/*
* Shouldn't this actually fail harder?
Expand All @@ -272,7 +274,6 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
$associationAlias = QueryBuilderHelper::addJoinOnce($queryBuilder, $queryNameGenerator, $alias, $associationField);
$associationField = $associationFieldIdentifier;
}

$this->addWhereByStrategy($strategy, $queryBuilder, $queryNameGenerator, $associationAlias, $associationField, $values, $caseSensitive);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class JsonSchemaContextDummy
* @var int The id
*
* @ApiProperty(identifier=true)
*
* @ODM\Id(strategy="INCREMENT", type="int")
*/
private $id;
Expand Down